cas_server.utils module

Some util function for the app

cas_server.utils.logger = <Logger cas_server.utils (INFO)>

logger facility

cas_server.utils.json_encode(obj)[source]

Encode a python object to json

cas_server.utils.context(params)[source]

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

Parameters
  • request (django.http.HttpRequest) – The request object used to generate this response.

  • data (dict) – The python dictionnary to return as a json

Returns

The content of data serialized in json

Return type

django.http.HttpResponse

cas_server.utils.import_attr(path)[source]

transform a python dotted path to the attr

Parameters

path (unicode or str 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

Parameters
  • url_name (unicode) – a URL pattern name

  • params (dict or NoneType) – Some parameter to append to the reversed URL

Returns

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

Return type

django.http.HttpResponseRedirect

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

Parameters
  • 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

Returns

The computed reverse URL of url_name with possible querystring from params

Return type

unicode

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

Parameters
  • get_or_post_params (django.http.QueryDict) – A GET or POST QueryDict

  • ignore (set) – An optinal set of keys to ignore during the copy

Returns

A copy of get_or_post_params

Return type

dict

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

Parameters
  • 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

Parameters
  • request (django.http.HttpRequest) – The current request object.

  • ignore_params (set) – An optional set of GET parameters to ignore

Returns

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

Return type

unicode

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

update parameters using params in the url query string

Parameters
  • url (unicode or str) – An URL possibily with a querystring

  • params (dict) – A dictionary of parameters for updating the url querystring

Returns

The URL with an updated querystring

Return type

unicode

cas_server.utils.unpack_nested_exception(error)[source]

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

cas_server.utils.gen_lt()[source]

Generate a Login Ticket

Returns

A ticket with prefix settings.CAS_LOGIN_TICKET_PREFIX and length settings.CAS_LT_LEN

Return type

unicode

cas_server.utils.gen_st()[source]

Generate a Service Ticket

Returns

A ticket with prefix settings.CAS_SERVICE_TICKET_PREFIX and length settings.CAS_ST_LEN

Return type

unicode

cas_server.utils.gen_pt()[source]

Generate a Proxy Ticket

Returns

A ticket with prefix settings.CAS_PROXY_TICKET_PREFIX and length settings.CAS_PT_LEN

Return type

unicode

cas_server.utils.gen_pgt()[source]

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

cas_server.utils.gen_pgtiou()[source]

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

cas_server.utils.gen_saml_id()[source]

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]
Parameters
  • nuplet (tuple) – A tuple

  • index (int) – An index

  • default – An optional default value

Returns

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

cas_server.utils.crypt_salt_is_valid(salt)[source]

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 https://tools.ietf.org/id/draft-stroeder-hashed-userpassword-values-01.html

schemes_salt = {b'{CRYPT}', b'{SMD5}', b'{SSHA256}', b'{SSHA384}', b'{SSHA512}', b'{SSHA}'}

valide schemes that require a salt

schemes_nosalt = {b'{MD5}', b'{SHA256}', b'{SHA384}', b'{SHA512}', b'{SHA}'}

valide sschemes that require no slat

exception BadScheme[source]

Bases: ValueError

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

exception BadHash[source]

Bases: ValueError

Error raised then the hash is too short

exception BadSalt[source]

Bases: ValueError

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

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

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

Parameters
  • 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

Returns

The hashed password encoded with charset

Return type

bytes

classmethod 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 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.

Parameters
  • 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 )

Returns

True if password match hashed_password using method, False otherwise

Return type

bool

cas_server.utils.decode_version(version)[source]

decode a version string following version semantic http://semver.org/ input a tuple of int. It will work as long as we do not use pre release versions.

Parameters

version (unicode) – A dotted version

Returns

A tuple a int

Return type

tuple

cas_server.utils.last_version()[source]

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

cas_server.utils.dictfetchall(cursor)[source]

Return all rows from a django cursor as a dict

cas_server.utils.logout_request(ticket)[source]

Forge a SLO logout request

Parameters

ticket (unicode) – A ticket value

Returns

A SLO XML body request

Return type

unicode

cas_server.utils.regexpr_validator(value)[source]

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