Add 42Oauth #37

Merged
Danhia merged 5 commits from :main into 42OAuth 2022-03-29 19:51:57 +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)
class userprofile(admin.ModelAdmin):
#list display
list_display = ['user', 'score', 'last_submission_date']
list_display = ['user', 'score', 'last_submission_date', 'intra42_campus']
# 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)
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_id = models.CharField(max_length=10, null=True, blank=True)
member = models.BooleanField(default=False)
member_since = models.DateTimeField('Member since', default=timezone.now)
member_until = models.DateTimeField('Member until', default=timezone.now)
def __str__(self):
return self.user.username
class Meta:
ordering = ['-score', 'last_submission_date', 'user__username']
ordering = ['-score', 'last_submission_date', 'user__username', 'intra42_campus']
verbose_name = 'profile'
verbose_name_plural = 'profiles'
permissions = (("view_info", "View user info"),)

View File

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