restauth_user - user handling

The restauth_user module includes all code related to user management. You can use one of the factory methods (get(), get_all() or create()) to retreive an instance or a list of instances of the User class.

The factory methods make sure that the User object used represents a user that actually exists in the RestAuth service by verifying the existance for returning the respective instance(s). If performance is critical, however, it is better to instantiate an instance directly, perform the desired operations on that object and catch the case of a non-existing user with an exception handler.

from RestAuthClient import common, restauth_user
conn = common.RestAuthConnection( 'https://auth.example.com', 'service', 'password' )

# this is two requests:
user = restauth_user.get( 'username' ) # does one request
user.verify_password( 'password' )

# this is just one request:
user = restauth_user.User( 'username' ) # does no request
user.verify_password( 'password' )

API documentation

Module handling code relevant to user authentication and property management.

class RestAuthClient.restauth_user.User(conn, name)[source]

An instance of this class is an object oriented abstraction of a user in a RestAuth server.

Warning

The constructor does not verify that the user actually exists. This has the advantage of saving one request to the RestAuth service. If you want to be sure that a user exists, use get() or get_all().

Parameters:
  • conn (RestAuthConnection) – The connection to the RestAuthServer.
  • name (str) – The name of this user.
add_group(grp)[source]

Make this user a member if the given group.

This method is just a shortcut for Group.add_user().

Parameters:

grp (str or Group) – The group of interest.

Raises:
  • BadRequest – If the server was unable to parse the request body.
  • Unauthorized – When the connection uses wrong credentials.
  • ResourceNotFound – If the user or group does not exist.
  • InternalServerError – When the RestAuth server returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.
create_property(prop, value)[source]

Create a new property for this user. This method fails if the property already exists. Use set_property() if you do not care if the property already exists.

Parameters:
  • prop (str) – The property to set.
  • value (str) – The new value of the property
Raises:
  • BadRequest – If the server was unable to parse the request body.
  • Unauthorized – When the connection uses wrong credentials.
  • ResourceNotFound – If the user does not exist in RestAuth.
  • PropertyExists – When the property already exists
  • PreconditionFailed – When the propertyname contains invalid characters
  • UnsupportedMediaType – The server does not support the content type used by this connection (see also: set_content_handler()).
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.
create_property_test(prop, value)[source]

Do a test-run on creating a property (i.e. to test user input against the RestAuth server configuration). This method throws the exact same Exceptions as create_property() and also returns None if creating the property would succeed.

Note

Invoking this method cannot guarantee that actually creating this property will work in the future, i.e. it may have been created by another client in the meantime.

get_groups()[source]

Get all groups that this user is a member of.

This method is just a shortcut for group.get_all().

Returns:

All groups that the user is a member of.

Return type:

list of groups

Raises:
  • Unauthorized – When the connection uses wrong credentials.
  • ResourceNotFound – If the user does not exist.
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.
get_properties()[source]

Get all properties defined for this user.

Returns:

A key/value array of the properties defined for this user.

Return type:

dict

Raises:
  • Unauthorized – When the connection uses wrong credentials.
  • NotAcceptable – When the server cannot generate a response in the content type used by this connection (see also: set_content_handler()).
  • ResourceNotFound – If the user does not exist in RestAuth.
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.
get_property(prop)[source]

Get the given property for this user.

Returns:

The value of the property.

Return type:

str

Raises:
  • Unauthorized – When the connection uses wrong credentials.
  • ResourceNotFound – If the user or property does not exist.
  • NotAcceptable – When the server cannot generate a response in the content type used by this connection (see also: set_content_handler()).
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.
in_group(grp)[source]

Check if the user is a member in the given group.

This method is just a shortcut for Group.is_member().

Parameters:

grp (str or Group) – The group of interest.

Returns:

True if this user is a member, False otherwise.

Return type:

bool

Raises:
  • Unauthorized – When the connection uses wrong credentials.
  • ResourceNotFound – If the user or group does not exist.
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.
remove()[source]

Remove this user.

Raises:
  • Unauthorized – When the connection uses wrong credentials.
  • ResourceNotFound – If the user does not exist in RestAuth.
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.
remove_group(grp)[source]

Remove the users membership from the given group.

