Python google.appengine.api.users.User() Examples

The following are 30 code examples of google.appengine.api.users.User(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.appengine.api.users , or try the search function .
Example #1
Source File: appengine.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _build_state_value(request_handler, user):
    """Composes the value for the 'state' parameter.

    Packs the current request URI and an XSRF token into an opaque string that
    can be passed to the authentication server via the 'state' parameter.

    Args:
        request_handler: webapp.RequestHandler, The request.
        user: google.appengine.api.users.User, The current user.

    Returns:
        The state value as a string.
    """
    uri = request_handler.request.url
    token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
                                    action_id=str(uri))
    return uri + ':' + token 
Example #2
Source File: appengine.py    From sndlatr with Apache License 2.0 6 votes vote down vote up
def _parse_state_value(state, user):
  """Parse the value of the 'state' parameter.

  Parses the value and validates the XSRF token in the state parameter.

  Args:
    state: string, The value of the state parameter.
    user: google.appengine.api.users.User, The current user.

  Raises:
    InvalidXsrfTokenError: if the XSRF token is invalid.

  Returns:
    The redirect URI.
  """
  uri, token = state.rsplit(':', 1)
  if not xsrfutil.validate_token(xsrf_secret_key(), token, user.user_id(),
                                 action_id=uri):
    raise InvalidXsrfTokenError()

  return uri 
Example #3
Source File: appengine.py    From earthengine with MIT License 6 votes vote down vote up
def __init__(self, model, key_name, property_name, cache=None, user=None):
    """Constructor for Storage.

    Args:
      model: db.Model or ndb.Model, model class
      key_name: string, key name for the entity that has the credentials
      property_name: string, name of the property that is a CredentialsProperty
        or CredentialsNDBProperty.
      cache: memcache, a write-through cache to put in front of the datastore.
        If the model you are using is an NDB model, using a cache will be
        redundant since the model uses an instance cache and memcache for you.
      user: users.User object, optional. Can be used to grab user ID as a
        key_name if no key name is specified.
    """
    if key_name is None:
      if user is None:
        raise ValueError('StorageByKeyName called with no key name or user.')
      key_name = user.user_id()

    self._model = model
    self._key_name = key_name
    self._property_name = property_name
    self._cache = cache 
Example #4
Source File: appengine.py    From earthengine with MIT License 6 votes vote down vote up
def _build_state_value(request_handler, user):
  """Composes the value for the 'state' parameter.

  Packs the current request URI and an XSRF token into an opaque string that
  can be passed to the authentication server via the 'state' parameter.

  Args:
    request_handler: webapp.RequestHandler, The request.
    user: google.appengine.api.users.User, The current user.

  Returns:
    The state value as a string.
  """
  uri = request_handler.request.url
  token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
                                  action_id=str(uri))
  return  uri + ':' + token 
Example #5
Source File: appengine.py    From earthengine with MIT License 6 votes vote down vote up
def _parse_state_value(state, user):
  """Parse the value of the 'state' parameter.

  Parses the value and validates the XSRF token in the state parameter.

  Args:
    state: string, The value of the state parameter.
    user: google.appengine.api.users.User, The current user.

  Raises:
    InvalidXsrfTokenError: if the XSRF token is invalid.

  Returns:
    The redirect URI.
  """
  uri, token = state.rsplit(':', 1)
  if not xsrfutil.validate_token(xsrf_secret_key(), token, user.user_id(),
                                 action_id=uri):
    raise InvalidXsrfTokenError()

  return uri 
