diff --git a/Website/skoolos/settings.py b/Website/skoolos/settings.py index b099675..c312991 100644 --- a/Website/skoolos/settings.py +++ b/Website/skoolos/settings.py @@ -135,4 +135,4 @@ STATIC_URL = '/static/' CRISPY_TEMPLATE_PACK = 'bootstrap4' -LOGIN_REDIRECT_URL = '/' \ No newline at end of file +LOGIN_REDIRECT_URL = '/' diff --git a/Website/skoolos/urls.py b/Website/skoolos/urls.py index 1ffab10..00553b1 100644 --- a/Website/skoolos/urls.py +++ b/Website/skoolos/urls.py @@ -19,5 +19,8 @@ urlpatterns = [ path('api/', include(router.urls)), path('api-auth/', include('rest_framework.urls')), path('admin/', admin.site.urls), - path('login/', auth_views.LoginView.as_view(template_name="users/login.html"), name='login') -] \ No newline at end of file + path('login/', auth_views.LoginView.as_view(template_name="users/login.html"), name='login'), + path('register/', user_views.register, name='register'), + path('create_account/', user_views.create_account, name='create_account'), + path('callback/', user_views.callback, name='callback'), +] diff --git a/Website/skoolsite/urls.py b/Website/skoolsite/urls.py deleted file mode 100644 index e69de29..0000000 diff --git a/Website/users/admin.py b/Website/users/admin.py index 8c38f3f..73093bd 100644 --- a/Website/users/admin.py +++ b/Website/users/admin.py @@ -1,3 +1,5 @@ from django.contrib import admin +from .models import Token # Register your models here. +admin.site.register(Token) diff --git a/Website/users/forms.py b/Website/users/forms.py new file mode 100644 index 0000000..956b95d --- /dev/null +++ b/Website/users/forms.py @@ -0,0 +1,19 @@ +from django import forms +from django.contrib.auth.models import User + +class UserCreationForm(forms.ModelForm): + + username = forms.CharField(disabled=True) + email = forms.EmailField(disabled=True) + first_name = forms.CharField(disabled=True) + last_name = forms.CharField(disabled=True) + password = forms.PasswordInput() + confirm_password = forms.PasswordInput() + + + def __init__(self, *args, **kwargs): + super(UserCreationForm, self).__init__(*args, **kwargs) + + class Meta: + model = User + fields = ['username', 'email', 'first_name', 'password', 'confirm_password'] diff --git a/Website/users/migrations/0001_initial.py b/Website/users/migrations/0001_initial.py new file mode 100644 index 0000000..86549e5 --- /dev/null +++ b/Website/users/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# Generated by Django 3.0.7 on 2020-06-13 08:25 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Token', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('username', models.TextField()), + ('email', models.TextField()), + ('first_name', models.TextField()), + ('last_name', models.TextField()), + ('isStudent', models.BooleanField()), + ('token', models.CharField(max_length=255)), + ], + ), + ] diff --git a/Website/users/models.py b/Website/users/models.py index 71a8362..bed1e0f 100644 --- a/Website/users/models.py +++ b/Website/users/models.py @@ -1,3 +1,21 @@ from django.db import models +from uuid import uuid4 + # Create your models here. + +class Token(models.Model): + username = models.TextField() + email = models.TextField() + first_name = models.TextField() + last_name = models.TextField() + isStudent = models.BooleanField() + token = models.CharField(max_length=255) + + def save(self, *args, **kwargs): + if not self.token: + self.token = uuid4() + return super(Token, self).save(*args, **kwargs) + + def __str__(self): + return f"{self.username}'s Token"; diff --git a/Website/users/templates/users/create_password.html b/Website/users/templates/users/create_password.html new file mode 100644 index 0000000..6018ee3 --- /dev/null +++ b/Website/users/templates/users/create_password.html @@ -0,0 +1,13 @@ +{% extends "users/base.html" %} +{% block content %} +
+
+
+ + + + +
+
+
+{% endblock %} diff --git a/Website/users/templates/users/login.html b/Website/users/templates/users/login.html index 9f71406..40837e2 100644 --- a/Website/users/templates/users/login.html +++ b/Website/users/templates/users/login.html @@ -8,9 +8,9 @@ {% csrf_token %} {{ form | crispy }} -

