| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import base64 |
| 4 |
import pickle # nosec |
| 5 |
import time |
| 6 |
from collections import UserDict |
| 7 |
|
| 8 |
from openid.association import Association as OIDAssociation |
| 9 |
from openid.extensions.ax import FetchResponse |
| 10 |
from openid.extensions.sreg import SRegResponse |
| 11 |
from openid.store.interface import OpenIDStore as OIDStore |
| 12 |
from openid.store.nonce import SKEW |
| 13 |
|
| 14 |
from allauth.account.internal.emailkit import valid_email_or_none |
| 15 |
|
| 16 |
from .models import OpenIDNonce, OpenIDStore |
| 17 |
|
| 18 |
|
| 19 |
class JSONSafeSession(UserDict): |
| 20 |
""" |
| 21 |
openid puts e.g. class OpenIDServiceEndpoint in the session. |
| 22 |
Django 1.6 no longer pickles stuff, so we'll need to do some |
| 23 |
hacking here... |
| 24 |
""" |
| 25 |
|
| 26 |
def __init__(self, session) -> None: |
| 27 |
UserDict.__init__(self) |
| 28 |
self.data = session |
| 29 |
|
| 30 |
def __setitem__(self, key, value) -> None: |
| 31 |
data = base64.b64encode(pickle.dumps(value)).decode("ascii") |
| 32 |
return UserDict.__setitem__(self, key, data) |
| 33 |
|
| 34 |
def __getitem__(self, key): |
| 35 |
data = UserDict.__getitem__(self, key) |
| 36 |
# We can avoid all of this once this is released: |
| 37 |
# https://github.com/necaris/python3-openid/pull/71 |
| 38 |
# We're not loading pickled data from an external source, so this |
| 39 |
# is safe. |
| 40 |
return pickle.loads(base64.b64decode(data.encode("ascii"))) # nosec |
| 41 |
|
| 42 |
|
| 43 |
class OldAXAttribute: |
| 44 |
PERSON_NAME = "http://openid.net/schema/namePerson" |
| 45 |
PERSON_FIRST_NAME = "http://openid.net/schema/namePerson/first" |
| 46 |
PERSON_LAST_NAME = "http://openid.net/schema/namePerson/last" |
| 47 |
|
| 48 |
|
| 49 |
class AXAttribute: |
| 50 |
CONTACT_EMAIL = "http://axschema.org/contact/email" |
| 51 |
PERSON_NAME = "http://axschema.org/namePerson" |
| 52 |
PERSON_FIRST_NAME = "http://axschema.org/namePerson/first" |
| 53 |
PERSON_LAST_NAME = "http://axschema.org/namePerson/last" |
| 54 |
|
| 55 |
|
| 56 |
AXAttributes = [ |
| 57 |
AXAttribute.CONTACT_EMAIL, |
| 58 |
AXAttribute.PERSON_NAME, |
| 59 |
AXAttribute.PERSON_FIRST_NAME, |
| 60 |
AXAttribute.PERSON_LAST_NAME, |
| 61 |
OldAXAttribute.PERSON_NAME, |
| 62 |
OldAXAttribute.PERSON_FIRST_NAME, |
| 63 |
OldAXAttribute.PERSON_LAST_NAME, |
| 64 |
] |
| 65 |
|
| 66 |
|
| 67 |
class SRegField: |
| 68 |
EMAIL = "email" |
| 69 |
NAME = "fullname" |
| 70 |
|
| 71 |
|
| 72 |
SRegFields = [ |
| 73 |
SRegField.EMAIL, |
| 74 |
SRegField.NAME, |
| 75 |
] |
| 76 |
|
| 77 |
|
| 78 |
class DBOpenIDStore(OIDStore): |
| 79 |
def storeAssociation(self, server_url, assoc=None) -> None: |
| 80 |
secret = base64.encodebytes(assoc.secret).decode() |
| 81 |
OpenIDStore.objects.create( |
| 82 |
server_url=server_url, |
| 83 |
handle=assoc.handle, |
| 84 |
secret=secret, |
| 85 |
issued=assoc.issued, |
| 86 |
lifetime=assoc.lifetime, |
| 87 |
assoc_type=assoc.assoc_type, |
| 88 |
) |
| 89 |
|
| 90 |
def getAssociation(self, server_url, handle=None): |
| 91 |
stored_assocs = OpenIDStore.objects.filter(server_url=server_url) |
| 92 |
if handle: |
| 93 |
stored_assocs = stored_assocs.filter(handle=handle) |
| 94 |
|
| 95 |
stored_assocs.order_by("-issued") |
| 96 |
|
| 97 |
if not stored_assocs.exists(): |
| 98 |
return None |
| 99 |
|
| 100 |
return_val = None |
| 101 |
|
| 102 |
for stored_assoc in stored_assocs: |
| 103 |
assoc = OIDAssociation( |
| 104 |
stored_assoc.handle, |
| 105 |
base64.decodebytes(stored_assoc.secret.encode("utf-8")), |
| 106 |
stored_assoc.issued, |
| 107 |
stored_assoc.lifetime, |
| 108 |
stored_assoc.assoc_type, |
| 109 |
) |
| 110 |
# See: |
| 111 |
# necaris/python3-openid@1abb155c8fc7b508241cbe9d2cae24f18e4a379b |
| 112 |
if hasattr(assoc, "getExpiresIn"): |
| 113 |
expires_in = assoc.getExpiresIn() |
| 114 |
else: |
| 115 |
expires_in = assoc.expiresIn |
| 116 |
if expires_in == 0: |
| 117 |
stored_assoc.delete() |
| 118 |
else: |
| 119 |
if return_val is None: |
| 120 |
return_val = assoc |
| 121 |
|
| 122 |
return return_val |
| 123 |
|
| 124 |
def removeAssociation(self, server_url, handle) -> None: |
| 125 |
stored_assocs = OpenIDStore.objects.filter(server_url=server_url) |
| 126 |
if handle: |
| 127 |
stored_assocs = stored_assocs.filter(handle=handle) |
| 128 |
|
| 129 |
stored_assocs.delete() |
| 130 |
|
| 131 |
def useNonce(self, server_url, timestamp, salt): |
| 132 |
if abs(timestamp - time.time()) > SKEW: |
| 133 |
return False |
| 134 |
|
| 135 |
try: |
| 136 |
OpenIDNonce.objects.get( |
| 137 |
server_url=server_url, timestamp=timestamp, salt=salt |
| 138 |
) |
| 139 |
except OpenIDNonce.DoesNotExist: |
| 140 |
OpenIDNonce.objects.create( |
| 141 |
server_url=server_url, timestamp=timestamp, salt=salt |
| 142 |
) |
| 143 |
return True |
| 144 |
|
| 145 |
return False |
| 146 |
|
| 147 |
|
| 148 |
def get_email_from_response(response): |
| 149 |
email = None |
| 150 |
sreg = SRegResponse.fromSuccessResponse(response) |
| 151 |
if sreg: |
| 152 |
email = valid_email_or_none(sreg.get(SRegField.EMAIL)) |
| 153 |
if not email: |
| 154 |
ax = FetchResponse.fromSuccessResponse(response) |
| 155 |
if ax: |
| 156 |
try: |
| 157 |
values = ax.get(AXAttribute.CONTACT_EMAIL) |
| 158 |
if values: |
| 159 |
email = valid_email_or_none(values[0]) |
| 160 |
except KeyError: |
| 161 |
pass |
| 162 |
return email |
| 163 |
|
| 164 |
|
| 165 |
def get_value_from_response(response, sreg_names=None, ax_names=None): |
| 166 |
value = None |
| 167 |
if sreg_names: |
| 168 |
sreg = SRegResponse.fromSuccessResponse(response) |
| 169 |
if sreg: |
| 170 |
for name in sreg_names: |
| 171 |
value = sreg.get(name) |
| 172 |
if value: |
| 173 |
break |
| 174 |
|
| 175 |
if not value and ax_names: |
| 176 |
ax = FetchResponse.fromSuccessResponse(response) |
| 177 |
if ax: |
| 178 |
for name in ax_names: |
| 179 |
try: |
| 180 |
values = ax.get(name) |
| 181 |
if values: |
| 182 |
value = values[0] |
| 183 |
except KeyError: |
| 184 |
pass |
| 185 |
if value: |
| 186 |
break |
| 187 |
return value |