Example #6
Source File: users_id_token_test.py    From endpoints-python with Apache License 2.0 6 votes vote down vote up
def VerifyIdToken(self, cls, *args):
    with mock.patch.object(users_id_token, 'time') as mock_time,\
          mock.patch.object(users_id_token, '_get_id_token_user') as mock_get:
      mock_time.time.return_value = 1001
      mock_get.return_value = users.User('test@gmail.com')
      os.environ['HTTP_AUTHORIZATION'] = ('Bearer ' + self._SAMPLE_TOKEN)
      if args:
        cls.method(*args)
      else:
        users_id_token._maybe_set_current_user_vars(cls.method)
      mock_time.time.assert_called_once_with()
      mock_get.assert_called_once_with(
        self._SAMPLE_TOKEN,
        users_id_token._DEFAULT_GOOGLE_ISSUER,
        self._SAMPLE_AUDIENCES,
        (constants.API_EXPLORER_CLIENT_ID,) + self._SAMPLE_ALLOWED_CLIENT_IDS,
        1001,
        memcache,
      ) 
Example #7
Source File: appengine.py    From billing-export-python with Apache License 2.0 6 votes vote down vote up
def _build_state_value(request_handler, user):
  """Composes the value for the 'state' parameter.

  Packs the current request URI and an XSRF token into an opaque string that
  can be passed to the authentication server via the 'state' parameter.

  Args:
    request_handler: webapp.RequestHandler, The request.
    user: google.appengine.api.users.User, The current user.

  Returns:
    The state value as a string.
  """
  uri = request_handler.request.url
  token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
                                  action_id=str(uri))
  return  uri + ':' + token 
Example #8
Source File: appengine.py    From billing-export-python with Apache License 2.0 6 votes vote down vote up
def _parse_state_value(state, user):
  """Parse the value of the 'state' parameter.

  Parses the value and validates the XSRF token in the state parameter.

  Args:
    state: string, The value of the state parameter.
    user: google.appengine.api.users.User, The current user.

  Raises:
    InvalidXsrfTokenError: if the XSRF token is invalid.

  Returns:
    The redirect URI.
  """
  uri, token = state.rsplit(':', 1)
  if not xsrfutil.validate_token(xsrf_secret_key(), token, user.user_id(),
                                 action_id=uri):
    raise InvalidXsrfTokenError()

  return uri 
Example #9
Source File: appengine.py    From aqua-monitor with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _build_state_value(request_handler, user):
    """Composes the value for the 'state' parameter.

    Packs the current request URI and an XSRF token into an opaque string that
    can be passed to the authentication server via the 'state' parameter.

    Args:
        request_handler: webapp.RequestHandler, The request.
        user: google.appengine.api.users.User, The current user.

    Returns:
        The state value as a string.
    """
    uri = request_handler.request.url
    token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
                                    action_id=str(uri))
    return uri + ':' + token 
Example #10
Source File: appengine.py    From aqua-monitor with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _parse_state_value(state, user):
    """Parse the value of the 'state' parameter.

    Parses the value and validates the XSRF token in the state parameter.

    Args:
        state: string, The value of the state parameter.
        user: google.appengine.api.users.User, The current user.

    Raises:
        InvalidXsrfTokenError: if the XSRF token is invalid.

    Returns:
        The redirect URI.
    """
    uri, token = state.rsplit(':', 1)
    if not xsrfutil.validate_token(xsrf_secret_key(), token, user.user_id(),
                                   action_id=uri):
        raise InvalidXsrfTokenError()

    return uri 
Example #11
Source File: appengine.py    From googleapps-message-recall with Apache License 2.0 6 votes vote down vote up
def __init__(self, model, key_name, property_name, cache=None, user=None):
    """Constructor for Storage.

    Args:
      model: db.Model or ndb.Model, model class
      key_name: string, key name for the entity that has the credentials
      property_name: string, name of the property that is a CredentialsProperty
        or CredentialsNDBProperty.
      cache: memcache, a write-through cache to put in front of the datastore.
        If the model you are using is an NDB model, using a cache will be
        redundant since the model uses an instance cache and memcache for you.
      user: users.User object, optional. Can be used to grab user ID as a
        key_name if no key name is specified.
    """
    if key_name is None:
      if user is None:
        raise ValueError('StorageByKeyName called with no key name or user.')
      key_name = user.user_id()

    self._model = model
    self._key_name = key_name
    self._property_name = property_name
    self._cache = cache 
