| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
import time, base64 |
|---|
| 19 |
from djangoid.server.models import OidStoreNonce, OidStoreAssociation, OidStoreSetting |
|---|
| 20 |
from djangoid.openid.store.interface import OpenIDStore |
|---|
| 21 |
from djangoid.openid.association import Association |
|---|
| 22 |
from djangoid.openid import cryptutil |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
class DjangoidStore(OpenIDStore): |
|---|
| 26 |
def __init__(self): |
|---|
| 27 |
self.max_nonce_age = 6 * 60 * 60 |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
def blobDecode(self, blob): |
|---|
| 32 |
return base64.decodestring(blob) |
|---|
| 33 |
|
|---|
| 34 |
def blobEncode(self, s): |
|---|
| 35 |
return base64.encodestring(s) |
|---|
| 36 |
|
|---|
| 37 |
def storeAssociation(self, server_url, association): |
|---|
| 38 |
OidStoreAssociation.objects.filter(server_url = server_url, handle = association.handle).delete() |
|---|
| 39 |
a = OidStoreAssociation(server_url = server_url, handle = association.handle, secret = self.blobEncode(association.secret), issued = association.issued, lifetime = association.lifetime, assoc_type = association.assoc_type) |
|---|
| 40 |
a.save() |
|---|
| 41 |
|
|---|
| 42 |
def getAssociation(self, server_url, handle = None): |
|---|
| 43 |
associations = None |
|---|
| 44 |
if handle is not None: |
|---|
| 45 |
associations = OidStoreAssociation.objects.filter(server_url = server_url, handle = handle) |
|---|
| 46 |
else: |
|---|
| 47 |
associations = OidStoreAssociation.objects.filter(server_url = server_url) |
|---|
| 48 |
if associations.count() == 0: |
|---|
| 49 |
return None |
|---|
| 50 |
else: |
|---|
| 51 |
assocs = [] |
|---|
| 52 |
for a in associations: |
|---|
| 53 |
adata = [a.handle, self.blobDecode(a.secret), a.issued, a.lifetime, a.assoc_type] |
|---|
| 54 |
|
|---|
| 55 |
assoc = Association(*adata) |
|---|
| 56 |
|
|---|
| 57 |
if assoc.getExpiresIn() == 0: |
|---|
| 58 |
self.removeAssociation(server_url, assoc.handle) |
|---|
| 59 |
else: |
|---|
| 60 |
assocs.append((assoc.issued, assoc)) |
|---|
| 61 |
|
|---|
| 62 |
if assocs: |
|---|
| 63 |
assocs.sort() |
|---|
| 64 |
return assocs[-1][1] |
|---|
| 65 |
else: |
|---|
| 66 |
return None |
|---|
| 67 |
|
|---|
| 68 |
def removeAssociation(self, server_url, handle): |
|---|
| 69 |
assocs = OidStoreAssociation.objects.filter(server_url = server_url, handle = handle) |
|---|
| 70 |
cnt = assocs.count() |
|---|
| 71 |
assocs.delete() |
|---|
| 72 |
return cnt > 0 |
|---|
| 73 |
|
|---|
| 74 |
def storeNonce(self, nonce): |
|---|
| 75 |
now = int(time.time()) |
|---|
| 76 |
nonce = OidStoreNonce(nonce = nonce, expires = now) |
|---|
| 77 |
nonce.save() |
|---|
| 78 |
|
|---|
| 79 |
def useNonce(self, nonce): |
|---|
| 80 |
nonce = OidStoreNonce.objects.get(nonce = nonce) |
|---|
| 81 |
if nonce.count() <= 0: |
|---|
| 82 |
present = 0 |
|---|
| 83 |
else: |
|---|
| 84 |
nonce_age = int(time.time()) - nonce.timestamp |
|---|
| 85 |
if nonce_age > self.max_nonce_age: |
|---|
| 86 |
present = 0 |
|---|
| 87 |
else: |
|---|
| 88 |
present = 1 |
|---|
| 89 |
|
|---|
| 90 |
nonce.delete() |
|---|
| 91 |
|
|---|
| 92 |
return present |
|---|
| 93 |
|
|---|
| 94 |
def getAuthKey(self): |
|---|
| 95 |
key = OidStoreSetting.objects.get(setting = "auth_key") |
|---|
| 96 |
if key.count() == 0: |
|---|
| 97 |
auth_key = cryptutil.randomString(self.AUTH_KEY_LEN) |
|---|
| 98 |
s = OidStoreSetting(setting = "auth_key", value = self.blobEncode(auth_key)) |
|---|
| 99 |
s.save() |
|---|
| 100 |
else: |
|---|
| 101 |
auth_key = self.blobDecode(key.value) |
|---|
| 102 |
|
|---|
| 103 |
if len(auth_key) != self.AUTH_KEY_LEN: |
|---|
| 104 |
fmt = "Expected %d-byte string for auth key, got %r" |
|---|
| 105 |
raise ValueError(fmt % (self.AUTH_KEY_LEN, auth_key)) |
|---|
| 106 |
|
|---|
| 107 |
return auth_key |
|---|
| 108 |
|
|---|
| 109 |
def isDumb(self): |
|---|
| 110 |
return False |
|---|