use settings

This commit is contained in:
Francis 2020-07-22 04:32:25 +02:00
parent 704dfee977
commit 6e25e279e2
No known key found for this signature in database
GPG key ID: 071BEA4C2B10077C
2 changed files with 25 additions and 15 deletions

View file

@ -127,4 +127,17 @@ STATIC_URL = '/static/'
# Custom stuff # Custom stuff
SERVER_URL = 'http://localhost:8000'
AUTH_USER_MODEL = 'users.CustomUser' 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',
}

View file

@ -1,19 +1,13 @@
import logging import logging
import requests import requests
from django.conf import settings
from django.contrib.auth import login from django.contrib.auth import login
from django.http.request import HttpRequest from django.http.request import HttpRequest
from django.shortcuts import redirect from django.shortcuts import redirect
from users.models import CustomUser 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__) logger = logging.getLogger(__file__)
@ -23,17 +17,20 @@ class OAuthException(Exception):
def register(_): def register(_):
RESPONSE_TYPE = 'code' RESPONSE_TYPE = 'code'
REDIRECT_URI = 'http://localhost:8000/login/zeus/authorized' return redirect(f'{settings.OAUTH["AUTHORIZE_URI"]}?'
return redirect(f'{AUTHORIZE_URI}?response_type={RESPONSE_TYPE}&client_id={CLIENT_ID}&redirect_uri={REDIRECT_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): def register_callback(req: HttpRequest):
code = req.GET['code'] code = req.GET['code']
response = requests.post(ACCESS_TOKEN_URI, data={'code': code, response = requests.post(settings.OAUTH["AUTHORIZE_URI"],
data={'code': code,
'grant_type': 'authorization_code', 'grant_type': 'authorization_code',
'client_id': CLIENT_ID, 'client_id': settings.OAUTH["CLIENT_ID"],
'client_secret': CLIENT_SECRET, 'client_secret': settings.OAUTH["CLIENT_SECRET"],
'redirect_uri': 'http://localhost:8000/login/zeus/authorized'}) 'redirect_uri': settings.OAUTH["REDIRECT_URI"]})
try: try:
if response.status_code == 200: if response.status_code == 200:
json: dict = response.json() json: dict = response.json()
@ -64,5 +61,5 @@ def validate_user(zeus_id, username) -> CustomUser:
def user_info(access_token): 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() return r.json()