Example #12
Source File: appengine.py    From googleapps-message-recall with Apache License 2.0 6 votes vote down vote up
def _build_state_value(request_handler, user):
  """Composes the value for the 'state' parameter.

  Packs the current request URI and an XSRF token into an opaque string that
  can be passed to the authentication server via the 'state' parameter.

  Args:
    request_handler: webapp.RequestHandler, The request.
    user: google.appengine.api.users.User, The current user.

  Returns:
    The state value as a string.
  """
  uri = request_handler.request.url
  token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
                                  action_id=str(uri))
  return  uri + ':' + token 
Example #13
Source File: appengine.py    From googleapps-message-recall with Apache License 2.0 6 votes vote down vote up
def _parse_state_value(state, user):
  """Parse the value of the 'state' parameter.

  Parses the value and validates the XSRF token in the state parameter.

  Args:
    state: string, The value of the state parameter.
    user: google.appengine.api.users.User, The current user.

  Raises:
    InvalidXsrfTokenError: if the XSRF token is invalid.

  Returns:
    The redirect URI.
  """
  uri, token = state.rsplit(':', 1)
  if not xsrfutil.validate_token(xsrf_secret_key(), token, user.user_id(),
                                 action_id=uri):
    raise InvalidXsrfTokenError()

  return uri 
Example #14
Source File: auth_test.py    From loaner with Apache License 2.0 6 votes vote down vote up
def setUp(self):
    super(LoanerEndpointsTest, self).setUp()
    self.service = FakeApiPermissionChecks()
    self.request = message_types.VoidMessage()

    user_model.Role.create(
        'technical_admin',
        role_permissions=[permissions.Permissions.AUDIT_SHELF],
        associated_group=loanertest.TECHNICAL_ADMIN_EMAIL)

    user_model.User(
        id=loanertest.SUPER_ADMIN_EMAIL, superadmin=True).put()
    user_model.User(
        id=loanertest.TECHNICAL_ADMIN_EMAIL,
        roles=[ndb.Key(user_model.Role, 'technical_admin')]).put()
    user_model.User(id=loanertest.USER_EMAIL).put() 
Example #15
Source File: appengine.py    From alfred-gmail with MIT License 6 votes vote down vote up
def _parse_state_value(state, user):
    """Parse the value of the 'state' parameter.

    Parses the value and validates the XSRF token in the state parameter.

    Args:
        state: string, The value of the state parameter.
        user: google.appengine.api.users.User, The current user.

    Returns:
        The redirect URI, or None if XSRF token is not valid.
    """
    uri, token = state.rsplit(':', 1)
    if xsrfutil.validate_token(xsrf_secret_key(), token, user.user_id(),
                               action_id=uri):
        return uri
    else:
        return None 
Example #16
Source File: appengine.py    From alfred-gmail with MIT License 6 votes vote down vote up
def _build_state_value(request_handler, user):
    """Composes the value for the 'state' parameter.

    Packs the current request URI and an XSRF token into an opaque string that
    can be passed to the authentication server via the 'state' parameter.

    Args:
        request_handler: webapp.RequestHandler, The request.
        user: google.appengine.api.users.User, The current user.

    Returns:
        The state value as a string.
    """
    uri = request_handler.request.url
    token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
                                    action_id=str(uri))
    return uri + ':' + token 
Example #17
Source File: appengine.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _parse_state_value(state, user):
    """Parse the value of the 'state' parameter.

    Parses the value and validates the XSRF token in the state parameter.

    Args:
        state: string, The value of the state parameter.
        user: google.appengine.api.users.User, The current user.

    Raises:
        InvalidXsrfTokenError: if the XSRF token is invalid.

    Returns:
        The redirect URI.
    """
    uri, token = state.rsplit(':', 1)
    if not xsrfutil.validate_token(xsrf_secret_key(), token, user.user_id(),
                                   action_id=uri):
        raise InvalidXsrfTokenError()

    return uri 
