User Models

A collection of user related models for Auth.

Warning

This is an experimental module. The API is subject to changes.

class webapp2_extras.appengine.auth.models.User(*args, **kwds)[source]

Stores user authentication credentials or authorization ids.

classmethod get_by_auth_id(auth_id)[source]

Returns a user object based on a auth_id.

Parameters:
  • auth_id

    String representing a unique id for the user. Examples:

    • own:username
    • google:username
Returns:

A user object.

classmethod get_by_auth_token(user_id, token)[source]

Returns a user object based on a user ID and token.

Parameters:
  • user_id – The user_id of the requesting user.
  • token – The token string to be verified.
Returns:

A tuple (User, timestamp), with a user object and the token timestamp, or (None, None) if both were not found.

classmethod get_by_auth_password(auth_id, password)[source]

Returns a user object, validating password.

Parameters:
  • auth_id – Authentication id.
  • password – Password to be checked.
Returns:

A user object, if found and password matches.

Raises :

auth.InvalidAuthIdError or auth.InvalidPasswordError.

classmethod create_user(auth_id, unique_properties=None, **user_values)[source]

Creates a new user.

Parameters:
  • auth_id

    A string that is unique to the user. Users may have multiple auth ids. Example auth ids:

    • own:username
    • own:email@example.com
    • google:username
    • yahoo:username

    The value of auth_id must be unique.

  • unique_properties – Sequence of extra property names that must be unique.
  • user_values – Keyword arguments to create a new user entity. Since the model is an Expando, any provided custom properties will be saved. To hash a plain password, pass a keyword password_raw.
Returns:

A tuple (boolean, info). The boolean indicates if the user was created. If creation succeeds, info is the user entity; otherwise it is a list of duplicated unique properties that caused creation to fail.

classmethod validate_token(user_id, subject, token)[source]

Checks for existence of a token, given user_id, subject and token.

Parameters:
  • user_id – User unique ID.
  • subject

    The subject of the key. Examples:

    • ‘auth’
    • ‘signup’
  • token – The token string to be validated.
Returns:

A UserToken or None if the token does not exist.

classmethod create_auth_token(user_id)[source]

Creates a new authorization token for a given user ID.

Parameters:
  • user_id – User unique ID.
Returns:

A string with the authorization token.

classmethod delete_auth_token(user_id, token)[source]

Deletes a given authorization token.

Parameters:
  • user_id – User unique ID.
  • token – A string with the authorization token.
class webapp2_extras.appengine.auth.models.UserToken(*args, **kwds)[source]

Stores validation tokens for users.

classmethod get_key(user, subject, token)[source]

Returns a token key.

Parameters:
  • user – User unique ID.
  • subject

    The subject of the key. Examples:

    • ‘auth’
    • ‘signup’
  • token – Randomly generated token.
Returns:

model.Key containing a string id in the following format: {user_id}.{subject}.{token}.

classmethod create(user, subject, token=None)[source]

Creates a new token for the given user.

Parameters:
  • user – User unique ID.
  • subject

    The subject of the key. Examples:

    • ‘auth’
    • ‘signup’
  • token – Optionally an existing token may be provided. If None, a random token will be generated.
Returns:

The newly created UserToken.

classmethod get(user=None, subject=None, token=None)[source]

Fetches a user token.

Parameters:
  • user – User unique ID.
  • subject

    The subject of the key. Examples:

    • ‘auth’
    • ‘signup’
  • token – The existing token needing verified.
Returns:

A UserToken or None if the token does not exist.

class webapp2_extras.appengine.auth.models.Unique(*args, **kwds)[source]

A model to store unique values.

The only purpose of this model is to “reserve” values that must be unique within a given scope, as a workaround because datastore doesn’t support the concept of uniqueness for entity properties.

For example, suppose we have a model User with three properties that must be unique across a given group: username, auth_id and email:

class User(model.Model):
    username = model.StringProperty(required=True)
    auth_id = model.StringProperty(required=True)
    email = model.StringProperty(required=True)

To ensure property uniqueness when creating a new User, we first create Unique records for those properties, and if everything goes well we can save the new User record:

@classmethod
def create_user(cls, username, auth_id, email):
    # Assemble the unique values for a given class and attribute scope.
    uniques = [
        'User.username.%s' % username,
        'User.auth_id.%s' % auth_id,
        'User.email.%s' % email,
    ]

    # Create the unique username, auth_id and email.
    success, existing = Unique.create_multi(uniques)

    if success:
        # The unique values were created, so we can save the user.
        user = User(username=username, auth_id=auth_id, email=email)
        user.put()
        return user
    else:
        # At least one of the values is not unique.
        # Make a list of the property names that failed.
        props = [name.split('.', 2)[1] for name in uniques]
        raise ValueError('Properties %r are not unique.' % props)

Based on the idea from http://goo.gl/pBQhB

classmethod create(value)[source]

Creates a new unique value.

Parameters:
  • value

    The value to be unique, as a string.

    The value should include the scope in which the value must be unique (ancestor, namespace, kind and/or property name).

    For example, for a unique property email from kind User, the value can be User.email:me@myself.com. In this case User.email is the scope, and me@myself.com is the value to be unique.

Returns:

True if the unique value was created, False otherwise.

classmethod create_multi(values)[source]

Creates multiple unique values at once.

Parameters:
  • values – A sequence of values to be unique. See create().
Returns:

A tuple (bool, list_of_keys). If all values were created, bool is True and list_of_keys is empty. If one or more values weren’t created, bool is False and the list contains all the values that already existed in datastore during the creation attempt.

classmethod delete_multi(values)[source]

Deletes multiple unique values at once.

Parameters:
  • values – A sequence of values to be deleted.

Previous topic

webapp2

Next topic

Memcache sessions

This Page