cas_server.utils module¶
Some util function for the app
-
cas_server.utils.
logger
= <Logger cas_server.utils (INFO)>¶ logger facility
-
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 keysettings
set todjango.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
orstr
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
withparams
as querystring- Parameters
url_name (unicode) – a URL pattern name
params (
dict
orNoneType
) – Some parameter to append to the reversed URL
- Returns
A redirection to the URL with name
url_name
withparams
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 fromparams
as querystring- Parameters
url_name (unicode) – a URL pattern name
params (
dict
orNoneType
) – 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 fromparams
- Return type
unicode
-
cas_server.utils.
copy_params
(get_or_post_params, ignore=None)[source]¶ copy a
django.http.QueryDict
in adict
ignoring keys in the setignore
- 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
onresponse
with valuevalue
valid formax_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 theurl
query string- Parameters
url (
unicode
orstr
) – An URL possibily with a querystringparams (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 lengthsettings.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 lengthsettings.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 lengthsettings.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 lengthsettings.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 lengthsettings.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, elsedefault
(possiblyNone
)
-
cas_server.utils.
crypt_salt_is_valid
(salt)[source]¶ Validate a salt as crypt salt
- Parameters
salt (str) – a password salt
- Returns
True
ifsalt
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
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
withscheme
usingsalt
. This three variable beeing encoded incharset
.- 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 anycharset (str) – The encoding of
scheme
,password
andsalt
- Returns
The hashed password encoded with
charset
- Return type
bytes
-
classmethod
get_scheme
(hashed_passord)[source]¶ Return the scheme of
hashed_passord
or raiseBadHash
- 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 usingmethod
, assuming the encoding ischarset
.- Parameters
method (str) – on of
"crypt"
,"ldap"
,"hex_md5"
,"hex_sha1"
,"hex_sha224"
,"hex_sha256"
,"hex_sha384"
,"hex_sha512"
,"plain"
password (
str
orunicode
) – The user inputed passwordhashed_password (
str
orunicode
) – The hashed password as stored in the databasecharset (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
matchhashed_password
usingmethod
,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