diff --git a/Platform/settings.py b/Platform/settings.py index 10730fa..e50ed33 100644 --- a/Platform/settings.py +++ b/Platform/settings.py @@ -147,3 +147,4 @@ MINIO_SECRET_KEY = os.getenv("MINIO_SECRET_KEY", "minioadmin") MINIO_BUCKET_NAME = 'platform' VK_SERVICE_TOKEN = os.getenv("VK_SERVICE_TOKEN", None) +YANDEX_SERVICE_TOKEN = os.getenv("YANDEX_SERVICE_TOKEN", None) diff --git a/templates/profile.html b/templates/profile.html index 579bdf0..90a76cb 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -56,6 +56,13 @@
{% endif %} +
+ + + {% if account.yandex_id %} +
+ {% endif %} + {% endif %} diff --git a/templates/welcome.html b/templates/welcome.html index 75618f8..332ed90 100644 --- a/templates/welcome.html +++ b/templates/welcome.html @@ -20,7 +20,7 @@ или войти с помощью

- + diff --git a/web/urls.py b/web/urls.py index 4c75ae1..33b8967 100644 --- a/web/urls.py +++ b/web/urls.py @@ -11,5 +11,6 @@ urlpatterns = [ path(*ProfilePhoto.as_path()), path(*LogoutView.as_path()), path(*PingView.as_path()), - path(*VKAuthView.as_path()) + path(*VKAuthView.as_path()), + path(*YandexAuthView.as_path()) ] diff --git a/web/views/__init__.py b/web/views/__init__.py index 588a3df..0368782 100644 --- a/web/views/__init__.py +++ b/web/views/__init__.py @@ -5,4 +5,5 @@ from .profile import ProfileView from .profile_photo import ProfilePhoto from .logout import LogoutView from .ping import PingView -from .vk_auth import VKAuthView \ No newline at end of file +from .vk_auth import VKAuthView +from .yandex_auth import YandexAuthView diff --git a/web/views/yandex_auth.py b/web/views/yandex_auth.py new file mode 100644 index 0000000..cb2a7a2 --- /dev/null +++ b/web/views/yandex_auth.py @@ -0,0 +1,43 @@ +from django.contrib.auth import login +from requests import post, get + +from BaseLib.BaseView import BaseView +from Platform import settings +from web.models import CustomUser + + +class YandexAuthView(BaseView): + required_login = False + endpoint = "yandex_auth" + + def get(self): + code = self.request.GET['code'] + response = post('https://oauth.yandex.ru/token', data={ + 'client_id': '38f1906e99de4810bd79828f420ba885', + 'client_secret': settings.YANDEX_SERVICE_TOKEN, + 'grant_type': 'authorization_code', + 'code': code + }) + if response.status_code != 200: + print("Cant access, json: ", response.json()) + return '/welcome' + access_token = response.json().get('access_token') + if access_token is None: + print('no access token') + return '/welcome' + info_response = get('https://login.yandex.ru/info', headers={ + 'Authorization': f'OAuth {access_token}' + }) + if info_response.status_code != 200: + print("Cant access, json: ", response.json()) + return '/welcome' + data = info_response.json() + yandex_id = data['id'] + if self.request.user.is_authenticated: + self.request.user.yandex_id = yandex_id + self.request.user.save() + else: + user = CustomUser.objects.filter(yandex_id=yandex_id).first() + if user is not None: + login(self.request, user) + return '/profile'