yandex auth

This commit is contained in:
Administrator 2023-09-24 22:31:37 +03:00
parent 125eaf7f58
commit 073e52e09e
6 changed files with 56 additions and 3 deletions

View File

@ -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)

View File

@ -56,6 +56,13 @@
<div style="margin-top: -15px; margin-left: 30px;"><i style="color: green;" class="fa fa-check-circle"></i></div>
{% endif %}
</td>
<td><div style="width: 20px;"></div></td>
<td>
<a href="https://oauth.yandex.ru/authorize?response_type=code&client_id=c66de1919a9d4211b5129362020e6f27&redirect_uri=https://platform.sprinthub.ru/yandex_auth"><img style="width: 40px; height: 40px;" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/Yandex_icon.svg/2048px-Yandex_icon.svg.png"></a>
{% if account.yandex_id %}
<div style="margin-top: -15px; margin-left: 30px;"><i style="color: green;" class="fa fa-check-circle"></i></div>
{% endif %}
</td>
</tr>
{% endif %}
<tr>

View File

@ -20,7 +20,7 @@
</div>
или войти с помощью<br><br>
<a href="https://oauth.vk.com/authorize?client_id=8123759&redirect_uri=https://platform.sprinthub.ru/vk_auth&display=page&response_type=token&v=5.59"><img style="width: 40px; height: 40px;" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/VK_Compact_Logo_%282021-present%29.svg/1200px-VK_Compact_Logo_%282021-present%29.svg.png"></a>
<a href=#""><img style="width: 40px; height: 40px; margin-left: 10px;" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/Yandex_icon.svg/2048px-Yandex_icon.svg.png"></a>
<a href="https://oauth.yandex.ru/authorize?response_type=code&client_id=c66de1919a9d4211b5129362020e6f27&redirect_uri=https://platform.sprinthub.ru/yandex_auth"><img style="width: 40px; height: 40px; margin-left: 10px;" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/Yandex_icon.svg/2048px-Yandex_icon.svg.png"></a>
</div>
</center>
</div>

View File

@ -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())
]

View File

@ -6,3 +6,4 @@ from .profile_photo import ProfilePhoto
from .logout import LogoutView
from .ping import PingView
from .vk_auth import VKAuthView
from .yandex_auth import YandexAuthView

43
web/views/yandex_auth.py Normal file
View File

@ -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'