def test_google_compelete_login_401(self): from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter class LessMockedResponse(MockedResponse): def raise_for_status(self): if self.status_code != 200: raise HTTPError(None) request = RequestFactory().get(reverse(self.provider.id + '_login'), dict(process='login')) adapter = GoogleOAuth2Adapter() app = adapter.get_provider().get_app(request) token = SocialToken(token='some_token') response_with_401 = LessMockedResponse( 401, """ {"error": { "errors": [{ "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } }""") with mock.patch('allauth.socialaccount.providers.google.views.requests' ) as patched_requests: patched_requests.get.return_value = response_with_401 with self.assertRaises(HTTPError): adapter.complete_login(request, app, token)
def oauth_callback(request, *args, **kwargs): try: is_google = request.GET['provider'] == 'google' adapter = GoogleOAuth2Adapter( request) if is_google else FacebookOAuth2Adapter(request) provider = adapter.get_provider() app = adapter.get_provider().get_app(request) try: if is_google: callback_url = adapter.get_callback_url(request, app) scope = provider.get_scope(request) client = OAuth2Client(request, app.client_id, app.secret, adapter.access_token_method, adapter.access_token_url, callback_url, scope, scope_delimiter=adapter.scope_delimiter, headers=adapter.headers, basic_auth=adapter.basic_auth) access_token = client.get_access_token(request.GET['code']) token = adapter.parse_token(access_token) else: access_token = request.GET['code'] token = adapter.parse_token({ 'access_token': access_token, 'token_type': 'bearer', 'expires_in': 5179237 }) # hard-coded properties to make allauth happy token.app = app login = adapter.complete_login(request, app, token, response=access_token) login.token = token return complete_social_login(request, login) except (PermissionDenied, OAuth2Error, RequestException, ProviderException) as e: return render_authentication_error(request, adapter.provider_id, exception=e) except ImmediateHttpResponse as e: return e.response
def login(self, request): try: original_request = request._request token = request.POST.get(self.token_key, '') google_auth_adapter = GoogleOAuth2Adapter(request=original_request) app = SocialApp.objects.get(provider=self.provider) social_auth_token = SocialToken(app=app, token=token) login = google_auth_adapter.complete_login(request=original_request, app=app, token=social_auth_token) \ if self.provider is 'google' else fb_complete_login(request=request, app=app, token=social_auth_token) extra_data = login.account.extra_data json_error_response = None if 'email' not in extra_data: json_error_response = JsonResponse( dict(message='email is not provided'), status=400) if json_error_response is not None: return json_error_response user = User.objects.filter(email=extra_data['email']).first() if user is not None: token, is_created = Token.objects.get_or_create(user=user) return JsonResponse(dict(key=token.key)) login.token = social_auth_token login.state = SocialLogin.state_from_request(original_request) complete_social_login(original_request, login) token, is_created = Token.objects.get_or_create( user=original_request.user) return JsonResponse(dict(key=token.key)) except HTTPError as e: return JsonResponse(dict(message=str(e)), status=400)
def post(self, request, format=None): try: app = SocialApp.objects.get(provider="google") adapter = GoogleOAuth2Adapter(request) callback_url = 'http://localhost:3000' provider = adapter.get_provider() scope = provider.get_scope(request) client = OAuth2Client(self.request, app.client_id, app.secret, adapter.access_token_method, adapter.access_token_url, callback_url, scope, scope_delimiter=adapter.scope_delimiter, headers=adapter.headers, basic_auth=adapter.basic_auth) data = self.request.data code = data.get('code', '') access_token = client.get_access_token(code) token = adapter.parse_token(access_token) token.app = app login = adapter.complete_login(request, app, token, response=access_token) login.token = token complete_social_login(request, login) logged_in_user = login.account.user token, created = TokenModel.objects.get_or_create( user=logged_in_user) return Response({"success": True, 'key': token.key}) except Exception as e: # FIXME: Catch only what is needed print(e) return Response({"erorr": True})