42OAuth #41

Merged
Danhia merged 6 commits from 42OAuth into main 2022-03-29 21:03:48 +02:00
4 changed files with 41 additions and 20 deletions
Showing only changes of commit 21f38906df - Show all commits

View File

@ -7,6 +7,6 @@ from django.contrib import admin
@admin.register(UserProfileInfo) @admin.register(UserProfileInfo)
class userprofile(admin.ModelAdmin): class userprofile(admin.ModelAdmin):
#list display #list display
list_display = ['user', 'score', 'last_submission_date'] list_display = ['user', 'score', 'last_submission_date', 'intra42_campus']
# search list # search list
search_fields = ['score', 'user__username'] search_fields = ['score', 'user__username', 'intra42_campus']

View File

@ -0,0 +1,22 @@
# Generated by Django 3.2.11 on 2022-03-29 11:39
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0008_auto_20220329_1034'),
]
operations = [
migrations.AlterModelOptions(
name='userprofileinfo',
options={'ordering': ['-score', 'last_submission_date', 'user__username', 'intra42_campus'], 'permissions': (('view_info', 'View user info'),), 'verbose_name': 'profile', 'verbose_name_plural': 'profiles'},
),
migrations.AddField(
model_name='userprofileinfo',
name='intra42_campus_id',
field=models.CharField(blank=True, max_length=10, null=True),
),
]

View File

@ -14,13 +14,14 @@ class UserProfileInfo(models.Model):
discord_id = models.CharField(max_length=20, null=True, blank=True, unique=True) discord_id = models.CharField(max_length=20, null=True, blank=True, unique=True)
intra42_id = models.CharField(max_length=20, null=True, blank=True, unique=True) intra42_id = models.CharField(max_length=20, null=True, blank=True, unique=True)
intra42_campus = models.CharField(max_length=50, null=True, blank=True) intra42_campus = models.CharField(max_length=50, null=True, blank=True)
intra42_campus_id = models.CharField(max_length=10, null=True, blank=True)
member = models.BooleanField(default=False) member = models.BooleanField(default=False)
member_since = models.DateTimeField('Member since', default=timezone.now) member_since = models.DateTimeField('Member since', default=timezone.now)
member_until = models.DateTimeField('Member until', default=timezone.now) member_until = models.DateTimeField('Member until', default=timezone.now)
def __str__(self): def __str__(self):
return self.user.username return self.user.username
class Meta: class Meta:
ordering = ['-score', 'last_submission_date', 'user__username'] ordering = ['-score', 'last_submission_date', 'user__username', 'intra42_campus']
verbose_name = 'profile' verbose_name = 'profile'
verbose_name_plural = 'profiles' verbose_name_plural = 'profiles'
permissions = (("view_info", "View user info"),) permissions = (("view_info", "View user info"),)

View File

@ -7,12 +7,9 @@ from django.shortcuts import redirect
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
import os import os
from local_settings import API42_SECRET, API42_UID oauth = OAuth()
oauth_discord = OAuth() oauth.register(
oauth_intra42 = OAuth()
oauth_discord.register(
name='discord', name='discord',
client_id=os.getenv('OAUTH2_DISCORD_CLIENT_ID'), client_id=os.getenv('OAUTH2_DISCORD_CLIENT_ID'),
client_secret=os.getenv('OAUTH2_DISCORD_CLIENT_SECRET'), client_secret=os.getenv('OAUTH2_DISCORD_CLIENT_SECRET'),
@ -22,13 +19,12 @@ oauth_discord.register(
api_base_url='https://discord.com/api/' api_base_url='https://discord.com/api/'
) )
oauth_intra42.register( oauth.register(
name='intra42', name='intra42',
client_id=API42_UID, client_id=os.getenv('OAUTH2_INTRA42_CLIENT_ID'),
client_secret=API42_SECRET, client_secret=os.getenv('OAUTH2_INTRA42_CLIENT_SECRET'),
access_token_url='https://api.intra.42.fr/oauth/token', access_token_url='https://api.intra.42.fr/oauth/token',
authorize_url='https://api.intra.42.fr/oauth/authorize', authorize_url='https://api.intra.42.fr/oauth/authorize',
#client_kwargs={'scope': 'identify'},
api_base_url='https://api.intra.42.fr/' api_base_url='https://api.intra.42.fr/'
) )
@ -41,23 +37,24 @@ def connect_intra42(request):
redirect_uri = reverse('accounts:connections-connect-intra42-authorize') redirect_uri = reverse('accounts:connections-connect-intra42-authorize')
redirect_uri = "https://" + site.domain + redirect_uri[3:] # remove language code redirect_uri = "https://" + site.domain + redirect_uri[3:] # remove language code
print(redirect_uri) print(redirect_uri)
return oauth_intra42.intra42.authorize_redirect(request, redirect_uri) return oauth.intra42.authorize_redirect(request, redirect_uri)
@login_required @login_required
def authorize_intra42(request): def authorize_intra42(request):
if request.user.userprofileinfo.intra42_id: if request.user.userprofileinfo.intra42_id:
return bad_request(request, "Already connected") return bad_request(request, "Already connected")
try: try:
token = oauth_intra42.intra42.authorize_access_token(request) token = oauth.intra42.authorize_access_token(request)
except: except:
return redirect('accounts:edit') return redirect('accounts:edit')
response = oauth_intra42.intra42.get('v2/me', token=token) response = oauth.intra42.get('v2/me', token=token)
response = response.json() response = response.json()
intra42_id = response['id'] intra42_id = response['id']
intra42_campus = response['campus'][0]['name'] intra42_campus = response['campus'][0]['name']
print(intra42_campus) intra42_campus_id = response['campus'][0]['id']
request.user.userprofileinfo.intra42_id = intra42_id request.user.userprofileinfo.intra42_id = intra42_id
request.user.userprofileinfo.intra42_campus = intra42_campus request.user.userprofileinfo.intra42_campus = intra42_campus
request.user.userprofileinfo.intra42_campus_id = intra42_campus_id
request.user.userprofileinfo.save() request.user.userprofileinfo.save()
return redirect('accounts:edit') return redirect('accounts:edit')
@ -80,19 +77,20 @@ def connect_discord(request):
site = Site.objects.get_current() site = Site.objects.get_current()
redirect_uri = reverse('accounts:connections-connect-discord-authorize') redirect_uri = reverse('accounts:connections-connect-discord-authorize')
redirect_uri = "https://" + site.domain + redirect_uri[3:] # remove language code redirect_uri = "https://" + site.domain + redirect_uri[3:] # remove language code
print(redirect_uri) return oauth.discord.authorize_redirect(request, redirect_uri)
return oauth_discord.discord.authorize_redirect(request, redirect_uri)
@login_required @login_required
def authorize_discord(request): def authorize_discord(request):
if request.user.userprofileinfo.discord_id: if request.user.userprofileinfo.discord_id:
print("Already")
return bad_request(request, "Already connected") return bad_request(request, "Already connected")
try: try:
token = oauth_discord.discord.authorize_access_token(request) token = oauth.discord.authorize_access_token(request)
except: except:
return redirect('accounts:edit') return redirect('accounts:edit')
response = oauth_discord.discord.get('users/@me', token=token) response = oauth.discord.get('users/@me', token=token)
response = response.json() response = response.json()
print(response)
discord_id = response['id'] discord_id = response['id']
request.user.userprofileinfo.discord_id = discord_id request.user.userprofileinfo.discord_id = discord_id
request.user.userprofileinfo.save() request.user.userprofileinfo.save()