A collection of user related models for Auth.
Warning
This is an experimental module. The API is subject to changes.
Stores user authentication credentials or authorization ids.
Creates a new authorization token for a given user ID.
Parameters: | user_id – User unique ID. |
---|---|
Returns: | A string with the authorization token. |
Creates a new user.
Parameters: |
|
---|---|
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. |
Deletes a given authorization token.
Parameters: |
|
---|
Returns a user object based on a auth_id.
Parameters: | auth_id – String representing a unique id for the user. Examples:
|
---|---|
Returns: | A user object. |
Returns a user object, validating password.
Parameters: |
|
---|---|
Returns: | A user object, if found and password matches. |
Raises: | auth.InvalidAuthIdError or auth.InvalidPasswordError. |
Returns a user object based on a user ID and token.
Parameters: |
|
---|---|
Returns: | A tuple (User, timestamp), with a user object and the token timestamp, or (None, None) if both were not found. |
Checks for existence of a token, given user_id, subject and token.
Parameters: |
|
---|---|
Returns: | A UserToken or None if the token does not exist. |
Stores validation tokens for users.
Creates a new token for the given user.
Parameters: |
|
---|---|
Returns: | The newly created UserToken. |
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
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. |
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. |