def test_find_landing_path(self): from pyramid_google_login import find_landing_path request = mock_request() request.registry.settings = {} path = find_landing_path(request) self.assertEqual(path, '/')
def test_find_landing_path_landing_route(self): from pyramid_google_login import find_landing_path request = mock.Mock() request.registry.settings = { 'security.google_login.landing_route': 'myroute', } path = find_landing_path(request) self.assertEqual(path, request.route_path.return_value)
def test_find_landing_path_landing_url(self): from pyramid_google_login import find_landing_path request = mock.Mock() request.registry.settings = { 'security.google_login.landing_url': '/foobar', } path = find_landing_path(request) self.assertEqual(path, '/foobar')
def test_find_landing_path(self): from pyramid_google_login import find_landing_path request = mock.Mock() request.registry.settings = { } path = find_landing_path(request) self.assertEqual(path, '/')
def test_find_landing_path_landing_route_static(self): from pyramid_google_login import find_landing_path request = mock_request() request.registry.settings = { 'security.google_login.landing_route': 'myroute', } request.route_path.side_effect = KeyError() path = find_landing_path(request) self.assertEqual(path, request.static_path.return_value)
def test_find_landing_path_fallback(self): from pyramid_google_login import find_landing_path request = mock.Mock() request.registry.settings = { 'security.google_login.landing_route': 'myroute', } request.route_path.side_effect = KeyError() request.static_path.side_effect = KeyError() path = find_landing_path(request) self.assertEqual(path, '/')
def callback(request): api = request.googleapi settings = request.registry.settings app_url = settings.get(SETTINGS_PREFIX + 'app_url') if app_url is not None: redirect_uri = request.route_url('auth_callback', _app_url=app_url) else: redirect_uri = request.route_url('auth_callback') try: oauth2_token = api.exchange_token_from_code(redirect_uri) userinfo = api.get_userinfo_from_token(oauth2_token) api.check_hosted_domain_user(userinfo) userid = api.get_user_id_from_userinfo(userinfo) except AuthFailed as err: log.warning('Google Login failed (%s)', err) return redirect_to_signin(request, 'Google Login failed (%s)' % err) except Exception as err: log.warning('Google Login failed (%s)', err) # Protect against leaking critical information like client_secret return redirect_to_signin(request, 'Google Login failed (unkown)') # Find the redirect url (fail-safe, the authentication is more important) try: state_params = decode_state(request.params['state']) url = state_params['url'][0] except Exception: url = find_landing_path(request) user_logged_in = UserLoggedIn(request, userid, oauth2_token, userinfo) try: request.registry.notify(user_logged_in) except Exception: log.exception( 'Application crashed processing UserLoggedIn event' '\nuserinfo=%s oauth2_token=%s', userinfo, oauth2_token) return redirect_to_signin(request, 'Google Login failed (application error)') if user_logged_in.headers: headers = user_logged_in.headers else: headers = remember(request, userid) return HTTPFound(location=url, headers=headers)
def callback(request): api = request.googleapi redirect_uri = request.route_url('auth_callback') try: oauth2_token = api.exchange_token_from_code(redirect_uri) userinfo = api.get_userinfo_from_token(oauth2_token) api.check_hosted_domain_user(userinfo) userid = api.get_user_id_from_userinfo(userinfo) except AuthFailed as err: log.warning('Google Login failed (%s)', err) return redirect_to_signin(request, 'Google Login failed (%s)' % err) except Exception as err: log.warning('Google Login failed (%s)', err) # Protect against leaking critical information like client_secret return redirect_to_signin(request, 'Google Login failed (unkown)') # Find the redirect url (fail-safe, the authentication is more important) try: state_params = decode_state(request.params['state']) url = state_params['url'][0] except: url = find_landing_path(request) user_logged_in = UserLoggedIn(request, userid, oauth2_token, userinfo) try: request.registry.notify(user_logged_in) except: log.exception('Application crashed processing UserLoggedIn event' '\nuserinfo=%s oauth2_token=%s', userinfo, oauth2_token) return redirect_to_signin(request, 'Google Login failed (application error)') if user_logged_in.headers: headers = user_logged_in.headers else: headers = remember(request, principal=userid) return HTTPFound(location=url, headers=headers)
def signin(request): googleapi_settings = request.registry.settings['googleapi_settings'] message = request.params.get('message') url = request.params.get('url') if request.authenticated_userid: if url: return HTTPFound(location=url) else: return HTTPFound(location=find_landing_path(request)) if url: redirect_url = request.route_url('auth_signin_redirect', _query={'url': url}) else: redirect_url = request.route_url('auth_signin_redirect') return {'signin_redirect_url': redirect_url, 'message': message, 'signin_banner': googleapi_settings.signin_banner, 'signin_advice': googleapi_settings.signin_advice, 'hosted_domain': googleapi_settings.hosted_domain, }
def signin(request): googleapi_settings = request.registry.settings['googleapi_settings'] message = request.params.get('message') url = request.params.get('url') if request.authenticated_userid: if url: return HTTPFound(location=url) else: return HTTPFound(location=find_landing_path(request)) if url: redirect_url = request.route_url('auth_signin_redirect', _query={'url': url}) else: redirect_url = request.route_url('auth_signin_redirect') return { 'signin_redirect_url': redirect_url, 'message': message, 'signin_banner': googleapi_settings.signin_banner, 'signin_advice': googleapi_settings.signin_advice, 'hosted_domain': googleapi_settings.hosted_domain, }