Add logging
This commit is contained in:
parent
0d371aed13
commit
7ffeda0ca3
|
|
@ -3,14 +3,14 @@ from django.conf import settings
|
||||||
from django.utils.dateparse import parse_datetime
|
from django.utils.dateparse import parse_datetime
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from oauth.models import OAuth2Token
|
from oauth.models import OAuth2Token
|
||||||
from oauth.oauth_client import oauth, fetch_token
|
from oauth.oauth_client import oauth, fetch_token
|
||||||
from users.licenses import MYSKILLBOX_LICENSES, is_myskillbox_product, TEACHER_KEY
|
from users.licenses import MYSKILLBOX_LICENSES, is_myskillbox_product, TEACHER_KEY
|
||||||
from users.models import License
|
from users.models import License
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
from core.logger import get_logger
|
||||||
|
|
||||||
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
class HepClientException(Exception):
|
class HepClientException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
@ -41,6 +41,7 @@ class HepClient:
|
||||||
if response.status_code == 401:
|
if response.status_code == 401:
|
||||||
raise HepClientUnauthorizedException(response.status_code, response.json())
|
raise HepClientUnauthorizedException(response.status_code, response.json())
|
||||||
elif response.status_code != 200:
|
elif response.status_code != 200:
|
||||||
|
logger.warning(f'Hepclient error: Received {response.status_code} {response.json()}')
|
||||||
raise HepClientException(response.status_code, response.json())
|
raise HepClientException(response.status_code, response.json())
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,9 @@ from django.utils import timezone
|
||||||
|
|
||||||
from oauth.managers import OAuth2TokenManager
|
from oauth.managers import OAuth2TokenManager
|
||||||
|
|
||||||
|
from core.logger import get_logger
|
||||||
|
|
||||||
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
class OAuth2Token(models.Model):
|
class OAuth2Token(models.Model):
|
||||||
token_type = models.CharField(max_length=40)
|
token_type = models.CharField(max_length=40)
|
||||||
|
|
@ -68,6 +71,7 @@ class OAuth2Token(models.Model):
|
||||||
try:
|
try:
|
||||||
payload_bytes = base64.b64decode(jwt_parts[1])
|
payload_bytes = base64.b64decode(jwt_parts[1])
|
||||||
payload = json.loads(payload_bytes.decode("UTF-8"))
|
payload = json.loads(payload_bytes.decode("UTF-8"))
|
||||||
except:
|
except Exception as e:
|
||||||
|
logger.warning(f'OAuthToken error: Could not decode jwt: {e}')
|
||||||
return None
|
return None
|
||||||
return payload
|
return payload
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
from authlib.integrations.base_client import OAuthError
|
from authlib.integrations.base_client import OAuthError
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
|
from sentry_sdk import capture_exception
|
||||||
|
|
||||||
from oauth.hep_client import HepClient
|
from oauth.hep_client import HepClient
|
||||||
from oauth.oauth_client import oauth
|
from oauth.oauth_client import oauth
|
||||||
|
|
@ -8,8 +9,13 @@ from oauth.models import OAuth2Token
|
||||||
from oauth.user_signup_login_handler import handle_user_and_verify_products, EMAIL_NOT_VERIFIED, UNKNOWN_ERROR
|
from oauth.user_signup_login_handler import handle_user_and_verify_products, EMAIL_NOT_VERIFIED, UNKNOWN_ERROR
|
||||||
from django.contrib.auth import login as dj_login
|
from django.contrib.auth import login as dj_login
|
||||||
|
|
||||||
|
from core.logger import get_logger
|
||||||
|
|
||||||
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
OAUTH_REDIRECT = 'oauth-redirect'
|
OAUTH_REDIRECT = 'oauth-redirect'
|
||||||
|
|
||||||
|
|
||||||
def login(request):
|
def login(request):
|
||||||
hep_oauth_client = oauth.create_client('hep')
|
hep_oauth_client = oauth.create_client('hep')
|
||||||
redirect_uri = settings.OAUTH_LOCAL_REDIRECT_URI
|
redirect_uri = settings.OAUTH_LOCAL_REDIRECT_URI
|
||||||
|
|
@ -25,9 +31,9 @@ def authorize(request):
|
||||||
user, status_msg = handle_user_and_verify_products(user_data, token)
|
user, status_msg = handle_user_and_verify_products(user_data, token)
|
||||||
user.sync_with_hep_data(user_data)
|
user.sync_with_hep_data(user_data)
|
||||||
except OAuthError as e:
|
except OAuthError as e:
|
||||||
# logger
|
logger.warning(f'OAuth error: {e}')
|
||||||
# sentry event
|
if not settings.DEBUG:
|
||||||
# rename
|
capture_exception(e)
|
||||||
return redirect(f'/{OAUTH_REDIRECT}?state={UNKNOWN_ERROR}')
|
return redirect(f'/{OAUTH_REDIRECT}?state={UNKNOWN_ERROR}')
|
||||||
|
|
||||||
if user and status_msg != EMAIL_NOT_VERIFIED:
|
if user and status_msg != EMAIL_NOT_VERIFIED:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue