-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ZDL-97: Integrate Google OAuth login
- Loading branch information
1 parent
beef8da
commit 3fe6a6a
Showing
20 changed files
with
344 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,27 @@ | ||
# Generated by Django 3.0.8 on 2022-02-12 15:43 | ||
|
||
from django.db import migrations, models | ||
|
||
import authentication.validators | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("authentication", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="user", | ||
name="auth_provider", | ||
field=models.CharField( | ||
choices=[ | ||
(authentication.validators.AuthProviders["google"], "google"), | ||
(authentication.validators.AuthProviders["email"], "email"), | ||
], | ||
default="email", | ||
max_length=255, | ||
), | ||
), | ||
] |
This file contains 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 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 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 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 |
---|---|---|
|
@@ -81,6 +81,23 @@ def test_user_login(client): | |
assert response.status_code == 200 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_user_login_with_google_provider(client): | ||
""" | ||
Test User login with email on existing OAuth user | ||
""" | ||
user = UserFactory( | ||
email="[email protected]", password="temp-password", auth_provider="google" | ||
) | ||
data = {"email": user.email, "password": "temp-password"} | ||
|
||
response = client.post("/v1/auth/login/", data) | ||
response_data = response.json() | ||
|
||
assert response.status_code == 403 | ||
assert response_data == {"detail": "Please login using your login provider."} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_invalid_credentials_user_login(client): | ||
""" | ||
|
@@ -151,6 +168,7 @@ def test_retrieve_user_profile(): | |
assert data["first_name"] == "test" | ||
assert data["last_name"] == "test2" | ||
assert data["email"] == "[email protected]" | ||
assert data["auth_provider"] == "email" | ||
assert data.get("password") is None | ||
|
||
|
||
|
@@ -191,3 +209,40 @@ def test_patch_profile_details(): | |
assert modified_user.first_name == "modified_name1" | ||
assert modified_user.last_name == "modified_name2" | ||
assert modified_user.check_password("test2") is True | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_patch_profile_password_of_oauth_user_should_not_update(): | ||
""" | ||
Test patch user profile password of an existing oauth user should not update the password | ||
""" | ||
content_type = MULTIPART_CONTENT | ||
mock_logged_in_user = UserFactory( | ||
email="[email protected]", | ||
first_name="test", | ||
last_name="test2", | ||
auth_provider="google", | ||
password="oauth-generated-password", | ||
groups=Group.objects.all(), | ||
) | ||
user_token = mock_logged_in_user.tokens().token | ||
client = Client(HTTP_AUTHORIZATION=f"Bearer {user_token}") | ||
modified_data = { | ||
"password": "oauth-modified-password", | ||
} | ||
|
||
data = client._encode_json({} if not modified_data else modified_data, content_type) | ||
encoded_data = client._encode_data(data, content_type) | ||
response = client.generic( | ||
"PATCH", | ||
"/v1/auth/profile/", | ||
encoded_data, | ||
content_type=content_type, | ||
secure=False, | ||
enctype="multipart/form-data", | ||
) | ||
|
||
modified_user = User.objects.first() | ||
|
||
assert response.status_code == 204 | ||
assert modified_user.check_password("oauth-generated-password") is True |
This file contains 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 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 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.
This file contains 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,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class SocialAuthConfig(AppConfig): | ||
name = "social_auth" |
This file contains 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,18 @@ | ||
from google.auth.transport import requests | ||
from google.oauth2 import id_token | ||
|
||
|
||
class Google: | ||
@staticmethod | ||
def validate(auth_token): | ||
""" | ||
validate method Queries the Google oAUTH2 api to fetch the user info | ||
""" | ||
try: | ||
id_info = id_token.verify_oauth2_token(auth_token, requests.Request()) | ||
|
||
if "accounts.google.com" in id_info["iss"]: | ||
return id_info | ||
|
||
except ValueError: | ||
raise ValueError("The token is either invalid or has expired") |
This file contains 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,22 @@ | ||
from django.conf import settings | ||
from rest_framework import serializers | ||
from rest_framework.exceptions import AuthenticationFailed | ||
|
||
from social_auth.google import Google | ||
|
||
|
||
class GoogleSocialAuthSerializer(serializers.Serializer): | ||
auth_token = serializers.CharField() | ||
|
||
def validate_auth_token(self, auth_token): | ||
try: | ||
user_data = Google.validate(auth_token) | ||
except ValueError: | ||
raise serializers.ValidationError( | ||
"The token is invalid or expired. Please login again." | ||
) | ||
|
||
if user_data.get("aud") != settings.GOOGLE_CLIENT_ID: | ||
raise AuthenticationFailed("Please login using a valid Google token.") | ||
|
||
return user_data |
Empty file.
Oops, something went wrong.