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, '/')
예제 #2
0
    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)
예제 #3
0
    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')
예제 #4
0
    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)
예제 #6
0
    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, '/')
예제 #7
0
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)
예제 #8
0
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)
예제 #9
0
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,
            }
예제 #10
0
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,
    }