| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from hashlib import md5 |
| 4 |
|
| 5 |
from django.http import HttpRequest |
| 6 |
|
| 7 |
from allauth.socialaccount.adapter import get_adapter |
| 8 |
from allauth.socialaccount.providers.oauth2.views import ( |
| 9 |
OAuth2Adapter, |
| 10 |
OAuth2CallbackView, |
| 11 |
OAuth2LoginView, |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
USER_FIELDS = [ |
| 16 |
"age", |
| 17 |
"birthday", |
| 18 |
"current_status", |
| 19 |
"current_status_date", |
| 20 |
"current_status_id", |
| 21 |
"email", |
| 22 |
"first_name", |
| 23 |
"gender", |
| 24 |
"has_email", |
| 25 |
"last_name", |
| 26 |
"locale", |
| 27 |
"location", |
| 28 |
"name", |
| 29 |
"online", |
| 30 |
"photo_id", |
| 31 |
"pic1024x768", # big |
| 32 |
"pic190x190", # small |
| 33 |
"pic640x480", # medium |
| 34 |
"pic_1", # aka pic50x50 |
| 35 |
"pic_2", # aka pic128max |
| 36 |
"uid", |
| 37 |
] |
| 38 |
|
| 39 |
|
| 40 |
class OdnoklassnikiOAuth2Adapter(OAuth2Adapter): |
| 41 |
provider_id = "odnoklassniki" |
| 42 |
access_token_url = "https://api.odnoklassniki.ru/oauth/token.do" # nosec |
| 43 |
authorize_url = "https://www.odnoklassniki.ru/oauth/authorize" |
| 44 |
profile_url = "https://api.odnoklassniki.ru/fb.do" |
| 45 |
access_token_method = "POST" # nosec |
| 46 |
|
| 47 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 48 |
data = { |
| 49 |
"method": "users.getCurrentUser", |
| 50 |
"access_token": token.token, |
| 51 |
"fields": ",".join(USER_FIELDS), |
| 52 |
"format": "JSON", |
| 53 |
"application_key": app.key, |
| 54 |
} |
| 55 |
# Ondoklassniki prescribes a weak algo |
| 56 |
suffix = md5( |
| 57 |
f"{data['access_token']:s}{app.secret:s}".encode() |
| 58 |
).hexdigest() # nosec |
| 59 |
check_list = sorted( |
| 60 |
[f"{k:s}={v:s}" for k, v in data.items() if k != "access_token"] |
| 61 |
) |
| 62 |
data["sig"] = md5( |
| 63 |
("".join(check_list) + suffix).encode("utf-8") |
| 64 |
).hexdigest() # nosec |
| 65 |
|
| 66 |
with get_adapter().get_requests_session() as sess: |
| 67 |
response = sess.get(self.profile_url, params=data) |
| 68 |
extra_data = response.json() |
| 69 |
return self.get_provider().sociallogin_from_response(request, extra_data) |
| 70 |
|
| 71 |
|
| 72 |
oauth2_login = OAuth2LoginView.adapter_view(OdnoklassnikiOAuth2Adapter) |
| 73 |
oauth2_callback = OAuth2CallbackView.adapter_view(OdnoklassnikiOAuth2Adapter) |