Skip to content

Commit

Permalink
Update requirements.txt. Add github support for social authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasvallejosdev committed May 28, 2024
1 parent e663212 commit 524e059
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 27 deletions.
3 changes: 2 additions & 1 deletion auth_api/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.urls import path
from .views import GoogleLoginView, ConnectionView
from .views import GoogleLoginView, ConnectionView, GitHubLoginView

app_name = "auth_api"
urlpatterns = [
path("verify/", ConnectionView.as_view(), name="connection"),
path("social/login/github/", GitHubLoginView.as_view(), name="login-github"),
path("social/login/google/", GoogleLoginView.as_view(), name="login-google"),
]
13 changes: 9 additions & 4 deletions auth_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
import os

from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
from allauth.socialaccount.providers.github.views import GitHubOAuth2Adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
from dj_rest_auth.registration.views import SocialLoginView

from rest_framework.response import Response


class GoogleLoginView(SocialLoginView): # Authorization Code grant
class GoogleLoginView(SocialLoginView):
adapter_class = GoogleOAuth2Adapter
callback_url = f"{os.environ.get('BASE_URL', 'http://localhost:3000')}/api/auth/callback/google"
client_class = OAuth2Client


class GitHubLoginView(SocialLoginView):
adapter_class = GitHubOAuth2Adapter
callback_url = f"{os.environ.get('BASE_URL', 'http://localhost:3000')}/api/auth/callback/github"
client_class = OAuth2Client


class ConnectionView(APIView):
permission_classes = (permissions.IsAuthenticated,)

def post(self, request, *args, **kwargs):
return Response({
"message": "Connection successfully!"
})
return Response({"message": "Connection successfully!"})
8 changes: 4 additions & 4 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
flake8
pytest
pytest-django
model_bakery
flake8==7.0.0
pytest==8.2.1
pytest-django==4.8.0
model_bakery==1.0.0
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ PyJWT==2.7.0

dj-database-url==1.0.0
whitenoise==6.2.0
gunicorn==20.1.0
gunicorn==20.1.0

groq=0.8.0
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ filterwarnings =
ignore::UserWarning
markers =
unit: unit tests
e2e: end-to-end tests
e2e: end-to-end tests
ai: artificial intelligence tests
14 changes: 12 additions & 2 deletions tests/todo_api/test_list_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,20 @@ def test_retrieve_list_limited_to_user(api_client_with_credentials, user2):
assert res.status_code == status.HTTP_404_NOT_FOUND


def test_retrieve_inbox_list_not_found(api_client_with_credentials, user):
def test_retrieve_list_inbox(api_client_with_credentials, user):
baker.make(TaskList, created_by=user, name="inbox")
res = api_client_with_credentials.get(tasks_detail_url("inbox"))
assert res.status_code == status.HTTP_404_NOT_FOUND
assert res.status_code == status.HTTP_200_OK
assert res.data["list_uuid"] == "inbox"
assert res.data["name"] == "inbox"


def test_retrieve_list_upcoming(api_client_with_credentials, user):
baker.make(TaskList, created_by=user, name="upcoming")
res = api_client_with_credentials.get(tasks_detail_url("upcoming"))
assert res.status_code == status.HTTP_200_OK
assert res.data["list_uuid"] == "upcoming"
assert res.data["name"] == "upcoming"


def test_create_list(api_client_with_credentials, user):
Expand Down
7 changes: 7 additions & 0 deletions todo_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ def retrieve(self, request, list_uuid=None, *args, **kwargs):
Retrieve a task list object by list_uuid.
"""
list_uuid = list_uuid.lower()
if list_uuid in ["inbox", "upcoming"]:
data = {
"list_uuid": list_uuid,
"name": list_uuid,
}
serializer = self.get_serializer(data)
return Response(serializer.data)
queryset = self.get_queryset()

try:
Expand Down
36 changes: 23 additions & 13 deletions todo_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"core",
"todo_api",
"user_api",
"ai_api",
"auth_api",
"django.contrib.sites",
"dj_rest_auth",
Expand All @@ -50,6 +51,7 @@
"allauth.account",
"allauth.socialaccount",
"allauth.socialaccount.providers.google",
"allauth.socialaccount.providers.github",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -171,7 +173,7 @@
SITE_ID = 1 # https://dj-rest-auth.readthedocs.io/en/latest/installation.html#registration-optional
REST_USE_JWT = True # use JSON Web

# # Social authentication configuration
# All auth social providers configuration
SOCIALACCOUNT_PROVIDERS = {
"google": {
"SCOPE": [
Expand All @@ -183,12 +185,30 @@
"access_type": "online",
},
"VERIFIED_EMAIL": True,
}
"APP": {
"client_id": os.environ.get("GOOGLE_CLIENT_ID"),
"secret": os.environ.get("GOOGLE_CLIENT_SECRET"),
},
},
"github": {
"SCOPE": [
"user",
"repo",
],
"AUTH_PARAMS": {
"access_type": "online",
},
"VERIFIED_EMAIL": True,
"APP": {
"client_id": os.environ.get("GITHUB_CLIENT_ID"),
"secret": os.environ.get("GITHUB_CLIENT_SECRET"),
},
},
}

# JWT Settings
SIMPLE_JWT = {
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=30),
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=480),
"REFRESH_TOKEN_LIFETIME": timedelta(days=7),
"ROTATE_REFRESH_TOKENS": True,
"BLACKLIST_AFTER_ROTATION": True,
Expand All @@ -197,13 +217,3 @@
"USER_ID_CLAIM": "user_id",
"SIGNING_KEY": os.getenv("JWT_SECRET_KEY"),
}

# All auth social providers configuration
SOCIALACCOUNT_PROVIDERS = {
"google": {
"APP": {
"client_id": os.environ.get("GOOGLE_CLIENT_ID"),
"secret": os.environ.get("GOOGLE_CLIENT_SECRET"),
}
}
}
3 changes: 2 additions & 1 deletion todo_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
path("admin/", admin.site.urls),
path("api/", include("todo_api.urls"), name="task"),
path("api/user/", include("user_api.urls"), name="user"),
path("api/rest/auth/", include("dj_rest_auth.urls"), name="rest-auth"),
path("api/auth/", include("auth_api.urls"), name="auth"),
path("api/ai/", include("ai_api.urls"), name="ai"),
path("accounts/", include("allauth.urls")),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Expand Down

0 comments on commit 524e059

Please sign in to comment.