ska Package

ska Package

class ska.__init__.Signature(signature, auth_user, valid_until)[source]

Bases: object

Signature generation and validation based on symmetric keys.

Parameters:
  • signature (str) –
  • auth_user (str) –
  • valid_until (float|str) –
auth_user
static datetime_to_timestamp(dt)[source]

Human readable datetime according to the format specified in TIMESTAMP_FORMAT.

Parameters:dt (datetime.datetime) –
Return str:
static datetime_to_unix_timestamp(dt)[source]

Converts datetime.datetime to Unix timestamp.

Parameters:dt (datetime.datetime) –
Return float:Unix timestamp.
classmethod generate_signature(auth_user, secret_key, valid_until=None, lifetime=600)[source]

Generates the signature. If timestamp is given, the signature is created using the given timestamp. Otherwise current time is used.

Parameters:
  • auth_user (str) –
  • secret_key (str) –
  • valid_until (float|str) – Unix timestamp, valid until.
  • lifetime (int) – Lifetime of the signature in seconds.
Return str:
Example :
>>> sig = Signature.generate_signature('user', 'your-secret-key')
EBS6ipiqRLa6TY5vxIvZU30FpnM=
classmethod get_base(auth_user, timestamp)[source]

Add something here so that timestamp to signature conversion is not that obvious.

is_expired()[source]

Checks if current signature is expired. Returns True if signature is expired and False otherwise.

Return bool:
Example :
>>> sig = Signature.generate_signature('user', 'your-secret-key') # Generating the signature
>>> sig.is_expired()
False
static make_secret_key(secret_key)[source]

The secret key how its’ supposed to be used in generate signature.

Parameters:secret_key (str) –
Return str:
signature
classmethod timestamp_to_date(timestamp, fail_silently=True)[source]

Converts the given timestamp to date. If fail_silently is set to False, raises exceptions if timestamp is not valid timestamp (according to the format we have specified in the TIMESTAMP_FORMAT). Mainly used internally.

Parameters:
  • timestamp (str) –
  • fail_silently (bool) –
Return str:
classmethod unix_timestamp_to_date(timestamp, fail_silently=True)[source]

Converts the given Unix timestamp to date. If fail_silently is set to False, raises exceptions if timestamp is not valid timestamp.

Parameters:
  • timestamp (float|str) – UNIX timestamp. Parsable to float.
  • fail_silently (bool) –
Return str:
valid_until
classmethod validate_signature(signature, auth_user, secret_key, valid_until, return_object=False)[source]

Validates the signature.

Parameters:
  • signature (str) –
  • auth_user (str) –
  • secret_key (str) –
  • valid_until (float|str) – Unix timestamp.
  • return_object (bool) – If set to True, an instance of SignatureValidationResult is returned.
Return bool:
Example :
>>> Signature.validate_signature(
    'EBS6ipiqRLa6TY5vxIvZU30FpnM=',
    'user',
    'your-secret-key',
    '1377997396.0'
    )
False
class ska.__init__.RequestHelper(signature_param, auth_user_param, valid_until_param)[source]

Bases: object

Request helper for easy put/extract of signature params from URLs.

signature_to_dict(signature)[source]

Puts signature into a dictionary, which can later on be used to send when sending (POST) requests to the server.

Parameters:signature (ska.Signature) –
Return dict:
Example :

Required imports.

>>> from ska import Signature, RequestHelper

Generate signature.

>>> signature = Signature.generate_signature(
>>>     auth_user = 'user',
>>>     secret_key = 'your-secret-key'
>>>     )

Create a request helper.

>>> request_helper = RequestHelper(
>>>     signature_param = 'signature',
>>>     auth_user_param = 'auth_user',
>>>     valid_until_param = 'valid_until'
>>> )

Appending signature params to the endpoint URL.

>>> signed_dict = request_helper.signature_to_dict(
>>>     signature = signature
>>> )
{
    'signature': 'YlZpLFsjUKBalL4x5trhkeEgqE8=',
    'auth_user': 'user',
    'valid_until': '1378045287.0'
}
signature_to_url(signature, endpoint_url='', suffix='?')[source]