Example #18
Source File: appengine.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _build_state_value(request_handler, user):
    """Composes the value for the 'state' parameter.

    Packs the current request URI and an XSRF token into an opaque string that
    can be passed to the authentication server via the 'state' parameter.

    Args:
        request_handler: webapp.RequestHandler, The request.
        user: google.appengine.api.users.User, The current user.

    Returns:
        The state value as a string.
    """
    uri = request_handler.request.url
    token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
                                    action_id=str(uri))
    return uri + ':' + token 
Example #19
Source File: appengine.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _build_state_value(request_handler, user):
    """Composes the value for the 'state' parameter.

    Packs the current request URI and an XSRF token into an opaque string that
    can be passed to the authentication server via the 'state' parameter.

    Args:
        request_handler: webapp.RequestHandler, The request.
        user: google.appengine.api.users.User, The current user.

    Returns:
        The state value as a string.
    """
    uri = request_handler.request.url
    token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
                                    action_id=str(uri))
    return uri + ':' + token 
Example #20
Source File: appengine.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _parse_state_value(state, user):
    """Parse the value of the 'state' parameter.

    Parses the value and validates the XSRF token in the state parameter.

    Args:
        state: string, The value of the state parameter.
        user: google.appengine.api.users.User, The current user.

    Raises:
        InvalidXsrfTokenError: if the XSRF token is invalid.

    Returns:
        The redirect URI.
    """
    uri, token = state.rsplit(':', 1)
    if not xsrfutil.validate_token(xsrf_secret_key(), token, user.user_id(),
                                   action_id=uri):
        raise InvalidXsrfTokenError()

    return uri 
Example #21
Source File: appengine.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _build_state_value(request_handler, user):
    """Composes the value for the 'state' parameter.

    Packs the current request URI and an XSRF token into an opaque string that
    can be passed to the authentication server via the 'state' parameter.

    Args:
        request_handler: webapp.RequestHandler, The request.
        user: google.appengine.api.users.User, The current user.

    Returns:
        The state value as a string.
    """
    uri = request_handler.request.url
    token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
                                    action_id=str(uri))
    return uri + ':' + token 
Example #22
Source File: appengine.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _build_state_value(request_handler, user):
    """Composes the value for the 'state' parameter.

    Packs the current request URI and an XSRF token into an opaque string that
    can be passed to the authentication server via the 'state' parameter.

    Args:
        request_handler: webapp.RequestHandler, The request.
        user: google.appengine.api.users.User, The current user.

    Returns:
        The state value as a string.
    """
    uri = request_handler.request.url
    token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
                                    action_id=str(uri))
    return uri + ':' + token 
Example #23
Source File: appengine.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _parse_state_value(state, user):
    """Parse the value of the 'state' parameter.

    Parses the value and validates the XSRF token in the state parameter.

    Args:
        state: string, The value of the state parameter.
        user: google.appengine.api.users.User, The current user.

    Raises:
        InvalidXsrfTokenError: if the XSRF token is invalid.

    Returns:
        The redirect URI.
    """
    uri, token = state.rsplit(':', 1)
    if not xsrfutil.validate_token(xsrf_secret_key(), token, user.user_id(),
                                   action_id=uri):
        raise InvalidXsrfTokenError()

    return uri 
Example #24
Source File: appengine.py    From twitter-for-bigquery with Apache License 2.0 6 votes vote down vote up
def __init__(self, model, key_name, property_name, cache=None, user=None):
    """Constructor for Storage.

    Args:
      model: db.Model or ndb.Model, model class
      key_name: string, key name for the entity that has the credentials
      property_name: string, name of the property that is a CredentialsProperty
        or CredentialsNDBProperty.
      cache: memcache, a write-through cache to put in front of the datastore.
        If the model you are using is an NDB model, using a cache will be
        redundant since the model uses an instance cache and memcache for you.
      user: users.User object, optional. Can be used to grab user ID as a
        key_name if no key name is specified.
    """
    if key_name is None:
      if user is None:
        raise ValueError('StorageByKeyName called with no key name or user.')
      key_name = user.user_id()

    self._model = model
    self._key_name = key_name
    self._property_name = property_name
    self._cache = cache 
