annotate web/secure.py @ 485:d68af9e84485

working
author Matt Johnston <matt@ucc.asn.au>
date Thu, 06 Feb 2014 22:45:16 +0800
parents
children 101c66da848d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
485
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import os
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import time
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import fcntl
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import hmac
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 import binascii
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 import sys
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 import config
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 __all__ = ["get_csrf_blob", "check_csrf_blob", "setup_csrf"]
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 def get_user_hash():
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 return "aaa"
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 def setup_csrf():
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 NONCE_SIZE=16
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 global _csrf_fd, _csrf_key
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 _csrf_fd = open('%s/csrf.dat' % config.DATA_PATH, 'r+')
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 try:
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 fcntl.lockf(_csrf_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 os.fchmod(_csrf_fd.fileno(), 0600)
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 _csrf_fd.write("%d-%s" % (os.getpid(), binascii.hexlify(os.urandom(NONCE_SIZE))))
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 _csrf_fd.flush()
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 _csrf_fd.seek(0)
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 except IOError:
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 pass
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 fcntl.lockf(_csrf_fd, fcntl.LOCK_SH)
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 _csrf_key = _csrf_fd.read()
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 # keep the lock open until we go away
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 def get_csrf_blob():
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 expiry = int(config.CSRF_TIMEOUT + time.time())
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 content = '%s-%s' % (get_user_hash(), expiry)
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 mac = hmac.new(_csrf_key, content).hexdigest()
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 return "%s-%s" % (content, mac)
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 def check_csrf_blob(blob):
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 toks = blob.split('-')
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 if len(toks) != 3:
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 return False
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 user, expiry, mac = toks
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 if user != get_user_hash():
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 return False
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 try:
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 exp = int(expiry)
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 except ValueError:
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 return False
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 if exp < 1000000000:
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 return False
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 if exp > time.time():
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 return False
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 check_content = "%s-%s" % (user, expiry)
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 check_mac = hmac.new(_csrf_key, content).hexdigest()
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 if mac == check_mac:
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 return True
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 return False
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65