def dispatch(self, request): if 'error' in request.GET or 'code' not in request.GET: # TODO: Distinguish cancel from error return render_authentication_error(request) app = self.adapter.get_provider().get_app(self.request) client = self.get_client(request, app) try: access_token = client.get_access_token(request.GET['code']) token = self.adapter.parse_token(access_token) token.app = app login = self.adapter.complete_login(request, app, token, response=access_token) token.account = login.account login.token = token if self.adapter.supports_state: login.state = SocialLogin \ .verify_and_unstash_state( request, request.REQUEST.get('state')) else: login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login) except (OAuth2Error, PermissionDenied): if self.adapter.getErrorTemplate() is not None: return render_authentication_error(request, error_template=self.adapter.getErrorTemplate()) else: return render_authentication_error(request)
def post(self, request): if 'dr_auth_status' not in request.GET: return render_authentication_error(request, self.provider.id, error=AuthError.UNKNOWN) if request.GET['dr_auth_status'] != 'ok': return render_authentication_error(request, self.provider.id, error=AuthError.DENIED) if 'dr_auth_code' not in request.GET: return render_authentication_error(request, self.provider.id, error=AuthError.UNKNOWN) ret = None auth_exception = None try: app = self.provider.get_app(request) login = draugiem_complete_login(request, app, request.GET['dr_auth_code']) login.state = SocialLogin.unstash_state(request) ret = complete_social_login(request, login) except (requests.RequestException, DraugiemApiError) as e: auth_exception = e if not ret: ret = render_authentication_error(request, self.provider.id, exception=auth_exception) return ret
def dispatch(self, request): if 'error' in request.GET or 'code' not in request.GET: # TODO: Distinguish cancel from error return render_authentication_error(request) app = self.adapter.get_provider().get_app(self.request) client = self.get_client(request, app) try: access_token = client.get_access_token(request.GET['code']) token = self.adapter.parse_token(access_token) token.app = app login = self.adapter.complete_login(request, app, token, response=access_token) token.account = login.account login.token = token if self.adapter.supports_state: login.state = SocialLogin \ .verify_and_unstash_state( request, request.REQUEST.get('state')) else: login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login) except (OAuth2Error, PermissionDenied): return render_authentication_error(request)
def dispatch(self, request): """ View to handle final steps of OAuth based authentication where the user gets redirected back to from the service provider """ provider = self.provider login_done_url = reverse(provider.slug + "_callback") client = self._get_client(request, login_done_url) if not client.is_valid(): if 'denied' in request.GET: error = AuthError.CANCELLED else: error = AuthError.UNKNOWN extra_context = dict(oauth_client=client) return render_authentication_error(request, provider.slug, error=error, extra_context=extra_context) app = provider.get_app(request) try: access_token = client.get_access_token() token = SocialToken( app=app, token=access_token['oauth_token'], # .get() -- e.g. Evernote does not feature a secret token_secret=access_token.get('oauth_token_secret', '') ) login = provider.complete_login(request, app, token, response=access_token) login.token = token login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login) except OAuthError as e: return render_authentication_error(request, provider.slug, exception=e)
def dispatch(self, request): if 'error' in request.GET or not 'code' in request.GET: # TODO: Distinguish cancel from error return render_authentication_error(request) app = self.adapter.get_provider().get_app(self.request) client = self.get_client(request, app) try: access_token = client.get_access_token(request.GET['code']) token = self.adapter.parse_token(access_token) token.app = app # jp 6/1/2014 get open id from qq using the access token open_id = client.get_open_id(token.token) login = self.adapter.complete_login(request, app, token, response=access_token, consumer_key=client.consumer_key, openid=open_id) token.account = login.account login.token = token if self.adapter.supports_state: login.state = SocialLogin \ .verify_and_unstash_state( request, request.REQUEST.get('state')) else: login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login) except OAuth2Error as e: return render_authentication_error(request)
def dispatch(self, request): if 'error' in request.GET or not 'code' in request.GET: # TODO: Distinguish cancel from error return render_authentication_error(request) app = self.adapter.get_provider().get_app(self.request) client = self.get_client(request, app) try: access_token = client.get_access_token(request.GET['code']) token = self.adapter.parse_token(access_token) token.app = app # jp 6/1/2014 get open id from qq using the access token open_id = client.get_open_id(token.token) login = self.adapter.complete_login( request, app, token, response=access_token, consumer_key=client.consumer_key, openid=open_id) token.account = login.account login.token = token if self.adapter.supports_state: login.state = SocialLogin \ .verify_and_unstash_state( request, request.REQUEST.get('state')) else: login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login) except OAuth2Error as e: return render_authentication_error(request)
def callback(request): if 'dr_auth_status' not in request.GET: return render_authentication_error( request, DraugiemProvider.id, error=AuthError.UNKNOWN) if request.GET['dr_auth_status'] != 'ok': return render_authentication_error( request, DraugiemProvider.id, error=AuthError.DENIED) if 'dr_auth_code' not in request.GET: return render_authentication_error( request, DraugiemProvider.id, error=AuthError.UNKNOWN) ret = None auth_exception = None try: app = providers.registry.by_id( DraugiemProvider.id, request).get_app(request) login = draugiem_complete_login( request, app, request.GET['dr_auth_code']) login.state = SocialLogin.unstash_state(request) ret = complete_social_login(request, login) except (requests.RequestException, DraugiemApiError) as e: auth_exception = e if not ret: ret = render_authentication_error( request, DraugiemProvider.id, exception=auth_exception) return ret
def dispatch(self, request): if 'error' in request.GET or 'code' not in request.GET: # Distinguish cancel from error auth_error = request.GET.get('error', None) if auth_error == self.adapter.login_cancelled_error: error = AuthError.CANCELLED else: error = AuthError.UNKNOWN return render_authentication_error(request, self.adapter.provider_id, error=error) app = self.adapter.get_provider().get_app(self.request) client = self.get_client(request, app) try: access_token = client.get_access_token(request.GET['code']) token = self.adapter.parse_token(access_token) token.app = app login = self.adapter.complete_login(request, app, token, response=access_token) login.token = token if self.adapter.supports_state: login.state = SocialLogin \ .verify_and_unstash_state( request, get_request_param(request, 'state')) else: login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login) except (PermissionDenied, OAuth2Error) as e: return render_authentication_error(request, self.adapter.provider_id, exception=e)
def dispatch(self, request): """ View to handle final steps of OAuth based authentication where the user gets redirected back to from the service provider """ login_done_url = reverse(self.adapter.provider_id + "_callback") client = self._get_client(request, login_done_url) if not client.is_valid(): if 'denied' in request.GET: return HttpResponseRedirect(reverse('socialaccount_login_cancelled')) extra_context = dict(oauth_client=client) return render_authentication_error(request, extra_context) app = self.adapter.get_provider().get_app(request) try: access_token = client.get_access_token() token = SocialToken(app=app, token=access_token['oauth_token'], token_secret=access_token['oauth_token_secret']) login = self.adapter.complete_login(request, app, token) token.account = login.account login.token = token login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login) except OAuthError: return render_authentication_error(request)
def dispatch(self, request): """ View to handle final steps of OAuth based authentication where the user gets redirected back to from the service provider """ login_done_url = reverse(self.adapter.provider_id + "_callback") client = self._get_client(request, login_done_url) if not client.is_valid(): if "denied" in request.GET: error = AuthError.CANCELLED else: error = AuthError.UNKNOWN extra_context = dict(oauth_client=client) return render_authentication_error( request, self.adapter.provider_id, error=error, extra_context=extra_context ) app = self.adapter.get_provider().get_app(request) try: access_token = client.get_access_token() token = SocialToken( app=app, token=access_token["oauth_token"], # .get() -- e.g. Evernote does not feature a secret token_secret=access_token.get("oauth_token_secret", ""), ) login = self.adapter.complete_login(request, app, token, response=access_token) login.token = token login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login) except OAuthError as e: return render_authentication_error(request, self.adapter.provider_id, exception=e)
def callback(request): if "dr_auth_status" not in request.GET: return render_authentication_error(request, DraugiemProvider.id, error=AuthError.UNKNOWN) if request.GET["dr_auth_status"] != "ok": return render_authentication_error(request, DraugiemProvider.id, error=AuthError.DENIED) if "dr_auth_code" not in request.GET: return render_authentication_error(request, DraugiemProvider.id, error=AuthError.UNKNOWN) ret = None auth_exception = None try: app = providers.registry.by_id(DraugiemProvider.id, request).get_app(request) login = draugiem_complete_login(request, app, request.GET["dr_auth_code"]) login.state = SocialLogin.unstash_state(request) ret = complete_social_login(request, login) except (requests.RequestException, DraugiemApiError) as e: auth_exception = e if not ret: ret = render_authentication_error(request, DraugiemProvider.id, exception=auth_exception) return ret
def callback(request): client = _openid_consumer(request) response = client.complete(dict(request.REQUEST.items()), request.build_absolute_uri(request.path)) if response.status == consumer.SUCCESS: account = SocialAccount(uid=response.identity_url, provider=OpenIDProvider.id, extra_data={}) account.user = get_adapter().populate_new_user(request, account, email=_get_email_from_response(response)) login = SocialLogin(account) login.state = SocialLogin.unstash_state(request) ret = complete_social_login(request, login) elif response.status == consumer.CANCEL: ret = HttpResponseRedirect(reverse("socialaccount_login_cancelled")) else: ret = render_authentication_error(request) return ret
def callback(request): client = _openid_consumer(request) response = client.complete(dict(request.REQUEST.items()), request.build_absolute_uri(request.path)) if response.status == consumer.SUCCESS: login = providers.registry \ .by_id(OpenIDProvider.id) \ .sociallogin_from_response(request, response) login.state = SocialLogin.unstash_state(request) ret = complete_social_login(request, login) elif response.status == consumer.CANCEL: ret = HttpResponseRedirect(reverse('socialaccount_login_cancelled')) else: ret = render_authentication_error(request) return ret
def dispatch(self, request, *args, **kwargs): auth_error = get_request_param(request, "error") code = get_request_param(request, "code") if auth_error or not code: # Distinguish cancel from error if auth_error == self.adapter.login_cancelled_error: error = AuthError.CANCELLED else: error = AuthError.UNKNOWN return render_authentication_error(request, self.adapter.provider_id, error=error) app = self.adapter.get_provider().get_app(self.request) client = self.get_client(self.request, app) try: token_data = self.adapter.get_access_token_data(self.request, app=app, client=client) token = self.adapter.parse_token(data=token_data) token.app = app login = self.adapter.complete_login(request, app, token, response=token_data) login.token = token state = get_request_param(request, "state") if self.adapter.supports_state: login.state = SocialLogin.verify_and_unstash_state( request, state) else: login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login) except ( PermissionDenied, OAuth2Error, RequestException, ProviderException, ) as e: return render_authentication_error(request, self.adapter.provider_id, exception=e)
def get(self, request): client = self.get_client() response = self.get_openid_response(client) if response.status == consumer.SUCCESS: login = providers.registry \ .by_id(self.provider.id, request) \ .sociallogin_from_response(request, response) login.state = SocialLogin.unstash_state(request) return self.complete_login(login) else: if response.status == consumer.CANCEL: error = AuthError.CANCELLED else: error = AuthError.UNKNOWN return self.render_error(error)
def callback(request): client = _openid_consumer(request) response = client.complete( dict(request.REQUEST.items()), request.build_absolute_uri(request.path)) if response.status == consumer.SUCCESS: login = providers.y_provider_provider \ .by_id(OpenIDProvider.id) \ .sociallogin_from_response(request, response) login.state = SocialLogin.unstash_state(request) ret = complete_social_login(request, login) elif response.status == consumer.CANCEL: ret = HttpResponseRedirect(reverse('socialaccount_login_cancelled')) else: ret = render_authentication_error(request) return ret
def dispatch(self, request, *args, **kwargs): """ This overloading is necessary to manage the case when the user clicks on "Cancel" once on the "Mire de connexion PE Connect". Original code: https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/providers/oauth2/views.py#L113 """ if "error" in request.GET or "code" not in request.GET: # Distinguish cancel from error auth_error = request.GET.get("error", None) if auth_error == self.adapter.login_cancelled_error: error = AuthError.CANCELLED elif auth_error is None and "state" in request.GET: # This custom case happens when the user clicks "Cancel" on the # "Mire de connexion PE Connect". error = AuthError.CANCELLED else: error = AuthError.UNKNOWN logger.error("Unknown error in PEAMU dispatch.") return render_authentication_error(request, self.adapter.provider_id, error=error) app = self.adapter.get_provider().get_app(self.request) client = self.get_client(request, app) try: access_token = client.get_access_token(request.GET["code"]) token = self.adapter.parse_token(access_token) token.app = app login = self.adapter.complete_login(request, app, token, response=access_token) login.token = token if self.adapter.supports_state: login.state = SocialLogin.verify_and_unstash_state( request, get_request_param(request, "state")) else: login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login) except (PermissionDenied, OAuth2Error, RequestException, ProviderException) as e: # This log is useful, 3.6k events for 75 users in 18 days but we have to disable it # to not reach the Sentry limit. The issue is tracked in Trello. # logger.error("Unknown error in PEAMU dispatch with exception '%s'.", e) return render_authentication_error(request, self.adapter.provider_id, exception=e)
def dispatch(self, request): if 'error' in request.GET or 'access_token' not in request.GET: # Distinguish cancel from error auth_error = request.GET.get('error', None) if auth_error == self.adapter.login_cancelled_error: error = AuthError.CANCELLED else: error = AuthError.UNKNOWN return render_authentication_error(request, self.adapter.provider_id, error=error) app = self.adapter.get_provider().get_app(self.request) client = self.get_client(request, app) try: # Parse the access token access_token = { 'access_token': request.GET['access_token'], 'expires_in': request.GET['expires_in'] } token = self.adapter.parse_token(access_token) token.app = app login = self.adapter.complete_login(request, app, token, response=access_token) login.token = token SocialLogin.stash_state(request) if self.adapter.supports_state: login.state = SocialLogin \ .verify_and_unstash_state( request, get_request_param(request, 'state')) else: login.state = SocialLogin.unstash_state(request) # Make sure the user is initiated with some training in the DB request.session['initial_training'] = 'zooniverse' return complete_social_login(request, login) except (PermissionDenied, OAuth2Error, RequestException, ProviderException) as e: return render_authentication_error(request, self.adapter.provider_id, exception=e)
def dispatch(self, request): client = _openid_consumer(request) response = client.complete( dict(list(request.GET.items()) + list(request.POST.items())), request.build_absolute_uri(request.path)) if response.status == consumer.SUCCESS: login = self.provider.sociallogin_from_response(request, response) login.state = SocialLogin.unstash_state(request) ret = complete_social_login(request, login) else: if response.status == consumer.CANCEL: error = AuthError.CANCELLED else: error = AuthError.UNKNOWN ret = render_authentication_error(request, self.provider.id, error=error) return ret
def dispatch(self, request, *args, **kwargs): if 'error' in request.GET or 'code' not in request.GET: # Distinguish cancel from error auth_error = request.GET.get('error', None) if auth_error == self.adapter.login_cancelled_error: error = AuthError.CANCELLED else: error = AuthError.UNKNOWN return render_authentication_error( request, self.adapter.provider_id, error=error) app = self.adapter.get_provider().get_app(self.request) client = self.get_client(request, app) try: access_token = client.get_access_token(request.GET['code']) token = self.adapter.parse_token(access_token) token.app = app login = self.adapter.complete_login(request, app, token, response=access_token) login.token = token if self.adapter.supports_state: login.state = SocialLogin \ .verify_and_unstash_state( request, get_request_param(request, 'state')) else: login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login) except (PermissionDenied, OAuth2Error, RequestException, ProviderException) as e: if isinstance(e, PermissionDenied) or isinstance(e, OAuth2Error): # One of these errors are raised when a user authenticated through dataporten # uses the browser back button. Simply redirect to ``/``. return HttpResponseRedirect('/') return render_authentication_error( request, self.adapter.provider_id, exception=e)
def callback(request): client = _openid_consumer(request) response = client.complete(dict(request.REQUEST.items()), request.build_absolute_uri(request.path)) if response.status == consumer.SUCCESS: user = get_adapter() \ .populate_new_user(email=_get_email_from_response(response)) account = SocialAccount(uid=response.identity_url, provider=OpenIDProvider.id, user=user, extra_data={}) login = SocialLogin(account) login.state = SocialLogin.unstash_state(request) ret = complete_social_login(request, login) elif response.status == consumer.CANCEL: ret = HttpResponseRedirect(reverse('socialaccount_login_cancelled')) else: ret = render_authentication_error(request) return ret
def dispatch(self, request, *args, **kwargs): logger.info("start login") if 'error' in request.GET or 'code' not in request.GET: # Distinguish cancel from error auth_error = request.GET.get('error', None) logger.info("auth_error %s " % auth_error) if auth_error == self.adapter.login_cancelled_error: error = AuthError.CANCELLED logger.info("AuthError.CANCELLED %s " % error) else: error = AuthError.UNKNOWN logger.info("AuthError.UNKNOWN %s %s" % (error, auth_error)) return render_authentication_error(request, self.adapter.provider_id, error=error) app = self.adapter.get_provider().get_app(self.request) logger.info("app %s " % app) client = self.get_client(request, app) try: access_token = client.get_access_token(request.GET['code']) logger.info("access_token %s " % access_token) token = self.adapter.parse_token(access_token) logger.info("parse_token %s " % token) token.app = app login = self.adapter.complete_login(request, app, token, response=access_token) logger.info("logged in %s " % login) login.token = token if self.adapter.supports_state: login.state = SocialLogin \ .verify_and_unstash_state( request, get_request_param(request, 'state')) else: login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login) except (PermissionDenied, OAuth2Error, RequestException, ProviderException) as e: return render_authentication_error(request, self.adapter.provider_id, exception=e)
def dispatch(self, request): """ The CAS server redirects the user to this view after a successful authentication. On redirect, CAS server should add a ticket whose validity is verified here. If ticket is valid, CAS server may also return extra attributes about user. """ client = self.get_client(request) # CAS server should let a ticket. try: ticket = request.GET['ticket'] except KeyError: raise CASAuthenticationError( "CAS server didn't respond with a ticket." ) # Check ticket validity. # Response format on: # - success: username, attributes, pgtiou # - error: None, {}, None response = client.verify_ticket(ticket) uid, extra, _ = response if not uid: raise CASAuthenticationError( "CAS server doesn't validate the ticket." ) # Keep tracks of the last used CAS provider. request.session[CAS_PROVIDER_SESSION_KEY] = self.provider.id data = (uid, extra or {}) # Finish the login flow. login = self.adapter.complete_login(request, data) login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login)
def callback(request): client = _openid_consumer(request) response = client.complete( dict(request.REQUEST.items()), request.build_absolute_uri(request.path)) if response.status == consumer.SUCCESS: login = providers.registry \ .by_id(OpenIDProvider.id) \ .sociallogin_from_response(request, response) login.state = SocialLogin.unstash_state(request) ret = complete_social_login(request, login) else: if response.status == consumer.CANCEL: error = AuthError.CANCELLED else: error = AuthError.UNKNOWN ret = render_authentication_error( request, OpenIDProvider.id, error=error) return ret
def callback(request): client = _openid_consumer(request) response = client.complete( dict(list(request.GET.items()) + list(request.POST.items())), request.build_absolute_uri(request.path)) if response.status == consumer.SUCCESS: login = providers.registry \ .by_id(OpenIDProvider.id, request) \ .sociallogin_from_response(request, response) login.state = SocialLogin.unstash_state(request) ret = complete_social_login(request, login) else: if response.status == consumer.CANCEL: error = AuthError.CANCELLED else: error = AuthError.UNKNOWN ret = render_authentication_error( request, OpenIDProvider.id, error=error) return ret
def dispatch(self, request): if 'error' in request.GET or 'code' not in request.GET: # Distinguish cancel from error auth_error = request.GET.get('error', None) if auth_error == self.adapter.login_cancelled_error: error = AuthError.CANCELLED else: error = AuthError.UNKNOWN return render_authentication_error( request, self.adapter.provider_id, error=error) app = self.adapter.get_provider().get_app(self.request) client = self.get_client(request, app) try: access_token = client.get_access_token(request.GET['code']) token = self.adapter.parse_token(access_token) token.app = app login = self.adapter.complete_login(request, app, token, response=access_token) login.token = token if self.adapter.supports_state: login.state = SocialLogin \ .verify_and_unstash_state( request, get_request_param(request, 'state')) else: login.state = SocialLogin.unstash_state(request) return complete_social_login(request, login) except (PermissionDenied, OAuth2Error, RequestException, ProviderException) as e: return render_authentication_error( request, self.adapter.provider_id, exception=e)
def _unstash_state(request): """Return the state if it exists.""" if 'socialaccount_state' in request.session: return SocialLogin.unstash_state(request) return {}