teos.gatekeeper

exception AuthenticationFailure[source]

Bases: Exception

Raised when a user can not be authenticated. Either the user public key cannot be recovered or the user is not found within the registered ones.

class Gatekeeper(user_db, block_processor, subscription_slots, subscription_duration, expiry_delta)[source]

Bases: object

The Gatekeeper is in charge of managing the access to the tower. Only registered users are allowed to perform actions.

subscription_slots

The number of slots assigned to a user subscription.

Type

int

subscription_duration

The expiry assigned to a user subscription.

Type

int

expiry_delta

The grace period given to the user to renew their subscription.

Type

int

block_processor

A block processor instance to get block from bitcoind.

Type

BlockProcessor

user_db

A user database manager instance to interact with the database.

Type

UsersDBM

registered_users

A map of user_pk:user_info.

Type

dict

lock

A lock object to lock access to the Gatekeeper on updates.

Type

Lock

add_update_appointment(user_id, uuid, appointment)[source]

Adds (or updates) an appointment to a user subscription. The user slots are updated accordingly.

Slots are taken if a new appointment is given, or an update is given with an appointment bigger than the existing one.

Slots are given back if an update is given but the new appointment is smaller than the existing one.

Parameters
  • user_id (str) – the public key that identifies the user (33-bytes hex str).

  • uuid (str) – the appointment uuid.

  • appointment (ExtendedAppointment) – the summary of new appointment the user is requesting.

Returns

The number of remaining appointment slots.

Return type

int

Raises

NotEnoughSlots – if the user does not have enough slots to fill.

add_update_user(user_id)[source]

Adds a new user or updates the subscription of an existing one, by adding additional slots.

Parameters

user_id (str) – the public key that identifies the user (33-bytes hex str).

Returns

A tuple with the number of available slots in the user subscription, the subscription expiry (in absolute block height), and the registration_receipt.

Return type

tuple

Raises

InvalidParameter – if the user_pk does not match the expected format.

authenticate_user(message, signature)[source]

Checks if a request comes from a registered user by ec-recovering their public key from a signed message.

Parameters
  • message (bytes) – byte representation of the original message from where the signature was generated.

  • signature (str) – the user’s signature (hex-encoded).

Returns

A compressed key recovered from the signature and matching a registered user.

Return type

str

Raises

AuthenticationFailure – if the user cannot be authenticated.

get_expired_appointments(block_height)[source]

Gets a list of appointments that expire at a given block height.

Parameters

block_height – the block height that wants to be checked.

Returns

A list of appointment uuids that will expire at block_height.

Return type

list

exception NotEnoughSlots[source]

Bases: ValueError

Raised when trying to subtract more slots than a user has available.

class UserInfo(available_slots, subscription_expiry, appointments=None)[source]

Bases: object

Class used to stored information about a user.

Parameters
  • available_slots (int) – the number of appointment slots available to the user.

  • subscription_expiry (int) – the block height when the user subscription will expire.

  • appointments (dict) – a dictionary containing the current appointments of the user. Optional.

classmethod from_dict(user_data)[source]

Creates a UserInfo instance from a dictionary.

Parameters

user_data (dict) – a dictionary containing all the necessary key:value pairs.

Raises

ValueError – if any of the dictionary entries is missing.

to_dict()[source]

Converts a UserInfo instance in a dictionary.