-
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.
Merge branch 'main' of https://github.com/wafflestudio18-5/team2-server…
… into Feature/#18
- Loading branch information
Showing
9 changed files
with
96 additions
and
186 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
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,98 +1,35 @@ | ||
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 | ||
import requests | ||
from allauth.socialaccount import providers | ||
from allauth.socialaccount.providers.facebook.provider import GRAPH_API_URL | ||
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter | ||
from allauth.socialaccount.providers.facebook.views import compute_appsecret_proof | ||
|
||
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) | ||
from .provider import FacebookProviderNoRedirect | ||
from ..google.views import TokenOAuth2LoginView, TokenOAuth2CallbackView | ||
|
||
|
||
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) | ||
def fb_complete_login(request, app, token): | ||
provider = providers.registry.by_id(FacebookProviderNoRedirect.id, request) | ||
resp = requests.get( | ||
GRAPH_API_URL + "/me", | ||
params={ | ||
"fields": ",".join(provider.get_fields()), | ||
"access_token": token.token, | ||
"appsecret_proof": compute_appsecret_proof(app, token), | ||
}, | ||
) | ||
resp.raise_for_status() | ||
extra_data = resp.json() | ||
login = provider.sociallogin_from_response(request, extra_data) | ||
return login | ||
|
||
|
||
class FacebookOAuth2NoRedirectAdapter(FacebookOAuth2Adapter): | ||
provider_id = FacebookProviderNoRedirect.id | ||
|
||
def complete_login(self, request, app, access_token, **kwargs): | ||
return fb_complete_login(request, app, access_token) | ||
|
||
|
||
oauth2_callback = TokenOAuth2CallbackView.adapter_view(FacebookOAuth2NoRedirectAdapter) | ||
oauth2_login = TokenOAuth2LoginView.adapter_view(FacebookOAuth2NoRedirectAdapter) |
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,4 +1,5 @@ | ||
from django.urls import path | ||
|
||
from .views import oauth2_callback, oauth2_login | ||
|
||
urlpatterns = [ | ||
|
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
Oops, something went wrong.