Skip to content

Commit

Permalink
Update connector.py
Browse files Browse the repository at this point in the history
  • Loading branch information
untitledds authored Dec 1, 2024
1 parent 019eb3c commit 7a20f36
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions back/taiga_contrib_access_token_auth/connector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import requests
import logging
import os
from django.conf import settings
from taiga.base.connectors.exceptions import ConnectorBaseException

Expand All @@ -13,28 +12,38 @@ def get_user_info(access_token):
:param access_token: Токен доступа от OIDC-провайдера.
:returns: Информация о пользователе.
"""
userinfo_url = os.getenv("OIDC_USERINFO_ENDPOINT")
userinfo_url = settings.OIDC_USERINFO_ENDPOINT

userinfo_response = requests.get(
userinfo_url,
headers={'Authorization': f'Bearer {access_token}'}
)

if userinfo_response.status_code != 200:
logger.error(f"Failed to obtain user info: {userinfo_response.text}")
try:
userinfo_response = requests.get(
userinfo_url,
headers={'Authorization': f'Bearer {access_token}'}
)
userinfo_response.raise_for_status() # Выбрасывает исключение, если статус код не 200
except requests.exceptions.RequestException as e:
logger.error(f"Failed to obtain user info: {e}")
raise ConnectorBaseException({
"error_message": "Failed to obtain user info",
"status_code": userinfo_response.status_code,
"response_text": userinfo_response.text
"details": str(e)
})

user_info = userinfo_response.json()

# Проверка обязательных полей
required_fields = ['sub', 'preferred_username', 'email', 'name']
for field in required_fields:
if field not in user_info:
logger.error(f"Missing required field '{field}' in user info response")
raise ConnectorBaseException({
"error_message": f"Missing required field '{field}' in user info response",
"response_text": user_info
})

# Возвращаем информацию о пользователе
return {
'guid': user_info.get('sub', None),
'username': user_info.get('preferred_username', None),
'email': user_info.get('email', None),
'full_name': user_info.get('name', None),
'guid': user_info['sub'],
'username': user_info['preferred_username'],
'email': user_info['email'],
'full_name': user_info['name'],
'groups': user_info.get('groups', []),
}

0 comments on commit 7a20f36

Please sign in to comment.