Example #1
0
    def DoWebFlow(self, launch_browser):
        """Launches a browser to get credentials."""
        # pylint:disable=g-import-not-at-top, Import when needed for performance.
        import webbrowser
        try:
            # Sometimes it's not possible to launch the web browser. This often
            # happens when people ssh into other machines.
            if launch_browser:
                if c_gce.Metadata().connected:
                    launch_browser = False
                try:
                    browser = webbrowser.get()
                    if (hasattr(browser, 'name')
                            and browser.name in _WEBBROWSER_NAMES_BLACKLIST):
                        launch_browser = False
                except webbrowser.Error:
                    launch_browser = False

            return c_store.AcquireFromWebFlow(launch_browser=launch_browser)
        except c_store.FlowError:
            msg = 'There was a problem with web authentication.'
            if launch_browser:
                msg += ' Try running again with --no-launch-browser.'
            log.error(msg)
            raise
Example #2
0
def DoInstalledAppBrowserFlow(client_id_file, scopes, launch_browser):
  """Launches a browser to get credentials."""
  try:
    if client_id_file is not None:
      client_type = GetClientSecretsType(client_id_file)
      if client_type != clientsecrets.TYPE_INSTALLED:
        raise c_exc.ToolException(
            'Only client IDs of type \'%s\' are allowed, but encountered '
            'type \'%s\'' % (clientsecrets.TYPE_INSTALLED, client_type))
      return c_store.AcquireFromWebFlowAndClientIdFile(
          client_id_file=client_id_file,
          scopes=scopes,
          launch_browser=launch_browser)
    else:
      return c_store.AcquireFromWebFlow(
          launch_browser=launch_browser,
          scopes=scopes,
          client_id=DEFAULT_CREDENTIALS_DEFAULT_CLIENT_ID,
          client_secret=DEFAULT_CREDENTIALS_DEFAULT_CLIENT_SECRET)
  except c_store.FlowError:
    msg = 'There was a problem with web authentication.'
    if launch_browser:
      msg += ' Try running again with --no-launch-browser.'
    log.error(msg)
    raise
Example #3
0
 def DoInstalledAppBrowserFlow(self, launch_browser):
   """Launches a browser to get credentials."""
   try:
     return c_store.AcquireFromWebFlow(launch_browser=launch_browser)
   except c_store.FlowError:
     msg = 'There was a problem with web authentication.'
     if launch_browser:
       msg += ' Try running again with --no-launch-browser.'
     log.error(msg)
     raise
Example #4
0
def DoInstalledAppBrowserFlow(launch_browser,
                              scopes,
                              client_id_file=None,
                              client_id=None,
                              client_secret=None):
    """Launches a browser to get credentials.

  Args:
    launch_browser: bool, True to do a browser flow, false to allow the user to
      type in a token from a different 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.
    client_id: str, An alternate client id to use.  This is ignored if you give
      a client id file.  If None, the default client id for the Cloud SDK is
      used.
    client_secret: str, The secret to go along with client_id if specified.

  Returns:
    The clients obtained from the web flow.
  """
    try:
        if client_id_file:
            client_type = GetClientSecretsType(client_id_file)
            if client_type != clientsecrets.TYPE_INSTALLED:
                raise InvalidClientSecretsError(
                    'Only client IDs of type \'%s\' are allowed, but encountered '
                    'type \'%s\'' %
                    (clientsecrets.TYPE_INSTALLED, client_type))
            webflow = client.flow_from_clientsecrets(filename=client_id_file,
                                                     scope=scopes)
            return c_store.RunWebFlow(webflow, launch_browser=launch_browser)
        else:
            return c_store.AcquireFromWebFlow(launch_browser=launch_browser,
                                              scopes=scopes,
                                              client_id=client_id,
                                              client_secret=client_secret)
    except c_store.FlowError:
        msg = 'There was a problem with web authentication.'
        if launch_browser:
            msg += ' Try running again with --no-launch-browser.'
        log.error(msg)
        raise
Example #5
0
def DoInstalledAppBrowserFlow(launch_browser,
                              scopes,
                              client_id_file=None,
                              client_id=None,
                              client_secret=None):
    """Launches a browser to get credentials.

  Args:
    launch_browser: bool, True to do a browser flow, false to allow the user to
      type in a token from a different 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.
    client_id: str, An alternate client id to use.  This is ignored if you give
      a client id file.  If None, the default client id for the Cloud SDK is
      used.
    client_secret: str, The secret to go along with client_id if specified.

  Returns:
    The clients obtained from the web flow.
  """
    try:
        if client_id_file:
            AssertClientSecretIsInstalledType(client_id_file)
            webflow = client.flow_from_clientsecrets(filename=client_id_file,
                                                     scope=scopes)
            return c_store.RunWebFlow(webflow, launch_browser=launch_browser)
        else:
            return c_store.AcquireFromWebFlow(launch_browser=launch_browser,
                                              scopes=scopes,
                                              client_id=client_id,
                                              client_secret=client_secret)
    except c_store.FlowError 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
Example #6
0
    def DoWebFlow(self, launch_browser):
        """Launches a browser to get credentials."""
        try:
            # Sometimes it's not possible to launch the web browser. This often
            # happens when people ssh into other machines.
            if launch_browser:
                if c_gce.Metadata().connected:
                    launch_browser = False
                try:
                    browser = webbrowser.get()
                    if (hasattr(browser, 'name')
                            and browser.name in _WEBBROWSER_NAMES_BLACKLIST):
                        launch_browser = False
                except webbrowser.Error:
                    launch_browser = False

            return c_store.AcquireFromWebFlow(launch_browser=launch_browser)
        except c_store.FlowError:
            log.error(
                ('There was a problem with the web flow. Try running with '
                 '--no-launch-browser'))
            raise