Not registered? Create an account with Ionreg

+

Not registered? Create an account with Ion

-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/Website/users/templates/users/register.html b/Website/users/templates/users/register.html index aefb3cb..d7bac8d 100644 --- a/Website/users/templates/users/register.html +++ b/Website/users/templates/users/register.html @@ -3,13 +3,13 @@ {% block content %}
- +
-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/Website/users/views.py b/Website/users/views.py index 91ea44a..c8bf1af 100644 --- a/Website/users/views.py +++ b/Website/users/views.py @@ -1,3 +1,80 @@ -from django.shortcuts import render +import json +import requests + +from django.shortcuts import render, redirect + +from requests_oauthlib import OAuth2Session +from django.contrib import messages + +from .models import Token + +from django.contrib.auth import authenticate +from django.contrib.auth import login as auth_login +from django.contrib.auth import logout as auth_logout +from django.contrib.auth.models import User +from django.contrib.auth.decorators import login_required # Create your views here. +# Thanks Django, what would I do without this comment + +client_id = r'QeZPBSKqdvWFfBv1VYTSv9iFGz5T9pVJtNUjbEr6' +client_secret = r'0Wl3hAIGY9SvYOqTOLUiLNYa4OlCgZYdno9ZbcgCT7RGQ8x2f1l2HzZHsQ7ijC74A0mrOhhCVeZugqAmOADHIv5fHxaa7GqFNtQr11HX9ySTw3DscKsphCVi5P71mlGY' +redirect_uri = 'http://localhost:8000/callback/' +token_url = 'https://ion.tjhsst.edu/oauth/authorize/' +scope=["read"] + +def register(request): + oauth = OAuth2Session(client_id=client_id, redirect_uri=redirect_uri, scope=scope) + authorization_url, state = oauth.authorization_url("https://ion.tjhsst.edu/oauth/authorize/") + + return render(request,"users/register.html", {"authorization_url": authorization_url}) + +def callback (request): + if request.method == "GET": + code = request.GET.get('code') + state = request.GET.get("state") + # Then if we get a response from Ion with the authorization code + if code is not None and state is not None: + print ("made it") + # We send it back to fetch the acess_token + payload = {'grant_type':'authorization_code','code': code,'redirect_uri':redirect_uri,'client_id':client_id,'client_secret':client_secret, 'csrfmiddlewaretoken': state} + token = requests.post("https://ion.tjhsst.edu/oauth/token/", data=payload).json() + headers = {'Authorization': f"Bearer {token['access_token']}"} + print(token) + + # And finally get the user's profile! + profile = requests.get("https://ion.tjhsst.edu/api/profile", headers=headers).json() + print(profile) + username = profile['ion_username'] + email = profile['tj_email'] + first_name = profile['first_name'] + last_name = profile['last_name'] + isStudent = profile['is_student'] + + if User.objects.filter(username=username).count() != 0: + messages.success(request, "This user already exists!") + return redirect('register') + else: + token = Token(username = username, email = email, first_name = first_name, last_name = last_name, isStudent = isStudent) + token.save() + tokenHash = token.token + print(f'/create_account/?token={tokenHash}') + return redirect(f'/create_account/?token={tokenHash}') + + + messages.warning(request, "Invalid Callback Response") + return redirect('register') + + +def create_account (request): + if request.method == "GET" and Token.objects.filter(token=request.GET.get('token')).count() == 1: + token = Token.objects.get(token=request.GET.get('token')) + username = token.username + email = token.email + first_name = token.first_name + last_name = token.last_name + isStudent = token.isStudent + + + else: + return redirect('/register/')