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 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) (limited to 'accounts') 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 = {} -- cgit v1.2.1