The objective of security is protection of information from theft or corruption, while allowing the information to remain accessible to its intended users.
Ticket is a short packet of bytes generated by a network server for a client, which can be delivered to itself as a means of authentication or proof of authorization, and cannot easily be forged.
Ticket has the following characteristics:
Ticket can be instantiated by passing the following arguments:
Keys used for validation and encryption are ensured to be at least of 320 bits length. The ensure_strong_key() function appends HMAC signature to the key.
If the cryptography library is not available you will see a warning message:
Ticket: cypher not available
Although Ticket continues to function even cryptography library is not installed it strongly recommended to use cryptography in a production environment.
Ticket does not alter it state once initialized. It is guaranteed to be thread safe.
Here is typical use case when all possible configuration attributes are used:
from wheezy.security.crypto.comp import aes192
from wheezy.security.crypto.comp import sha1
from wheezy.security.crypto import Ticket
options = {
'CRYPTO_VALIDATION_KEY': 'LkLlYR5WbTk54kaIgJOp',
'CRYPTO_ENCRYPTION_KEY': 'rH64daeXBZdgrR7WNawf'
}
ticket = Ticket(
max_age=1200,
salt='CzQnV0KazDKElBYiIC2w',
digestmod=sha1,
cypher=aes192,
options=options)
The ticket instance can be shared application wide. The encode / decode methods are used in the following way:
protected_value = ticket.encode('hello')
assert 'hello' == ticket.decode(protected_value)
In case the validity of a ticket cannot be confirmed, the decode method returns None.
Ticket cypher can be any callable that satisfies the following contract:
Principal is a container of user specific security information. It includes the following attributes:
Here is a sample how to instantiate new Principal:
principal = Principal(
id='125134788',
roles=['user'],
alias='John Smith')
Principal supports the following methods:
You can use Ticket to securely pass Principal across network boundaries. Combining them both you can introduce an authentication/authorization cookie to your application.
Authorization specifies access rights to resources and provides access control in particular to your application.
You are able to request authorization by decorating your method with authorized(). Here is a typical use case:
from wheezy.security import authorized
class MyBusinessLogic(object):
principal = None
@authorized
def cancel_transfer(self, id):
return True
@authorized(roles=('operator',))
def approve_transfer(self):
return True
Note that the authorized() decorator requires the object to supply a principal attribute of type Principal.
If a caller is not authorized to perform a requested operation, a SecurityError exception is raised. See authorized() for more details.