URL encodes the signature params.

Parameters:
  • signature (ska.Signature) –
  • endpoint_url (str) –
  • suffix (str) – Suffix to add after the endpoint_url and before the appended signature params.
Return str:
Example :

Required imports.

>>> from ska import Signature, RequestHelper

Generate signature.

>>> signature = Signature.generate_signature(
>>>     auth_user = 'user',
>>>     secret_key = 'your-secret-key'
>>>     )

Create a request helper.

>>> request_helper = RequestHelper(
>>>     signature_param = 'signature',
>>>     auth_user_param = 'auth_user',
>>>     valid_until_param = 'valid_until'
>>> )

Appending signature params to the endpoint URL.

>>> url = request_helper.signature_to_url(
>>>     signature = signature,
>>>     endpoint_url = 'http://e.com/api/'
>>> )
http://e.com/api/?valid_until=1378045287.0&auth_user=user&signature=YlZpLFsjUKBalL4x5trhkeEgqE8%3D
validate_request_data(data, secret_key)[source]

Validates the request data.

Parameters:
  • data (dict) –
  • secret_key (str) –
Return ska.SignatureValidationResult:
 
Example :

If your imaginary HttpRequest object has GET property (dict), then you would validate the request data as follows.

Create a RequestHelper object with param names expected.

Required imports.

>>> from ska import RequestHelper

Create a request helper.

>>> request_helper = RequestHelper(
>>>     signature_param = 'signature',
>>>     auth_user_param = 'auth_user',
>>>     valid_until_param = 'valid_until'
>>> )

Validate the request data.

>>> validation_result = request_helper.validate_request_data(
>>>     data = request.GET,
>>>     secret_key = 'your-secret-key'
>>> )
ska.__init__.sign_url(auth_user, secret_key, valid_until=None, lifetime=600, url='', suffix='?', signature_param='signature', auth_user_param='auth_user', valid_until_param='valid_until')[source]

Signs the URL.

Parameters:
  • auth_user (str) – Username of the user making the request.
  • secret_key (str) – The shared secret key.
  • valid_until (float|str) – Unix timestamp. If not given, generated automatically (now + lifetime).
  • lifetime (int) – Signature lifetime in seconds.
  • url (str) – URL to be signed.
  • suffix (str) – Suffix to add after the endpoint_url and before the appended signature params.
  • signature_param (str) – Name of the GET param name which would hold the generated signature value.
  • auth_user_param (str) – Name of the GET param name which would hold the auth_user value.
  • valid_until_param (str) – Name of the GET param name which would hold the valid_until value.
Return str:
Example :

Required imports.

>>> from ska import sign_url

Producing a signed URL.

>>> signed_url = sign_url(
>>>     auth_user='user', secret_key='your-secret_key', lifetime=120,     >>>     url='http://e.com/api/', signature_param=DEFAULT_SIGNATURE_PARAM,
>>>     auth_user_param=DEFAULT_AUTH_USER_PARAM, valid_until_param=DEFAULT_VALID_UNTIL_PARAM
>>> )
http://e.com/api/?valid_until=1378045287.0&auth_user=user&signature=YlZpLFsjUKBalL4x5trhkeEgqE8%3D

defaults Module

Application defaults.

  • SIGNATURE_LIFETIME (int): Signature lifetime in seconds. Default value is 600 (seconds).
  • DEFAULT_SIGNATURE_PARAM (str): Default name of the GET param holding the generated signature value. Default value is signature.
  • DEFAULT_AUTH_USER_PARAM (str): Default name of the GET param holding the auth_user value. Default value is auth_user.
  • DEFAULT_VALID_UNTIL_PARAM (str): Default name of the GET param holding the valid_until value. Default value is valid_until.
  • DEFAULT_URL_SUFFIX (str): Suffix to add after the endpoint_url and before the appended signature params.

generate_signed_url Module

ska.generate_signed_url.main()[source]

Prints signed URL to console.

Example :

$ python src/ska/generate_signature.py -au user -sk test

Read the Docs v: 0.8
Versions
latest
0.9
0.8
0.7
0.6
0.5
0.4
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.