From 655957935a2c2daec3f5f1640754915d1bf26d6f Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Sun, 2 Jan 2022 18:31:34 +0100 Subject: pof of oauth2 --- accounts/urls.py | 2 ++ accounts/views.py | 26 +++++++++++++++++++++++++- manager/settings.py | 14 ++++++++++++++ requirements.txt | 1 + templates/registration/login.html | 1 + version.txt | 2 +- 6 files changed, 44 insertions(+), 2 deletions(-) diff --git a/accounts/urls.py b/accounts/urls.py index 6a44457..3dc7d18 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -6,4 +6,6 @@ urlpatterns = [ path('signup/', views.signup, name='signup'), path('personal/', views.personal, name='personal'), path('terms/', views.terms, name='terms'), + path('oauth_login/', views.oauth_login, name='oauth_login'), + path('auth/', views.auth, name='auth'), ] diff --git a/accounts/views.py b/accounts/views.py index c73ecfd..ef2d6c1 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -1,4 +1,7 @@ from django.shortcuts import render +from django.urls import reverse +from django.conf import settings +from django.contrib.auth.views import LoginView from django.contrib.auth.forms import UserCreationForm from django.contrib.auth import login, authenticate from django.http import FileResponse @@ -8,8 +11,11 @@ from django.http import HttpResponseRedirect from client.models import UserCode +from authlib.integrations.django_client import OAuth + import dateparser import os +import requests from io import BytesIO from PIL import Image, UnidentifiedImageError from pdf2image import convert_from_bytes @@ -18,6 +24,9 @@ from pdf2image.exceptions import ( PDFSyntaxError ) +oauth = OAuth() +hitobito = oauth.register(name="hitobito") + # override to remove help text class RegisterForm(UserCreationForm): def __init__(self, *args, **kwargs): @@ -26,6 +35,22 @@ class RegisterForm(UserCreationForm): for fieldname in ['username', 'password1', 'password2']: self.fields[fieldname].help_text = None +def oauth_login(request): + redirect_uri = request.build_absolute_uri(reverse('auth')) + return hitobito.authorize_redirect(request, redirect_uri) + +def auth(request): + token = hitobito.authorize_access_token(request) + print(token) + headers = { + "Authorization" : "Bearer " + token["access_token"], + "X-Scope": "with_roles", + } + resp = requests.get("https://demo.hitobito.com/oauth/profile", headers=headers) + print(resp) + print(resp.text) + return HttpResponseRedirect('/') + @sensitive_variables("raw_passsword") def signup(request): out_errors = [] @@ -76,7 +101,6 @@ def signup(request): } return render(request, 'accounts/signup.html', context) - @login_required def personal(request): context = {} diff --git a/manager/settings.py b/manager/settings.py index fae2385..358b0b7 100644 --- a/manager/settings.py +++ b/manager/settings.py @@ -57,6 +57,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + "django_extensions", ] MIDDLEWARE = [ @@ -69,6 +70,19 @@ MIDDLEWARE = [ 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] +AUTHLIB_OAUTH_CLIENTS = { + 'hitobito': { + 'client_id': 'uZOLGZDioF0FBm5FlkSYkiCJ0nsNErZmLgCDMbWiHYY', + 'client_secret': '-Vv4El7-UmiSoET_tvgqeNnJzrSN_76b9I_zKFZFKpo', + 'access_token_url': 'https://demo.hitobito.com/oauth/token', + 'access_token_params': None, + 'refresh_token_url': None, + 'authorize_url': 'https://demo.hitobito.com/oauth/authorize', + 'authorize_params': None, + 'client_kwargs': {"grant_type": "authorization_code", "scope": "with_roles"}, + } +} + if DEBUG: INSTALLED_APPS.append('debug_toolbar') MIDDLEWARE.append('debug_toolbar.middleware.DebugToolbarMiddleware') diff --git a/requirements.txt b/requirements.txt index 7682c40..e442da7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ dateparser pytz pdf2image django-debug-toolbar +requests diff --git a/templates/registration/login.html b/templates/registration/login.html index 022309e..5a24448 100644 --- a/templates/registration/login.html +++ b/templates/registration/login.html @@ -14,6 +14,7 @@ Password dimenticata

+ OAuth diff --git a/version.txt b/version.txt index b5aac86..571dffe 100644 --- a/version.txt +++ b/version.txt @@ -1,2 +1,2 @@ version=0.4 -rev=4 +rev=5 -- cgit v1.2.1