Auth

The momotor.rpc.auth module contains functions and classes to set up an authenticated channel to the Momotor broker.

It also provides functions to generate random authentication tokens.

Client

class momotor.rpc.auth.client.AuthenticatingChannel(channel, auth_token=None)

Wrapper for grpclib.client.Channel that adds an auth-token metadata to any request.

Proxies all methods of grpclib.client.Channel, and adds a few additional methods

is_authenticated()

Returns True if an auth-token is set

Return type

bool

request(name, cardinality, request_type, reply_type, *, timeout=None, deadline=None, metadata=None)

Wrapper for grpclib.client.Channel.request()

Return type

Stream[TypeVar(_SendType), TypeVar(_RecvType)]

async momotor.rpc.auth.client.authenticate(channel, api_key, api_secret, *, stub=None)

Authenticate with the server. Returns an AuthenticatingChannel.

Any exception returned by the server is raised as a subclass of RPCException

Parameters
  • channel (Channel) – channel to authenticate with

  • api_key (str) – Client’s api-key

  • api_secret (str) – Client’s api-secret

  • stub (Optional[AuthStub]) – stub to authenticate with (defaults to AuthStub(channel))

Return type

AuthenticatingChannel

async momotor.rpc.auth.client.get_authenticated_channel(host, port, api_key, api_secret, auth_token=None, *, ssl_context=None, loop=None, log_h2=False, keepalive_time=900, **channel_opts)

Connect to a broker and authenticate, possibly using an already existing token.

Returns a tuple with

  • the authenticated channel

  • the auth stub

If authentication fails, raises (a subclass of) RPCException

Produces logging information on the momotor.rpc.auth logger

Parameters
  • host (str) – Broker’s hostname

  • port (Optional[int]) – Broker’s port. If None, uses default ports 50051 or 50052, depending on ssl_context value

  • api_key (str) – API key to authenticate with

  • api_secret (str) – API secret to authenticate with

  • auth_token (Optional[str]) – (optional) existing authentication token to reuse session

  • ssl_context – SSL context to use

  • loop – asyncio event loop (Deprecated)

  • log_h2 – if True, enables logging of the h2 library

  • keepalive_time – keep alive time (None to disable)

  • channel_opts – additional keyword arguments supplied to grpclib.channel.Channel

Return type

Tuple[AuthenticatingChannel, AuthStub]

Returns

tuple containing: the authenticated channel and the auth stub

Utils

momotor.rpc.auth.utils.CHALLENGE_LENGTH = 512

Length of the challenge

momotor.rpc.auth.utils.API_KEY_CHARSET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

Valid characters for an API key

momotor.rpc.auth.utils.API_KEY_LENGTH = 24

Default length of an API key

momotor.rpc.auth.utils.API_SECRET_CHARSET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@$^&-_+./?'

Valid characters for an API secret

momotor.rpc.auth.utils.API_SECRET_LENGTH = 64

Default length of an API secret

momotor.rpc.auth.utils.SALT_CHARSET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@$^&-_+./?'

Valid characters for the salt used

momotor.rpc.auth.utils.SALT_LENGTH = 8

Length of the salt

momotor.rpc.auth.utils.gen_key(charset, length)

Generate a key of length using characters in charset

Parameters
  • charset (str) – Characters to use

  • length (int) – Length of the string

Return type

str

Returns

the generated key

momotor.rpc.auth.utils.gen_api_key()

Generate a random API key

Return type

str

Returns

the generated key

momotor.rpc.auth.utils.gen_api_secret()

Generate a random secret

Return type

str

Returns

the generated secret

momotor.rpc.auth.utils.gen_salt()

Generate a random salt

Return type

str

Returns

the generated salt

momotor.rpc.auth.utils.gen_challenge()

Generate a random challenge

Return type

bytes

Returns

the generated challenge

momotor.rpc.auth.utils.constant_time_compare(val1, val2)

Returns True if the two strings are equal, False otherwise.

The time taken is independent of the number of characters that match.

(Borrowed from Django)

momotor.rpc.auth.utils.calculate_challenge_response(api_key, api_secret, salt, challenge)

Calculate the response to a challenge request

Parameters
  • api_key (str) – The API key

  • api_secret (str) – The API secret

  • salt (str) – The salt

  • challenge (bytes) – The challenge

Return type

bytes

Returns

The challenge response