Example #1
0
def oauth2_callback():
  try:
    if login_via_test_token():
      return redirect('/')
  except:
    return 'error', 500

  flow = flow_from_clientsecrets(get_path_to_oauth_secrets(),
                                 scope='https://www.googleapis.com/auth/userinfo.email',
                                 redirect_uri=f'{authentication.get_host_for_request(request)}/_/auth/oauth2_callback')

  if not session.get('oauth_state') or session.get('oauth_state') != request.args.get('state'):
    return redirect(url_for('base.login'))

  try:
    credentials = flow.step2_exchange(request.args.get('code'))
  except (FlowExchangeError, ValueError) as e:
    logging.warning(e)
    # user declined to auth; move on
    return redirect(session.get('redirect_to_after_oauth', '/'))

  user_email = authentication.get_user_email(credentials)

  if user_email:
    authentication.login('google', user_email=user_email)

  return redirect(session.get('redirect_to_after_oauth', '/'))
Example #2
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())
Example #3
0
    def test_get_path_to_oauth_secrets__production_file_exists(
            self, mock_isfile):
        self.assertEqual(
            'config/client_secrets.json',
            '/'.join(config.get_path_to_oauth_secrets().rsplit('/', 2)[-2:]))

        self.assertTrue(
            mock_isfile.call_args[0][0].endswith('config/client_secrets.json'))
Example #4
0
 def test_get_path_to_oauth_secrets__production_file_does_not_exist__local_env(self, mock_isfile, mock_is_local):
   self.assertEqual('local/client_secrets_local_only.json',
                    '/'.join(config.get_path_to_oauth_secrets().rsplit('/', 2)[-2:]))