-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feature/#18 #32
Open
yuri-0415
wants to merge
17
commits into
main
Choose a base branch
from
Feature/#18
base: main
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
Feature/#18 #32
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e139856
Merge branch 'main' of https://github.com/wafflestudio18-5/team2-serv…
yuri-0415 ce16058
Feature/#18
yuri-0415 5026133
Feature/#18
yuri-0415 da1379a
Add google support
gyusang ebedca3
Create custom google login
gyusang fc76c24
update requirements.txt
yuri-0415 4cb0468
update facebook using default provider
yuri-0415 49c7d17
Merge branch 'main' into Feature/#18
yuri-0415 d127ff6
update
yuri-0415 d9ba747
Merge branch 'Feature/#18' of https://github.com/wafflestudio18-5/tea…
yuri-0415 b95e5a3
Update requirements.txt
yuri-0415 09fb0f6
Update requirements.txt
yuri-0415 2104e45
update
yuri-0415 08334be
Merge branch 'Feature/#18' of https://github.com/wafflestudio18-5/tea…
yuri-0415 5889859
Merge branch 'main' into Feature/#18
yuri-0415 04eca2b
Update requirements.txt
yuri-0415 41b8d0c
Merge branch 'main' of https://github.com/wafflestudio18-5/team2-serv…
yuri-0415 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
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 |
---|---|---|
@@ -1,17 +1,27 @@ | ||
asgiref==3.3.1 | ||
certifi==2020.12.5 | ||
chardet==4.0.0 | ||
Django==3.1.3 | ||
django-debug-toolbar==3.1.1 | ||
django-rest-framework==0.1.0 | ||
djangorestframework==3.12.2 | ||
idna==2.10 | ||
mailjet-rest==1.3.4 | ||
mysqlclient==2.0.1 | ||
python-dotenv==0.15.0 | ||
pytz==2020.4 | ||
requests==2.25.1 | ||
sqlparse==0.4.1 | ||
urllib3==1.26.2 | ||
django-redis==4.12.1 | ||
redis==3.5.3 | ||
asgiref==3.3.1 | ||
certifi==2020.12.5 | ||
cffi==1.14.4 | ||
chardet==4.0.0 | ||
cryptography==3.3.1 | ||
defusedxml==0.6.0 | ||
Django==3.1.3 | ||
django-allauth==0.44.0 | ||
django-debug-toolbar==3.1.1 | ||
django-rest-framework==0.1.0 | ||
djangorestframework==3.12.2 | ||
idna==2.10 | ||
mailjet-rest==1.3.4 | ||
mysqlclient==2.0.1 | ||
oauthlib==3.1.0 | ||
python-dotenv==0.15.0 | ||
python3-openid==3.2.0 | ||
pycparser==2.20 | ||
PyJWT==2.0.0 | ||
pytz==2020.4 | ||
requests==2.25.1 | ||
requests-oauthlib==1.3.0 | ||
six==1.15.0 | ||
sqlparse==0.4.1 | ||
urllib3==1.26.2 | ||
django-redis==4.12.1 | ||
redis==3.5.3 |
Binary file not shown.
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,9 @@ | ||
from allauth.socialaccount.providers.facebook.provider import FacebookProvider | ||
|
||
|
||
class FacebookProviderNoRedirect(FacebookProvider): | ||
id = 'facebook_no_redirect' | ||
name = 'Custom Facebook' | ||
|
||
|
||
provider_classes = [FacebookProviderNoRedirect] |
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,9 @@ | ||
from django.urls import path | ||
from .views import oauth2_callback, oauth2_login | ||
from .provider import FacebookProviderNoRedirect | ||
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns | ||
|
||
urlpatterns = [ | ||
path("facebook/login/", oauth2_login, name='facebook_no_redirect_login'), | ||
path("facebook/login/callback/", oauth2_callback, name='facebook_no_redirect_callback') | ||
] |
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,98 @@ | ||
from allauth.socialaccount.providers.facebook.views import ( | ||
OAuth2CallbackView, | ||
OAuth2LoginView, | ||
FacebookOAuth2Adapter, | ||
) | ||
from django.http import HttpResponseRedirect | ||
from django.conf import settings | ||
from .provider import FacebookProviderNoRedirect | ||
from user.serializers import UserSerializer | ||
from rest_framework.authtoken.models import Token | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
from rest_framework import status | ||
|
||
|
||
class SocialLoginView(APIView): | ||
def dispatch(self, request, *args, **kwargs): | ||
self.args = args | ||
self.kwargs = kwargs | ||
request = self.initialize_request(request, *args, **kwargs) | ||
self.request = request | ||
self.headers = self.default_response_headers # deprecate? | ||
|
||
try: | ||
self.initial(request, *args, **kwargs) | ||
except Exception as exc: | ||
pass | ||
|
||
def render(self, response): | ||
query_string = self.request.META.get('QUERY_STRING') | ||
if query_string: | ||
queries = query_string.split('&') | ||
for i in range(len(queries)): | ||
if 'state' in queries[i]: | ||
queries[i] = 'state=' + 'x' * 12 | ||
elif 'code' in queries[i]: | ||
queries[i] = 'code=' + 'x' * 75 | ||
self.request.META['QUERY_STRING'] = '&'.join(queries) | ||
self.response = self.finalize_response(self.request, response, *self.args, **self.kwargs) | ||
return self.response | ||
|
||
|
||
class TokenOAuth2CallbackView(OAuth2CallbackView): | ||
def dispatch(self, request, *args, **kwargs): | ||
api_view = SocialLoginView() | ||
api_view.dispatch(request, *args, **kwargs) | ||
res = super(TokenOAuth2CallbackView, self).dispatch(request, *args, **kwargs) | ||
if isinstance(res, HttpResponseRedirect) and res.url == settings.LOGIN_REDIRECT_URL: | ||
assert hasattr(request, 'user') | ||
# login success | ||
data = UserSerializer(instance=request.user).data | ||
token, created = Token.objects.get_or_create(user=request.user) | ||
data['token'] = token.key | ||
res = Response(data=data) | ||
else: | ||
res = Response({ | ||
'error': 'An error occurred in social login.', | ||
}, status=status.HTTP_400_BAD_REQUEST) | ||
return api_view.render(res) | ||
|
||
|
||
class TokenOAuth2LoginView(OAuth2LoginView): | ||
def dispatch(self, request, *args, **kwargs): | ||
api_view = SocialLoginView() | ||
api_view.dispatch(request, *args, **kwargs) | ||
res = super(TokenOAuth2LoginView, self).dispatch(request, *args, **kwargs) | ||
if isinstance(res, HttpResponseRedirect) and res.url: | ||
url = res.url | ||
data = { | ||
'url': url, | ||
} | ||
try: | ||
queries = url.split('?') | ||
queries = queries[1].split('&') | ||
for query in queries: | ||
if query[:6] == 'state=': | ||
data['state'] = query[6:] | ||
except: | ||
pass | ||
if 'state' not in data: | ||
res = Response({ | ||
'error': 'An error occurred in social login.' | ||
}, status=status.HTTP_400_BAD_REQUEST) | ||
else: | ||
res = Response(data) | ||
else: | ||
res = Response({ | ||
'error': 'An error occurred in social login.', | ||
}, status=status.HTTP_400_BAD_REQUEST) | ||
return api_view.render(res) | ||
|
||
|
||
class FacebookOAuth2NoRedirectAdapter(FacebookOAuth2Adapter): | ||
provider_id = FacebookProviderNoRedirect.id | ||
|
||
|
||
oauth2_callback = TokenOAuth2CallbackView.adapter_view(FacebookOAuth2NoRedirectAdapter) | ||
oauth2_login = TokenOAuth2LoginView.adapter_view(FacebookOAuth2NoRedirectAdapter) |
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,9 @@ | ||
from allauth.socialaccount.providers.google.provider import GoogleProvider | ||
|
||
|
||
class GoogleProviderNoRedirect(GoogleProvider): | ||
id = 'google_no_redirect' | ||
name = 'Custom Google' | ||
|
||
|
||
provider_classes = [GoogleProviderNoRedirect] |
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,7 @@ | ||
from django.urls import path | ||
from .views import oauth2_callback, oauth2_login | ||
|
||
urlpatterns = [ | ||
path("google/login/", oauth2_login, name='google_no_redirect_login'), | ||
path("google/login/callback/", oauth2_callback, name='google_no_redirect_callback') | ||
] |
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,98 @@ | ||
from allauth.socialaccount.providers.google.views import ( | ||
OAuth2CallbackView, | ||
OAuth2LoginView, | ||
GoogleOAuth2Adapter, | ||
) | ||
from django.http import HttpResponseRedirect | ||
from django.conf import settings | ||
from .provider import GoogleProviderNoRedirect | ||
from user.serializers import UserSerializer | ||
from rest_framework.authtoken.models import Token | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
from rest_framework import status | ||
|
||
|
||
class SocialLoginView(APIView): | ||
def dispatch(self, request, *args, **kwargs): | ||
self.args = args | ||
self.kwargs = kwargs | ||
request = self.initialize_request(request, *args, **kwargs) | ||
self.request = request | ||
self.headers = self.default_response_headers # deprecate? | ||
|
||
try: | ||
self.initial(request, *args, **kwargs) | ||
except Exception as exc: | ||
pass | ||
|
||
def render(self, response): | ||
query_string = self.request.META.get('QUERY_STRING') | ||
if query_string: | ||
queries = query_string.split('&') | ||
for i in range(len(queries)): | ||
if 'state' in queries[i]: | ||
queries[i] = 'state=' + 'x' * 12 | ||
elif 'code' in queries[i]: | ||
queries[i] = 'code=' + 'x' * 75 | ||
self.request.META['QUERY_STRING'] = '&'.join(queries) | ||
self.response = self.finalize_response(self.request, response, *self.args, **self.kwargs) | ||
return self.response | ||
|
||
|
||
class TokenOAuth2CallbackView(OAuth2CallbackView): | ||
def dispatch(self, request, *args, **kwargs): | ||
api_view = SocialLoginView() | ||
api_view.dispatch(request, *args, **kwargs) | ||
res = super(TokenOAuth2CallbackView, self).dispatch(request, *args, **kwargs) | ||
if isinstance(res, HttpResponseRedirect) and res.url == settings.LOGIN_REDIRECT_URL: | ||
assert hasattr(request, 'user') | ||
# login success | ||
data = UserSerializer(instance=request.user).data | ||
token, created = Token.objects.get_or_create(user=request.user) | ||
data['token'] = token.key | ||
res = Response(data=data) | ||
else: | ||
res = Response({ | ||
'error': 'An error occurred in social login.', | ||
}, status=status.HTTP_400_BAD_REQUEST) | ||
return api_view.render(res) | ||
|
||
|
||
class TokenOAuth2LoginView(OAuth2LoginView): | ||
def dispatch(self, request, *args, **kwargs): | ||
api_view = SocialLoginView() | ||
api_view.dispatch(request, *args, **kwargs) | ||
res = super(TokenOAuth2LoginView, self).dispatch(request, *args, **kwargs) | ||
if isinstance(res, HttpResponseRedirect) and res.url: | ||
url = res.url | ||
data = { | ||
'url': url, | ||
} | ||
try: | ||
queries = url.split('?') | ||
queries = queries[1].split('&') | ||
for query in queries: | ||
if query[:6] == 'state=': | ||
data['state'] = query[6:] | ||
except: | ||
pass | ||
if 'state' not in data: | ||
res = Response({ | ||
'error': 'An error occurred in social login.' | ||
}, status=status.HTTP_400_BAD_REQUEST) | ||
else: | ||
res = Response(data) | ||
else: | ||
res = Response({ | ||
'error': 'An error occurred in social login.', | ||
}, status=status.HTTP_400_BAD_REQUEST) | ||
return api_view.render(res) | ||
|
||
|
||
class GoogleOAuth2NoRedirectAdapter(GoogleOAuth2Adapter): | ||
provider_id = GoogleProviderNoRedirect.id | ||
|
||
|
||
oauth2_callback = TokenOAuth2CallbackView.adapter_view(GoogleOAuth2NoRedirectAdapter) | ||
oauth2_login = TokenOAuth2LoginView.adapter_view(GoogleOAuth2NoRedirectAdapter) |
Oops, something went wrong.
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.
@yuri-0415 Custom Google을 만든 것은, callback에서 로그인에 성공했을 때 response를 302 redirect 대신 200 ok로 바꾸고, 안에 user 정보와 토큰 정보를 포함하기 위함입니다.
또한 /accounts/google/login/을 요청했을 때에도 로그인 링크로 302 redirect되는 대신 url을 body에 포함한 200 ok가 됩니다. (API 문서를 참고해 주세요)