-
Notifications
You must be signed in to change notification settings - Fork 0
Google auth implemented #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Surfskills
wants to merge
2
commits into
master
Choose a base branch
from
fred
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from django.contrib import admin | ||
| from .models import User | ||
|
|
||
|
|
||
| class UserAdmin(admin.ModelAdmin): | ||
| list_display = ['username', 'email', 'auth_provider', 'created_at'] | ||
|
|
||
|
|
||
| admin.site.register(User, UserAdmin) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class AuthenticationConfig(AppConfig): | ||
| default_auto_field = 'django.db.models.BigAutoField' | ||
| name = 'authentication' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Generated by Django 5.0.3 on 2024-06-05 18:01 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ('auth', '0012_alter_user_first_name_max_length'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='User', | ||
| fields=[ | ||
| ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('password', models.CharField(max_length=128, verbose_name='password')), | ||
| ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), | ||
| ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), | ||
| ('username', models.CharField(db_index=True, max_length=255, unique=True)), | ||
| ('email', models.EmailField(db_index=True, max_length=255, unique=True)), | ||
| ('is_verified', models.BooleanField(default=False)), | ||
| ('is_active', models.BooleanField(default=True)), | ||
| ('is_staff', models.BooleanField(default=False)), | ||
| ('created_at', models.DateTimeField(auto_now_add=True)), | ||
| ('updated_at', models.DateTimeField(auto_now=True)), | ||
| ('auth_provider', models.CharField(default=None, max_length=255)), | ||
| ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), | ||
| ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), | ||
| ], | ||
| options={ | ||
| 'abstract': False, | ||
| }, | ||
| ), | ||
| ] |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| from django.contrib.auth.models import ( | ||
| AbstractBaseUser, BaseUserManager, PermissionsMixin) | ||
|
|
||
| from django.db import models | ||
| from rest_framework_simplejwt.tokens import RefreshToken | ||
|
|
||
|
|
||
| class UserManager(BaseUserManager): | ||
|
|
||
| def create_user(self, username, email, password=None): | ||
| if username is None: | ||
| raise TypeError('Users should have a username') | ||
| if email is None: | ||
| raise TypeError('Users should have a Email') | ||
|
|
||
| user = self.model(username=username, email=self.normalize_email(email)) | ||
| user.set_password(password) | ||
| user.save() | ||
| return user | ||
|
|
||
| def create_superuser(self, username, email, password=None): | ||
| if password is None: | ||
| raise TypeError('Password should not be none') | ||
|
|
||
| user = self.create_user(username, email, password) | ||
| user.is_superuser = True | ||
| user.is_staff = True | ||
| user.save() | ||
| return user | ||
|
|
||
|
|
||
| AUTH_PROVIDERS = {'google': 'google','email': 'email'} | ||
|
|
||
|
|
||
| class User(AbstractBaseUser, PermissionsMixin): | ||
| username = models.CharField(max_length=255, unique=True, db_index=True) | ||
| email = models.EmailField(max_length=255, unique=True, db_index=True) | ||
| is_verified = models.BooleanField(default=False) | ||
| is_active = models.BooleanField(default=True) | ||
| is_staff = models.BooleanField(default=False) | ||
| created_at = models.DateTimeField(auto_now_add=True) | ||
| updated_at = models.DateTimeField(auto_now=True) | ||
| auth_provider = models.CharField( | ||
| max_length=255, blank=False, | ||
| null=False, default=AUTH_PROVIDERS.get('email')) | ||
|
|
||
| USERNAME_FIELD = 'email' | ||
| REQUIRED_FIELDS = ['username'] | ||
|
|
||
| objects = UserManager() | ||
|
|
||
| def __str__(self): | ||
| return self.email | ||
|
|
||
| def tokens(self): | ||
| refresh = RefreshToken.for_user(self) | ||
| return { | ||
| 'refresh': str(refresh), | ||
| 'access': str(refresh.access_token) | ||
| } |
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class UsersConfig(AppConfig): | ||
| class GoogleAuthConfig(AppConfig): | ||
| default_auto_field = 'django.db.models.BigAutoField' | ||
| name = 'users' | ||
| name = 'google_auth' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from google.auth.transport import requests | ||
| from google.oauth2 import id_token | ||
| from rest_framework import serializers, status | ||
|
|
||
| class Google: | ||
| """Google class to fetch the user info and return it""" | ||
|
|
||
| @staticmethod | ||
| def validate(auth_token): | ||
| """ | ||
| Validate method queries the Google OAuth2 API to fetch the user info | ||
| """ | ||
| try: | ||
| idinfo = id_token.verify_oauth2_token(auth_token, requests.Request(), clock_skew_in_seconds=5) | ||
|
|
||
| # Acceptable issuer URLs | ||
| valid_issuers = [ | ||
| 'https://accounts.google.com', | ||
| 'accounts.google.com' | ||
| ] | ||
|
|
||
| # matching value issuers | ||
| if idinfo['iss'] not in valid_issuers: | ||
| raise serializers.ValidationError('Invalid token issuer.') | ||
|
|
||
| print(f"Token validated successfully: {idinfo}") | ||
| return idinfo | ||
|
|
||
| except ValueError as ve: | ||
| raise serializers.ValidationError('The token is either invalid or has expired.') | ||
|
|
||
| except Exception as e: | ||
| # error handling | ||
| print(f"Token validation error: {e}") | ||
| raise serializers.ValidationError('An error occurred during token validation.') |
Empty file.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from django.contrib.auth import authenticate | ||
| from authentication.models import User | ||
| import os | ||
| from rest_framework.exceptions import AuthenticationFailed | ||
|
|
||
|
|
||
| def register_social_user(provider, user_id, email, name): | ||
| filtered_user_by_email = User.objects.filter(email=email) | ||
|
|
||
| if filtered_user_by_email.exists(): | ||
| print(f"User with email {email} exists.") | ||
| if provider == filtered_user_by_email[0].auth_provider: | ||
| registered_user = authenticate(email=email, password=os.environ.get('SOCIAL_SECRET')) | ||
| print(f"Authenticated existing user: {registered_user.username}") | ||
| return { | ||
| 'username': registered_user.username, | ||
| 'email': registered_user.email, | ||
| 'tokens': registered_user.tokens() | ||
| } | ||
| else: | ||
| print(f"Authentication failed: Existing user uses a different provider.") | ||
| raise AuthenticationFailed(detail=f'Please continue your login using {filtered_user_by_email[0].auth_provider}') | ||
| else: | ||
| print(f"Creating new user with email {email}") | ||
| user = { | ||
| 'username': email, | ||
| 'email': email, | ||
| 'password': os.environ.get('SOCIAL_SECRET') | ||
| } | ||
| user = User.objects.create_user(**user) | ||
| user.is_verified = True | ||
| user.auth_provider = provider | ||
| user.save() | ||
| new_user = authenticate(email=email, password=os.environ.get('SOCIAL_SECRET')) | ||
| print(f"Created and authenticated new user: {new_user.username}") | ||
| return { | ||
| 'email': new_user.email, | ||
| 'username': new_user.username, | ||
| 'tokens': new_user.tokens() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from rest_framework import serializers | ||
| from .google import Google | ||
| from .register import register_social_user | ||
| import os | ||
| from rest_framework.exceptions import AuthenticationFailed | ||
|
|
||
| class GoogleSocialAuthSerializer(serializers.Serializer): | ||
| auth_token = serializers.CharField() | ||
|
|
||
| def validate_auth_token(self, auth_token): | ||
| user_data = Google.validate(auth_token) | ||
| try: | ||
| user_data['sub'] | ||
| except KeyError: | ||
| print('KeyError: sub not found in user_data') | ||
| raise serializers.ValidationError('The token is invalid or expired. Please login again.') | ||
|
|
||
| if user_data['aud'] != os.environ.get('GOOGLE_CLIENT_ID'): | ||
| print(f"Invalid client ID: {user_data['aud']} != {os.environ.get('GOOGLE_CLIENT_ID')}") | ||
| raise AuthenticationFailed('Invalid client ID.') | ||
|
|
||
| user_id = user_data['sub'] | ||
| email = user_data['email'] | ||
| name = user_data['name'] | ||
| provider = 'google' | ||
|
|
||
| print(f"User data: user_id={user_id}, email={email}, name={name}, provider={provider}") | ||
|
|
||
| return register_social_user(provider=provider, user_id=user_id, email=email, name=name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from django.test import TestCase | ||
|
|
||
| # Create your tests here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from django.urls import path | ||
| from .views import GoogleSocialAuthView | ||
|
|
||
| urlpatterns = [ | ||
| path('google/', GoogleSocialAuthView.as_view()), | ||
|
|
||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from rest_framework import status | ||
| from rest_framework.response import Response | ||
| from rest_framework.generics import GenericAPIView | ||
| from .serializers import GoogleSocialAuthSerializer | ||
|
|
||
| class GoogleSocialAuthView(GenericAPIView): | ||
| serializer_class = GoogleSocialAuthSerializer | ||
|
|
||
| def post(self, request): | ||
| """ | ||
| POST with "auth_token" | ||
| Send an idtoken as from google to get user information | ||
| """ | ||
| print('Request data:', request.data) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also let take this out as well for production |
||
| serializer = self.serializer_class(data=request.data) | ||
| serializer.is_valid(raise_exception=True) | ||
| data = serializer.validated_data['auth_token'] | ||
| print('Validated auth_token:', data) | ||
| return Response(data, status=status.HTTP_200_OK) | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's ensure we remove prints in production but aside that I think we are good