Example #25
Source File: appengine.py    From twitter-for-bigquery with Apache License 2.0 6 votes vote down vote up
def _build_state_value(request_handler, user):
  """Composes the value for the 'state' parameter.

  Packs the current request URI and an XSRF token into an opaque string that
  can be passed to the authentication server via the 'state' parameter.

  Args:
    request_handler: webapp.RequestHandler, The request.
    user: google.appengine.api.users.User, The current user.

  Returns:
    The state value as a string.
  """
  uri = request_handler.request.url
  token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
                                  action_id=str(uri))
  return  uri + ':' + token 
Example #26
Source File: appengine.py    From twitter-for-bigquery with Apache License 2.0 6 votes vote down vote up
def _parse_state_value(state, user):
  """Parse the value of the 'state' parameter.

  Parses the value and validates the XSRF token in the state parameter.

  Args:
    state: string, The value of the state parameter.
    user: google.appengine.api.users.User, The current user.

  Raises:
    InvalidXsrfTokenError: if the XSRF token is invalid.

  Returns:
    The redirect URI.
  """
  uri, token = state.rsplit(':', 1)
  if not xsrfutil.validate_token(xsrf_secret_key(), token, user.user_id(),
                                 action_id=uri):
    raise InvalidXsrfTokenError()

  return uri 
Example #27
Source File: datastore_types.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def PropertyTypeName(value):
  """Returns the name of the type of the given property value, as a string.

  Raises BadValueError if the value is not a valid property type.

  Args:
    value: any valid property value

  Returns:
    string
  """
  if value.__class__ in _PROPERTY_MEANINGS:
    meaning = _PROPERTY_MEANINGS[value.__class__]
    name = entity_pb.Property._Meaning_NAMES[meaning]
    return name.lower().replace('_', ':')
  elif isinstance(value, basestring):
    return 'string'
  elif isinstance(value, users.User):
    return 'user'
  elif isinstance(value, long):
    return 'int'
  elif value is None:
    return 'null'
  else:
    return typename(value).lower() 
Example #28
Source File: oauth_api.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def get_current_user(_scope=None):
  """Returns the User on whose behalf the request was made.

  Args:
    _scope: The custom OAuth scope or an iterable of scopes at least one of
      which is accepted.

  Returns:
    User

  Raises:
    OAuthRequestError: The request was not a valid OAuth request.
    OAuthServiceFailureError: An unknown error occurred.
  """

  _maybe_call_get_oauth_user(_scope)
  return _get_user_from_environ() 
Example #29
Source File: mail.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def invalid_email_reason(email_address, field):
  """Determines the reason why an email is invalid.

  Args:
    email_address: Email address to check.
    field: Field that is invalid.

  Returns:
    A string that indicates the reason why an email is invalid; otherwise
    returns `None`.
  """
  if email_address is None:
    return 'None email address for %s.' % field

  if isinstance(email_address, users.User):
    email_address = email_address.email()
  if not isinstance(email_address, basestring):
    return 'Invalid email address type for %s.' % field
  stripped_address = email_address.strip()
  if not stripped_address:
    return 'Empty email address for %s.' % field
  return None 
Example #30
Source File: test_utils.py    From upvote with Apache License 2.0 6 votes vote down vote up
def CreateVote(blockable, **kwargs):
  """Creates a Vote for the given Blockable.

  Args:
    blockable: The Blockable to vote for.
    **kwargs: dict, Any Vote properties to customize.

  Returns:
    The newly-created Vote entity.
  """
  defaults = {
      'user_email': RandomEmail(),
      'weight': 1,
      'was_yes_vote': True,
      'candidate_type': constants.RULE_TYPE.BINARY
  }
  defaults.update(kwargs)

  vote = vote_models.Vote(**defaults)
  vote.key = vote_models.Vote.GetKey(
      blockable.key, ndb.Key(user_models.User, defaults['user_email']))
  vote.put()
  return vote