def run_oauth(self, m, user=None): strategy = DjangoStrategy(DjangoStorage) backend = self.Backend_Class(strategy, redirect_uri=self.client_complete_url) start_url = do_auth(backend).url start_query = parse_qs(urlparse(start_url).query) # set 'state' in client backend.data.update({'state': start_query['state']}) m.get(backend.USER_DATA_URL, json={"username": self.social_username, "email": self.social_email}, status_code=200) m.post(backend.ACCESS_TOKEN_URL, json={'access_token': self.access_token, 'token_type': self.token_type, 'expires_in': self.expires_in, 'scope': self.scope, 'refresh_token': self.refresh_token}, status_code=200) def _login(backend, user, social_user): backend.strategy.session_set('username', user.username) do_complete(backend, user=user, login=_login) social = backend.strategy.storage.user.get_social_auth(backend.name, self.social_username) return strategy.session_get('username'), social, backend
def do_login(self, after_complete_checks=True, user_data_body=None, expected_username=None): self.strategy.set_settings({ 'SOCIAL_AUTH_GITHUB_KEY': 'a-key', 'SOCIAL_AUTH_GITHUB_SECRET': 'a-secret-key', 'SOCIAL_AUTH_LOGIN_REDIRECT_URL': self.login_redirect_url, 'SOCIAL_AUTH_AUTHENTICATION_BACKENDS': ( 'social.backends.github.GithubOAuth2', ) }) start_url = do_auth(self.strategy).url target_url = self.strategy.build_absolute_uri( '/complete/github/?code=foobar' ) start_query = parse_qs(urlparse(start_url).query) location_url = target_url + ('?' in target_url and '&' or '?') + \ 'state=' + start_query['state'] location_query = parse_qs(urlparse(location_url).query) HTTPretty.register_uri(HTTPretty.GET, start_url, status=301, location=location_url) HTTPretty.register_uri(HTTPretty.GET, location_url, status=200, body='foobar') response = requests.get(start_url) expect(response.url).to.equal(location_url) expect(response.text).to.equal('foobar') HTTPretty.register_uri(HTTPretty.GET, uri=self.backend.ACCESS_TOKEN_URL, status=200, body=self.access_token_body or '', content_type='text/json') if self.user_data_url: user_data_body = user_data_body or self.user_data_body or '' HTTPretty.register_uri(HTTPretty.GET, self.user_data_url, body=user_data_body, content_type='text/json') self.strategy.set_request_data(location_query) class Request(object):session = {} self.strategy.request = Request() def _login_lambda(strategy, user, social_user): strategy.request = Request() return strategy.session_set('username', user.username) redirect = do_complete( self.strategy, user=self.user, login=_login_lambda ) if after_complete_checks: expect(self.strategy.session_get('username')).to.equal( expected_username or self.expected_username ) expect(redirect.url).to.equal(self.login_redirect_url) return redirect
def auth(request, backend): referer = request.META.get('HTTP_REFERER', '') scheme = 'https' if 'https' in referer else 'http' site_name = getattr(settings, 'SITE_NAME', '') test_site_name = getattr(settings, 'OAUTH_TEST_SITE_NAME', '') if test_site_name: site_name = test_site_name request.session['social_oauth_referer_scheme'] = scheme request.session['social_oauth_redirect_base'] = '{}://{}'.format(scheme, site_name) if site_name else '' return do_auth(request.strategy, redirect_name=REDIRECT_FIELD_NAME)
def get_auth_url(backend, redirect_uri, *args, **kwargs): uri = redirect_uri if uri and not uri.startswith('/'): uri = url_for(uri, backend=backend) g.strategy = load_strategy() g.backend = load_backend(g.strategy, backend, redirect_uri=uri, *args, **kwargs) resp = do_auth(g.backend) return resp.location
def do_login(self, after_complete_checks=True, user_data_body=None, expected_username=None): self.strategy.set_settings( { "SOCIAL_AUTH_GITHUB_KEY": "a-key", "SOCIAL_AUTH_GITHUB_SECRET": "a-secret-key", "SOCIAL_AUTH_LOGIN_REDIRECT_URL": self.login_redirect_url, "SOCIAL_AUTH_AUTHENTICATION_BACKENDS": ("social.backends.github.GithubOAuth2",), } ) start_url = do_auth(self.backend).url target_url = self.strategy.build_absolute_uri("/complete/github/?code=foobar") start_query = parse_qs(urlparse(start_url).query) location_url = target_url + ("?" in target_url and "&" or "?") + "state=" + start_query["state"] location_query = parse_qs(urlparse(location_url).query) HTTPretty.register_uri(HTTPretty.GET, start_url, status=301, location=location_url) HTTPretty.register_uri(HTTPretty.GET, location_url, status=200, body="foobar") response = requests.get(start_url) expect(response.url).to.equal(location_url) expect(response.text).to.equal("foobar") HTTPretty.register_uri( HTTPretty.POST, uri=self.backend.ACCESS_TOKEN_URL, status=200, body=self.access_token_body or "", content_type="text/json", ) if self.user_data_url: user_data_body = user_data_body or self.user_data_body or "" HTTPretty.register_uri(HTTPretty.GET, self.user_data_url, body=user_data_body, content_type="text/json") self.strategy.set_request_data(location_query, self.backend) def _login(backend, user, social_user): backend.strategy.session_set("username", user.username) redirect = do_complete(self.backend, user=self.user, login=_login) if after_complete_checks: expect(self.strategy.session_get("username")).to.equal(expected_username or self.expected_username) expect(redirect.url).to.equal(self.login_redirect_url) return redirect
def auth(backend): return do_auth(g.backend)
def auth(request): return do_auth(request.strategy, redirect_name='next')
def auth(request, backend): return do_auth(request.social_strategy, redirect_name=REDIRECT_FIELD_NAME)
def _auth(self, backend): return do_auth(self.backend)
def _auth(self, backend): do_auth(self.backend)
def do_login_with_partial_pipeline(self, before_complete=None): self.strategy.set_settings( { "SOCIAL_AUTH_GITHUB_KEY": "a-key", "SOCIAL_AUTH_GITHUB_SECRET": "a-secret-key", "SOCIAL_AUTH_LOGIN_REDIRECT_URL": self.login_redirect_url, "SOCIAL_AUTH_AUTHENTICATION_BACKENDS": ("social.backends.github.GithubOAuth2",), "SOCIAL_AUTH_PIPELINE": ( "social.pipeline.social_auth.social_details", "social.pipeline.social_auth.social_uid", "social.pipeline.social_auth.auth_allowed", "social.pipeline.partial.save_status_to_session", "social.tests.pipeline.ask_for_password", "social.pipeline.social_auth.social_user", "social.pipeline.user.get_username", "social.pipeline.user.create_user", "social.pipeline.social_auth.associate_user", "social.pipeline.social_auth.load_extra_data", "social.tests.pipeline.set_password", "social.pipeline.user.user_details", ), } ) start_url = do_auth(self.backend).url target_url = self.strategy.build_absolute_uri("/complete/github/?code=foobar") start_query = parse_qs(urlparse(start_url).query) location_url = target_url + ("?" in target_url and "&" or "?") + "state=" + start_query["state"] location_query = parse_qs(urlparse(location_url).query) HTTPretty.register_uri(HTTPretty.GET, start_url, status=301, location=location_url) HTTPretty.register_uri(HTTPretty.GET, location_url, status=200, body="foobar") response = requests.get(start_url) expect(response.url).to.equal(location_url) expect(response.text).to.equal("foobar") HTTPretty.register_uri( HTTPretty.GET, uri=self.backend.ACCESS_TOKEN_URL, status=200, body=self.access_token_body or "", content_type="text/json", ) if self.user_data_url: HTTPretty.register_uri( HTTPretty.GET, self.user_data_url, body=self.user_data_body or "", content_type="text/json" ) self.strategy.set_request_data(location_query, self.backend) def _login(backend, user, social_user): backend.strategy.session_set("username", user.username) redirect = do_complete(self.backend, user=self.user, login=_login) url = self.strategy.build_absolute_uri("/password") expect(redirect.url).to.equal(url) HTTPretty.register_uri(HTTPretty.GET, redirect.url, status=200, body="foobar") HTTPretty.register_uri(HTTPretty.POST, redirect.url, status=200) password = "******" requests.get(url) requests.post(url, data={"password": password}) data = parse_qs(HTTPretty.last_request.body) expect(data["password"]).to.equal(password) self.strategy.session_set("password", data["password"]) if before_complete: before_complete() redirect = do_complete(self.backend, user=self.user, login=_login) expect(self.strategy.session_get("username")).to.equal(self.expected_username) expect(redirect.url).to.equal(self.login_redirect_url)
def add_social_account(request): request.session['discussion'] = request.matchdict['discussion_slug'] request.session['add_account'] = True # TODO: Make False later. return do_auth(request.backend, redirect_name='next')
def do_login_with_partial_pipeline(self, before_complete=None): self.strategy.set_settings({ 'SOCIAL_AUTH_GITHUB_KEY': 'a-key', 'SOCIAL_AUTH_GITHUB_SECRET': 'a-secret-key', 'SOCIAL_AUTH_LOGIN_REDIRECT_URL': self.login_redirect_url, 'SOCIAL_AUTH_AUTHENTICATION_BACKENDS': ('social.backends.github.GithubOAuth2', ), 'SOCIAL_AUTH_PIPELINE': ('social.pipeline.social_auth.social_details', 'social.pipeline.social_auth.social_uid', 'social.pipeline.social_auth.auth_allowed', 'social.pipeline.partial.save_status_to_session', 'social.tests.pipeline.ask_for_password', 'social.pipeline.social_auth.social_user', 'social.pipeline.user.get_username', 'social.pipeline.user.create_user', 'social.pipeline.social_auth.associate_user', 'social.pipeline.social_auth.load_extra_data', 'social.tests.pipeline.set_password', 'social.pipeline.user.user_details') }) start_url = do_auth(self.backend).url target_url = self.strategy.build_absolute_uri( '/complete/github/?code=foobar') start_query = parse_qs(urlparse(start_url).query) location_url = target_url + ('?' in target_url and '&' or '?') + \ 'state=' + start_query['state'] location_query = parse_qs(urlparse(location_url).query) HTTPretty.register_uri(HTTPretty.GET, start_url, status=301, location=location_url) HTTPretty.register_uri(HTTPretty.GET, location_url, status=200, body='foobar') response = requests.get(start_url) expect(response.url).to.equal(location_url) expect(response.text).to.equal('foobar') HTTPretty.register_uri(HTTPretty.GET, uri=self.backend.ACCESS_TOKEN_URL, status=200, body=self.access_token_body or '', content_type='text/json') if self.user_data_url: HTTPretty.register_uri(HTTPretty.GET, self.user_data_url, body=self.user_data_body or '', content_type='text/json') self.strategy.set_request_data(location_query, self.backend) def _login(backend, user, social_user): backend.strategy.session_set('username', user.username) redirect = do_complete(self.backend, user=self.user, login=_login) url = self.strategy.build_absolute_uri('/password') expect(redirect.url).to.equal(url) HTTPretty.register_uri(HTTPretty.GET, redirect.url, status=200, body='foobar') HTTPretty.register_uri(HTTPretty.POST, redirect.url, status=200) password = '******' requests.get(url) requests.post(url, data={'password': password}) data = parse_qs(HTTPretty.last_request.body) expect(data['password']).to.equal(password) self.strategy.session_set('password', data['password']) if before_complete: before_complete() redirect = do_complete(self.backend, user=self.user, login=_login) expect(self.strategy.session_get('username')).to.equal( self.expected_username) expect(redirect.url).to.equal(self.login_redirect_url)
def auth(backend): return do_auth(g.strategy)
def auth(request, backend): print '--- 1 complete ---' return do_auth(request.backend, redirect_name=REDIRECT_FIELD_NAME)
def auth(request): return do_auth(request.backend, redirect_name='next')
def _auth(self, backend): return do_auth(self.strategy)
def login(self, backend): return do_auth(self.backend)
def do_login_with_partial_pipeline(self, before_complete=None): self.strategy.set_settings({ 'SOCIAL_AUTH_GITHUB_KEY': 'a-key', 'SOCIAL_AUTH_GITHUB_SECRET': 'a-secret-key', 'SOCIAL_AUTH_LOGIN_REDIRECT_URL': self.login_redirect_url, 'SOCIAL_AUTH_AUTHENTICATION_BACKENDS': ( 'social.backends.github.GithubOAuth2', ), 'SOCIAL_AUTH_PIPELINE': ( 'social.pipeline.social_auth.social_details', 'social.pipeline.social_auth.social_uid', 'social.pipeline.social_auth.auth_allowed', 'social.pipeline.partial.save_status_to_session', 'social.tests.pipeline.ask_for_password', 'social.pipeline.social_auth.social_user', 'social.pipeline.user.get_username', 'social.pipeline.user.create_user', 'social.pipeline.social_auth.associate_user', 'social.pipeline.social_auth.load_extra_data', 'social.tests.pipeline.set_password', 'social.pipeline.user.user_details' ) }) start_url = do_auth(self.strategy).url target_url = self.strategy.build_absolute_uri( '/complete/github/?code=foobar' ) start_query = parse_qs(urlparse(start_url).query) location_url = target_url + ('?' in target_url and '&' or '?') + \ 'state=' + start_query['state'] location_query = parse_qs(urlparse(location_url).query) HTTPretty.register_uri(HTTPretty.GET, start_url, status=301, location=location_url) HTTPretty.register_uri(HTTPretty.GET, location_url, status=200, body='foobar') response = requests.get(start_url) expect(response.url).to.equal(location_url) expect(response.text).to.equal('foobar') HTTPretty.register_uri(HTTPretty.GET, uri=self.backend.ACCESS_TOKEN_URL, status=200, body=self.access_token_body or '', content_type='text/json') if self.user_data_url: HTTPretty.register_uri(HTTPretty.GET, self.user_data_url, body=self.user_data_body or '', content_type='text/json') self.strategy.set_request_data(location_query) def _login(strategy, user, social_user): strategy.session_set('username', user.username) redirect = do_complete(self.strategy, user=self.user, login=_login) url = self.strategy.build_absolute_uri('/password') expect(redirect.url).to.equal(url) HTTPretty.register_uri(HTTPretty.GET, redirect.url, status=200, body='foobar') HTTPretty.register_uri(HTTPretty.POST, redirect.url, status=200) password = '******' requests.get(url) requests.post(url, data={'password': password}) data = parse_qs(HTTPretty.last_request.body) expect(data['password']).to.equal(password) self.strategy.session_set('password', data['password']) if before_complete: before_complete() redirect = do_complete(self.strategy, user=self.user, login=_login) expect(self.strategy.session_get('username')).to.equal( self.expected_username ) expect(redirect.url).to.equal(self.login_redirect_url)
def login(self, backend): return do_auth(self.strategy)
def auth(request): forget(request) request.session['discussion'] = request.matchdict['discussion_slug'] request.session['add_account'] = False return do_auth(request.backend, redirect_name='next')
def auth(request): request.session['discussion'] = request.matchdict['discussion_slug'] request.session['add_account'] = False return do_auth(request.backend, redirect_name='next')
def auth(request, backend): return do_auth(request.backend, redirect_name=REDIRECT_FIELD_NAME)
def login(self, backend): ret = do_auth(self.strategy) cherrypy.log.error("session=" + repr(cherrypy.session.items())) return ret
def login(self, backend): ret = do_auth(self.strategy) cherrypy.log.error("session=" + repr(cherrypy.session.items()) ) return ret
def auth(request, backend): """ Start the authentication flow """ return do_auth(request.backend, redirect_name=REDIRECT_FIELD_NAME)
def _auth(self, backend): do_auth(self.strategy)