Esempio n. 1
0
def ensure_logged_in(server_url):
  """Checks that user is logged in, asking to do it if not.

  Raises:
    ValueError if the server_url is not acceptable.
  """
  # It's just a waste of time on a headless bot (it can't do interactive login).
  if tools.is_headless() or net.get_oauth_config().disabled:
    return None
  server_url = server_url.lower().rstrip('/')
  allowed = (
      'https://',
      'http://localhost:', 'http://127.0.0.1:', 'http://::1:')
  if not server_url.startswith(allowed):
    raise ValueError('URL must start with https:// or be http:// to localhost')
  service = AuthService(server_url)
  try:
    service.login(False)
  except IOError:
    raise ValueError('Failed to contact %s' % server_url)
  try:
    identity = service.get_current_identity()
  except AuthServiceError:
    raise ValueError('Failed to fetch identify from %s' % server_url)
  if identity == 'anonymous:anonymous':
    raise ValueError(
        'Please login to %s: \n'
        '  python auth.py login --service=%s' % (server_url, server_url))
  email = identity.split(':')[1]
  logging.info('Logged in to %s: %s', server_url, email)
  return email
Esempio n. 2
0
def ensure_logged_in(server_url):
    """Checks that user is logged in, asking to do it if not.

  Raises:
    ValueError if the server_url is not acceptable.
  """
    # It's just a waste of time on a headless bot (it can't do interactive login).
    if tools.is_headless() or net.get_oauth_config().disabled:
        return None
    server_url = normalize_host_url(server_url)
    service = AuthService(server_url)
    try:
        service.login(False)
    except IOError:
        raise ValueError('Failed to contact %s' % server_url)
    try:
        identity = service.get_current_identity()
    except AuthServiceError:
        raise ValueError('Failed to fetch identify from %s' % server_url)
    if identity == 'anonymous:anonymous':
        raise ValueError('Please login to %s: \n'
                         '  python auth.py login --service=%s' %
                         (server_url, server_url))
    email = identity.split(':')[1]
    logging.info('Logged in to %s: %s', server_url, email)
    return email
Esempio n. 3
0
def ensure_logged_in(server_url):
  """Checks that user is logged in, asking to do it if not.

  Raises:
    ValueError if the server_url is not acceptable.
  """
  # It's just a waste of time on a headless bot (it can't do interactive login).
  if tools.is_headless() or net.get_oauth_config().disabled:
    return None
  server_url = normalize_host_url(server_url)
  service = AuthService(server_url)
  try:
    service.login(False)
  except IOError:
    raise ValueError('Failed to contact %s' % server_url)
  try:
    identity = service.get_current_identity()
  except AuthServiceError:
    raise ValueError('Failed to fetch identify from %s' % server_url)
  if identity == 'anonymous:anonymous':
    raise ValueError(
        'Please login to %s: \n'
        '  python auth.py login --service=%s' % (server_url, server_url))
  email = identity.split(':')[1]
  logging.info('Logged in to %s: %s', server_url, email)
  return email