From 6e25e279e2327c73365c23a5f120b5a3b1dec8d6 Mon Sep 17 00:00:00 2001 From: Francis Date: Wed, 22 Jul 2020 04:32:25 +0200 Subject: [PATCH] use settings --- KeRS/settings.py | 13 +++++++++++++ oauth/views.py | 27 ++++++++++++--------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/KeRS/settings.py b/KeRS/settings.py index 9157f12..a1fbc89 100644 --- a/KeRS/settings.py +++ b/KeRS/settings.py @@ -127,4 +127,17 @@ STATIC_URL = '/static/' # Custom stuff +SERVER_URL = 'http://localhost:8000' + AUTH_USER_MODEL = 'users.CustomUser' + +_BASE_OAUTH_URL = 'https://adams.ugent.be/oauth' + +OAUTH = { + 'USER_API_URI': f'{_BASE_OAUTH_URL}/api/current_user/', + 'ACCESS_TOKEN_URI': f'{_BASE_OAUTH_URL}/oauth2/token/', + 'AUTHORIZE_URI': f'{_BASE_OAUTH_URL}/oauth2/authorize/', + 'REDIRECT_URI': f'{SERVER_URL}/login/zeus/authorized', + 'CLIENT_ID': 'tomtest', + 'CLIENT_SECRET': 'blargh', +} \ No newline at end of file diff --git a/oauth/views.py b/oauth/views.py index b380a03..3fc547e 100644 --- a/oauth/views.py +++ b/oauth/views.py @@ -1,19 +1,13 @@ import logging import requests +from django.conf import settings from django.contrib.auth import login from django.http.request import HttpRequest from django.shortcuts import redirect from users.models import CustomUser -USER_API_URI = 'https://adams.ugent.be/oauth/api/current_user/' -ACCESS_TOKEN_URI = 'https://adams.ugent.be/oauth/oauth2/token/' -AUTHORIZE_URI = 'https://adams.ugent.be/oauth/oauth2/authorize/' - -CLIENT_ID = 'tomtest' -CLIENT_SECRET = 'blargh' - logger = logging.getLogger(__file__) @@ -23,17 +17,20 @@ class OAuthException(Exception): def register(_): RESPONSE_TYPE = 'code' - REDIRECT_URI = 'http://localhost:8000/login/zeus/authorized' - return redirect(f'{AUTHORIZE_URI}?response_type={RESPONSE_TYPE}&client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}') + return redirect(f'{settings.OAUTH["AUTHORIZE_URI"]}?' + f'response_type={RESPONSE_TYPE}&' + f'client_id={settings.OAUTH["CLIENT_ID"]}&' + f'redirect_uri={settings.OAUTH["REDIRECT_URI"]}') def register_callback(req: HttpRequest): code = req.GET['code'] - response = requests.post(ACCESS_TOKEN_URI, data={'code': code, - 'grant_type': 'authorization_code', - 'client_id': CLIENT_ID, - 'client_secret': CLIENT_SECRET, - 'redirect_uri': 'http://localhost:8000/login/zeus/authorized'}) + response = requests.post(settings.OAUTH["AUTHORIZE_URI"], + data={'code': code, + 'grant_type': 'authorization_code', + 'client_id': settings.OAUTH["CLIENT_ID"], + 'client_secret': settings.OAUTH["CLIENT_SECRET"], + 'redirect_uri': settings.OAUTH["REDIRECT_URI"]}) try: if response.status_code == 200: json: dict = response.json() @@ -64,5 +61,5 @@ def validate_user(zeus_id, username) -> CustomUser: def user_info(access_token): - r = requests.get(USER_API_URI, headers={'Authorization': f'Bearer {access_token}'}) + r = requests.get(settings.OAUTH["USER_API_URI"], headers={'Authorization': f'Bearer {access_token}'}) return r.json()