This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class MailRuAccount(ProviderAccount):
def get_profile_url(self):
return self.account.extra_data.get("link")
def get_avatar_url(self):
ret = None
if self.account.extra_data.get("has_pic"):
pic_big_url = self.account.extra_data.get("pic_big")
pic_small_url = self.account.extra_data.get("pic_small")
if pic_big_url:
return pic_big_url
elif pic_small_url:
return pic_small_url
else:
return ret
def to_str(self):
dflt = super(MailRuAccount, self).to_str()
return self.account.extra_data.get("name", dflt)
class MailRuProvider(OAuth2Provider):
id = "mailru"
name = "Mail.RU"
account_class = MailRuAccount
def extract_uid(self, data):
return data["uid"]
def extract_common_fields(self, data):
return dict(
email=data.get("email"),
last_name=data.get("last_name"),
username=data.get("nick"),
first_name=data.get("first_name"),
)
provider_classes = [MailRuProvider]

View File

@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from .provider import MailRuProvider
class MailRuTests(OAuth2TestsMixin, TestCase):
provider_id = MailRuProvider.id
def get_mocked_response(self, verified_email=True):
return MockedResponse(
200,
"""
[ { "uid": "15410773191172635989", "first_name": "Евгений", "last_name": "Маслов", "nick": "maslov", "email": "emaslov@mail.ru", "sex": 0, "birthday": "15.02.1980", "has_pic": 1, "pic": "http://avt.appsmail.ru/mail/emaslov/_avatar", "pic_small": "http://avt.appsmail.ru/mail/emaslov/_avatarsmall", "pic_big": "http://avt.appsmail.ru/mail/emaslov/_avatarbig", "link": "http://my.mail.ru/mail/emaslov/", "referer_type": "", "referer_id": "", "is_online": 1, "friends_count": 145, "is_verified": 1, "vip" : 0, "app_installed": 1, "location": { "country": { "name": "Россия", "id": "24" }, "city": { "name": "Москва", "id": "25" }, "region": { "name": "Москва", "id": "999999" } } }]""",
) # noqa
def get_login_response_json(self, with_refresh_token=True):
# FIXME: This is not an actual response. I added this in order
# to get the test suite going but did not verify to check the
# exact response being returned.
return '{"access_token": "testac", "uid": "weibo", "refresh_token": "testrf", "x_mailru_vid": "1"}' # noqa

View File

@@ -0,0 +1,6 @@
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
from .provider import MailRuProvider
urlpatterns = default_urlpatterns(MailRuProvider)

View File

@@ -0,0 +1,37 @@
import requests
from hashlib import md5
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import MailRuProvider
class MailRuOAuth2Adapter(OAuth2Adapter):
provider_id = MailRuProvider.id
access_token_url = "https://connect.mail.ru/oauth/token"
authorize_url = "https://connect.mail.ru/oauth/authorize"
profile_url = "http://www.appsmail.ru/platform/api"
def complete_login(self, request, app, token, **kwargs):
uid = kwargs["response"]["x_mailru_vid"]
data = {
"method": "users.getInfo",
"app_id": app.client_id,
"secure": "1",
"uids": uid,
}
param_list = sorted([item + "=" + data[item] for item in data])
data["sig"] = md5(
("".join(param_list) + app.secret).encode("utf-8")
).hexdigest()
response = requests.get(self.profile_url, params=data)
extra_data = response.json()[0]
return self.get_provider().sociallogin_from_response(request, extra_data)
oauth2_login = OAuth2LoginView.adapter_view(MailRuOAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(MailRuOAuth2Adapter)