Beispiel #1
0
def DoInstalledAppBrowserFlowGoogleAuth(launch_browser,
                                        scopes,
                                        client_id_file=None):
  """Launches a 3LO oauth2 flow to get google-auth credentials.

  Args:
    launch_browser: bool, True to launch the browser, false to ask users to copy
      the auth url to a browser.
    scopes: [str], The list of scopes to authorize.
    client_id_file: str, The path to a file containing the client id and secret
      to use for the flow.  If None, the default client id for the Cloud SDK is
      used.
  Returns:
    google.auth.credentials.Credentials, The credentials obtained from the flow.
  """
  if client_id_file:
    AssertClientSecretIsInstalledType(client_id_file)
  google_auth_flow = c_flow.CreateGoogleAuthFlow(scopes, client_id_file)
  try:
    user_creds = c_flow.RunGoogleAuthFlow(google_auth_flow, launch_browser)
    return c_google_auth.UserCredWithReauth.FromGoogleAuthUserCredentials(
        user_creds)
  except c_flow.Error as e:
    if c_store.IsContextAwareAccessDeniedError(e):
      msg = c_store.CONTEXT_AWARE_ACCESS_HELP_MSG
    else:
      msg = 'There was a problem with web authentication.'
      if launch_browser:
        msg += ' Try running again with --no-launch-browser.'
    log.error(msg)
    raise
Beispiel #2
0
 def testRunGoogleAuthFlow_LaunchBrowser(self):
     run_local_server_mock = self.StartObjectPatch(flow.InstalledAppFlow,
                                                   'run_local_server')
     run_console_mock = self.StartObjectPatch(flow.InstalledAppFlow,
                                              'run_console')
     google_auth_flow = flow.CreateGoogleAuthFlow(self.scopes,
                                                  self.client_id_file)
     flow.RunGoogleAuthFlow(google_auth_flow, launch_browser=True)
     run_console_mock.assert_not_called()
     run_local_server_mock.assert_called()
Beispiel #3
0
 def testRunGoogleAuthFlow_LaunchBrowser_LocalServerError(self):
     run_local_server_mock = self.StartObjectPatch(flow.InstalledAppFlow,
                                                   'run_local_server')
     run_local_server_mock.side_effect = flow.LocalServerCreationError
     run_console_mock = self.StartObjectPatch(flow.InstalledAppFlow,
                                              'run_console')
     google_auth_flow = flow.CreateGoogleAuthFlow(self.scopes,
                                                  self.client_id_file)
     flow.RunGoogleAuthFlow(google_auth_flow, launch_browser=True)
     run_console_mock.assert_called()
     run_local_server_mock.assert_called()
     self.AssertErrContains('Defaulting to URL copy/paste mode.')
Beispiel #4
0
def DoInstalledAppBrowserFlowGoogleAuth(launch_browser,
                                        scopes,
                                        client_id_file=None):
    """Launches a 3LO oauth2 flow to get google-auth credentials.

  Args:
    launch_browser: bool, True to launch the browser, false to ask users to copy
      the auth url to a browser.
    scopes: [str], The list of scopes to authorize.
    client_id_file: str, The path to a file containing the client id and secret
      to use for the flow.  If None, the default client id for the Cloud SDK is
      used.
  Returns:
    google.auth.credentials.Credentials, The credentials obtained from the flow.
  """
    if client_id_file:
        AssertClientSecretIsInstalledType(client_id_file)
    google_auth_flow = c_flow.CreateGoogleAuthFlow(scopes, client_id_file)
    return c_flow.RunGoogleAuthFlow(google_auth_flow, launch_browser)