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.
Returns a user object based on a auth_id.
| Parameters: |
|
|---|---|
| Returns: | A user object. |
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. |
Returns a user object, validating password.
| Parameters: |
|
|---|---|
| Returns: | A user object, if found and password matches. |
| Raises : | auth.InvalidAuthIdError or auth.InvalidPasswordError. |
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. |
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.
Returns a token key.
| Parameters: |
|
|---|---|
| Returns: | model.Key containing a string id in the following format: {user_id}.{subject}.{token}. |
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: |
|
|---|---|
| Returns: | True if the unique value was created, False otherwise. |
Creates multiple unique values at once.
| Parameters: |
|
|---|---|
| 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. |