This method is just a shortcut for Group.remove_user().

Parameters:

grp (str or Group) – The group of interest.

Raises:
  • Unauthorized – When the connection uses wrong credentials.
  • ResourceNotFound – If the user or group does not exist.
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.
remove_property(prop)[source]

Delete the given property.

Raises:
  • Unauthorized – When the connection uses wrong credentials.
  • ResourceNotFound – If the user or property does not exist.
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.
set_password(password=None)[source]

Set the password of this user.

Parameters:

password (str) – The new password of the user. If None or an empty string, the user is effectively disabled.

Raises:
  • BadRequest – If the server was unable to parse the request body.
  • Unauthorized – When the connection uses wrong credentials.
  • ResourceNotFound – If the user does not exist in RestAuth.
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnsupportedMediaType – The server does not support the content type used by this connection (see also: set_content_handler()).
  • UnknownStatus – If the response status is unknown.
  • PreconditionFailed – When the password is invalid.
set_property(prop, value)[source]

Set a property for this user. This method overwrites any previous entry.

Parameters:
  • prop (str) – The property to set.
  • value (str) – The new value of the property
Raises:
  • BadRequest – If the server was unable to parse the request body.
  • Unauthorized – When the connection uses wrong credentials.
  • ResourceNotFound – If the user does not exist in RestAuth.
  • NotAcceptable – When the server cannot generate a response in the content type used by this connection (see also: set_content_handler()).
  • UnsupportedMediaType – The server does not support the content type used by this connection (see also: set_content_handler()).
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.
verify_password(password)[source]

Verify the given password.

Parameters:

password (str) – The password to verify.

Returns:

True if the password is correct, False if the password is wrong or the user does not exist.

Return type:

bool

Raises:
  • BadRequest – If the server was unable to parse the request body.
  • Unauthorized – When the connection uses wrong credentials.
  • UnsupportedMediaType – The server does not support the content type used by this connection (see also: set_content_handler()).
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.
RestAuthClient.restauth_user.create(conn, name, password=None, properties=None)[source]

Factory method that creates a new user in the RestAuth database.

Parameters:
  • conn (RestAuthConnection) – A connection to a RestAuth service.
  • name (str) – Name of the user to get
  • password (str) – Password for the new user. If None or an empty string, the user is effectively disabled.
Returns:

The user object representing the user just created.

Return type:

User

Raises:
  • BadRequest – If the server was unable to parse the request body.
  • Unauthorized – When the connection uses wrong credentials.
  • UserExists – If the user already exists.
  • PreconditionFailed – When username or password is invalid.
  • UnsupportedMediaType – The server does not support the content type used by this connection (see also: set_content_handler()).
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.
RestAuthClient.restauth_user.create_test(conn, name, password=None, properties=None)[source]

Do a test-run on creating a new user (i.e. to test user input against the RestAuth server configuration). This method throws the exact same Exceptions as create() but always returns None instead of a User instance if the user could be created that way.

Note

Invoking this method cannot guarantee that actually creating this user will work in the future, i.e. it may have been created by another client in the meantime.

RestAuthClient.restauth_user.get(conn, name)[source]

Factory method that gets an existing user from RestAuth. This method verifies that the user exists in RestAuth and throws ResourceNotFound if not.

Parameters:
  • conn (RestAuthConnection) – A connection to a RestAuth service.
  • name (str) – Name of the user to get
Returns:

The user object representing the user just created.

Return type:

User

Raises:
  • Unauthorized – When the connection uses wrong credentials.
  • ResourceNotFound – If the user does not exist in RestAuth.
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.
RestAuthClient.restauth_user.get_all(conn)[source]

Factory method that gets all users known to RestAuth.

Parameters:

conn (RestAuthConnection) – A connection to a RestAuth service.

Returns:

A list of User objects

Return type:

[User]

Raises:
  • Unauthorized – When the connection uses wrong credentials.
  • NotAcceptable – When the server cannot generate a response in the content type used by this connection (see also: set_content_handler()).
  • InternalServerError – When the RestAuth service returns HTTP status code 500
  • UnknownStatus – If the response status is unknown.

Table Of Contents

Previous topic

common - Common code used by other classes

Next topic

group - group handling

This Page