sqrl.utils module

class sqrl.utils.Base64[source]

Bases: object

Helper class for base64 encoding/decoding

classmethod decode(s)[source]

Decode unicode string from base64 where remaining “=” characters were stripped.

Parameters:s (str) – Unicode string to be decoded from base64
classmethod encode(s)[source]

Encode binary string as base64. Remaining “=” characters are removed.

Parameters:s (bytes) – Bytes string to be encoded as base64
class sqrl.utils.Encoder[source]

Bases: object

Helper class for encoding/decoding SQRL response data.

classmethod base64_dumps(data)[source]

Dumps given data into a single Base64 string.

Practically this is the same as dumps() except dumps() can return multiline string for dict. This method normalizes that further by converting that multiline string to a single base64 encoded value.

Returns:Base64 encoded binary data of input data
Return type:binary
classmethod dumps(data)[source]

Recursively dumps given data to SQRL response format.

Before data is dumped out, it is normalized by using normalize().

This dumps each data type as follows:

Dict:returns an \r\n multiline string. Each line is for a single key-pair of format <key>=<dumped value>.
List:tilde (~) joined dumped list of values
Other:no operation
classmethod normalize(data)[source]

Recursively normalize data for encoding.

This encodes each data type as follows:

Dict:returns an OrderedDict where all values are recursively normalized. Empty dict is normalized to empty string
List:each value is recursively normalized
Binary:Base64 encode data
Str:no operation
Other:data is casted to string using __str__ (or __unicode__)
class sqrl.utils.QRGenerator(url)[source]

Bases: object

Helper class for generating a QR image for the given SQRL url.

Parameters:url (str) – URL for which to generate QR image
generate_image()[source]

Generate QR image and get its binary data.

Returns:Binary data of the png image file which can directly be returned to the user
Return type:bytes
sqrl.utils.get_user_ip(request)[source]

Utility function for getting user’s IP from request address.

This either returns the IP address from the request.REMOTE_ADDR or request.META'HTTP_X_REAL_IP'] when request might of been reverse proxied.