Exemple #1
0
def get_google_login_url(oauth_redirect_uri=None,
                         redirect_to_after_oauth=None):
    if not oauth_redirect_uri:
        oauth_redirect_uri = '%s%s' % (
            'http://localhost:9095' if request.host.startswith('localhost')
            else authentication.get_host_for_request(request),
            '/_/auth/oauth2_callback')

    if not redirect_to_after_oauth:
        redirect_to_after_oauth = 'http://localhost:5007' if request.host.startswith(
            'localhost') else '/'

    session['redirect_to_after_oauth'] = str(redirect_to_after_oauth)

    # http://oauth2client.readthedocs.io/en/latest/source/oauth2client.client.html
    flow = flow_from_clientsecrets(
        get_path_to_oauth_secrets(),
        scope='https://www.googleapis.com/auth/userinfo.email',
        redirect_uri=oauth_redirect_uri)

    session['oauth_state'] = utils.generate_secret(32)
    try:
        return str(flow.step1_get_authorize_url(state=session['oauth_state']))
    except TypeError:
        # TODO: Fix breakage only appearing in tests.
        return str(flow.step1_get_authorize_url())
    def test_get_host_for_request__override__no_host_attribute(self):
        mock_request = self._get_mock_request(
            None, {'X-Upstream-Host': 'upstream.trot.to'})

        self.assertEqual('https://upstream.trot.to',
                         authentication.get_host_for_request(mock_request))
    def test_get_host_for_request__override(self):
        mock_request = self._get_mock_request(
            'localhost:9095', {'X-Upstream-Host': 'upstream.trot.to'})

        self.assertEqual('https://upstream.trot.to',
                         authentication.get_host_for_request(mock_request))
    def test_get_host_for_request__no_override__localhost(self):
        mock_request = self._get_mock_request('localhost:9095')

        self.assertEqual('http://localhost:9095',
                         authentication.get_host_for_request(mock_request))
    def test_get_host_for_request__no_override(self):
        mock_request = self._get_mock_request('trot.to')

        self.assertEqual('https://trot.to',
                         authentication.get_host_for_request(mock_request))