cas_server.utils module

Some util function for the app

cas_server.utils.logger = <logging.Logger object>

logger facility


Encode a python object to json


Function that add somes variable to the context before template rendering

Parameters:params (dict) – The context dictionary used to render templates.
Returns:The params dictionary with the key settings set to django.conf.settings.
Return type:dict
cas_server.utils.json_response(request, data)[source]

Wrapper dumping data to a json and sending it to the user with an HttpResponse

  • request (django.http.HttpRequest) – The request object used to generate this response.
  • data (dict) – The python dictionnary to return as a json

The content of data serialized in json

Return type:



transform a python dotted path to the attr

Parameters:path (unicode or anything) – A dotted path to a python object or a python object
Returns:The python object pointed by the dotted path or the python object unchanged
cas_server.utils.redirect_params(url_name, params=None)[source]

Redirect to url_name with params as querystring

  • url_name (unicode) – a URL pattern name
  • params (dict or NoneType) – Some parameter to append to the reversed URL

A redirection to the URL with name url_name with params as querystring.

Return type:


cas_server.utils.reverse_params(url_name, params=None, **kwargs)[source]

compute the reverse url of url_name and add to it parameters from params as querystring

  • url_name (unicode) – a URL pattern name
  • params (dict or NoneType) – Some parameter to append to the reversed URL
  • **kwargs

    additional parameters needed to compure the reverse URL


The computed reverse URL of url_name with possible querystring from params

Return type:


cas_server.utils.copy_params(get_or_post_params, ignore=None)[source]

copy a django.http.QueryDict in a dict ignoring keys in the set ignore


A copy of get_or_post_params

Return type:


Set the cookie key on response with value value valid for max_age secondes

  • response (django.http.HttpResponse) – a django response where to set the cookie
  • key (unicode) – the cookie key
  • value (unicode) – the cookie value
  • max_age (int) – the maximum validity age of the cookie
cas_server.utils.get_current_url(request, ignore_params=None)[source]

Giving a django request, return the current http url, possibly ignoring some GET parameters

  • request (django.http.HttpRequest) – The current request object.
  • ignore_params (set) – An optional set of GET parameters to ignore

The URL of the current page, possibly omitting some parameters from ignore_params in the querystring.

Return type:


cas_server.utils.update_url(url, params)[source]

update parameters using params in the url query string

  • url (unicode or str) – An URL possibily with a querystring
  • params (dict) – A dictionary of parameters for updating the url querystring

The URL with an updated querystring

Return type:



If exception are stacked, return the first one

Parameters:error – A python exception with possible exception embeded within
Returns:A python exception with no exception embeded within

Generate a Login Ticket

Returns:A ticket with prefix settings.CAS_LOGIN_TICKET_PREFIX and length settings.CAS_LT_LEN
Return type:unicode

Generate a Service Ticket

Returns:A ticket with prefix settings.CAS_SERVICE_TICKET_PREFIX and length settings.CAS_ST_LEN
Return type:unicode

Generate a Proxy Ticket

Returns:A ticket with prefix settings.CAS_PROXY_TICKET_PREFIX and length settings.CAS_PT_LEN
Return type:unicode

Generate a Proxy Granting Ticket

Returns:A ticket with prefix settings.CAS_PROXY_GRANTING_TICKET_PREFIX and length settings.CAS_PGT_LEN
Return type:unicode

Generate a Proxy Granting Ticket IOU

Returns:A ticket with prefix settings.CAS_PROXY_GRANTING_TICKET_IOU_PREFIX and length settings.CAS_PGTIOU_LEN
Return type:unicode

Generate an saml id

Returns:A random id of length settings.CAS_TICKET_LEN
Return type:unicode
cas_server.utils.get_tuple(nuplet, index, default=None)[source]
  • nuplet (tuple) – A tuple
  • index (int) – An index
  • default – An optional default value

nuplet[index] if defined, else default (possibly None)


Validate a salt as crypt salt

Parameters:salt (str) – a password salt
Returns:True if salt is a valid crypt salt on this system, False otherwise
Return type:bool
class cas_server.utils.LdapHashUserPassword[source]

Bases: object

Class to deal with hashed password as defined at

schemes_salt = set(['{SSHA512}', '{SSHA384}', '{CRYPT}', '{SMD5}', '{SSHA}', '{SSHA256}'])

valide schemes that require a salt

schemes_nosalt = set(['{SHA}', '{SHA512}', '{SHA256}', '{MD5}', '{SHA384}'])

valide sschemes that require no slat

exception BadScheme[source]

Bases: exceptions.ValueError

Error raised then the hash scheme is not in LdapHashUserPassword.schemes_salt + LdapHashUserPassword.schemes_nosalt

exception LdapHashUserPassword.BadHash[source]

Bases: exceptions.ValueError

Error raised then the hash is too short

exception LdapHashUserPassword.BadSalt[source]

Bases: exceptions.ValueError

Error raised then, with the scheme {CRYPT}, the salt is invalid

classmethod LdapHashUserPassword.hash(scheme, password, salt=None, charset='utf8')[source]

Hash password with scheme using salt. This three variable beeing encoded in charset.

  • scheme (bytes) – A valid scheme
  • password (bytes) – A byte string to hash using scheme
  • salt (bytes) – An optional salt to use if scheme requires any
  • charset (str) – The encoding of scheme, password and salt

The hashed password encoded with charset

Return type:


classmethod LdapHashUserPassword.get_scheme(hashed_passord)[source]

Return the scheme of hashed_passord or raise BadHash

Parameters:hashed_passord (bytes) – A hashed password
Returns:The scheme used by the hashed password
Return type:bytes
Raises:BadHash – if no valid scheme is found within hashed_passord
classmethod LdapHashUserPassword.get_salt(hashed_passord)[source]

Return the salt of hashed_passord possibly empty

Parameters:hashed_passord (bytes) – A hashed password
Returns:The salt used by the hashed password (empty if no salt is used)
Return type:bytes
Raises:BadHash – if no valid scheme is found within hashed_passord or if the hashed password is too short for the scheme found.
cas_server.utils.check_password(method, password, hashed_password, charset)[source]

Check that password match hashed_password using method, assuming the encoding is charset.

  • method (str) – on of "crypt", "ldap", "hex_md5", "hex_sha1", "hex_sha224", "hex_sha256", "hex_sha384", "hex_sha512", "plain"
  • password (str or unicode) – The user inputed password
  • hashed_password (str or unicode) – The hashed password as stored in the database
  • charset (str) – The used char encoding (also used internally, so it must be valid for the charset used by password when it was initially )

True if password match hashed_password using method, False otherwise

Return type:



decode a version string following version semantic input a tuple of int

Parameters:version (unicode) – A dotted version
Returns:A tuple a int
Return type:tuple

Fetch the last version from pypi and return it. On successful fetch from pypi, the response is cached 24h, on error, it is cached 10 min.

Returns:the last django-cas-server version
Return type:unicode

Return all rows from a django cursor as a dict


Forge a SLO logout request

Parameters:ticket (unicode) – A ticket value
Returns:A SLO XML body request
Return type:unicode

Test that value is a valid regular expression

Parameters:value (unicode) – A regular expression to test
Raises:ValidationError – if value is not a valid regular expression