Skip to content

Authentication

The auth parameter of the Consumer constructor offers a way to define an auth method to use for all requests.

auth_method = SomeAuthMethod(...)
github = GitHub(BASE_URL, auth=auth_method)

uplink.auth.BasicAuth

BasicAuth(username, password)

Authorizes requests using HTTP Basic Authentication.

There are two ways to use BasicAuth with a Consumer:

# implicit BasicAuth
github = Github(BASE_URL, auth=(USER, PASS))

# explicit BasicAuth
github = GitHub(BASE_URL, auth=BasicAuth(USER, PASS))
Source code in uplink/auth.py
def __init__(self, username, password):
    self._username = username
    self._password = password

uplink.auth.ProxyAuth

ProxyAuth(username, password)

Authorizes requests with an intermediate HTTP proxy.

If both API auth and intermediate Proxy auth are required, wrap ProxyAuth in MultiAuth:

# only ProxyAuth
github = Github(BASE_URL, auth=ProxyAuth(PROXYUSER, PROXYPASS))

# both BasicAuth and ProxyAuth
auth_methods = MultiAuth(
    BasicAuth(USER, PASS),
    ProxyAuth(PROXYUSER, PROXYPASS)
)
github = GitHub(BASE_URL, auth=auth_methods)
Source code in uplink/auth.py
def __init__(self, username, password):
    self._username = username
    self._password = password

uplink.auth.BearerToken

BearerToken(token)

Authorizes requests using a Bearer Token.

token = something_like_oauth_that_returns_a_token()
bearer = BearerToken(token)
api_consumer = SomeApiConsumerClass(BASE_URL, auth=bearer)
Source code in uplink/auth.py
def __init__(self, token):
    self._token = token

uplink.auth.MultiAuth

MultiAuth(*auth_methods)

Authorizes requests using multiple auth methods at the same time.

This is useful for API users to supply both API credentials and intermediary credentials (such as for a proxy).

auth_methods = MultiAuth(
    BasicAuth(USER, PASS),
    ProxyAuth(PROXY_USER, PROXY_PASS)
)
api_consumer = SomeApiConsumerClass(BASE_URL, auth=auth_methods)

This may also be used if an API requires multiple Auth Tokens.

auth_methods = MultiAuth(
    BearerToken(API_TOKEN),
    ApiTokenParam(QUERY_PARAMETER_NAME, QUERY_PARAMETER_VALUE),
    ApiTokenHeader(API_HEADER_NAME, API_TOKEN_2)
)
api_consumer = SomeApiConsumerClass(BASE_URL, auth=auth_methods)

API library authors may find it more helpful to treat MultiAuth as a list using append or extend to add aditional auth methods.

auth_methods = MultiAuth()

auth_methods.append(BearerToken(API_TOKEN))
auth_methods.extend([
    ApiTokenParam(QUERY_PARAMETER_NAME, QUERY_PARAMETER_VALUE),
    ApiTokenHeader(API_HEADER_NAME, API_TOKEN_2)
])
api_consumer = SomeApiConsumerClass(BASE_URL, auth=auth_methods)

# looping over contained auth methods is also supported
for method in auth_methods:
    print(method.__class__.__name__)
Source code in uplink/auth.py
def __init__(self, *auth_methods):
    self._auth_methods = [get_auth(auth_method) for auth_method in auth_methods]

uplink.auth.ApiTokenParam

ApiTokenParam(param, token)

Authorizes requests using a token or key in a query parameter.

Users may use this directly, or API library authors may subclass this to predefine the query parameter name to use. If supplying query parameter name on a subclass, define the _param property or attribute and override __init__() without using super().

Examples:

Direct use:

token_param = ApiTokenParam(QUERY_PARAM_NAME, TOKEN)
api_consumer = SomeApiConsumerClass(BASE_URL, auth=token_param)

Subclass in API library:

class ExampleApiTokenParam(ApiTokenParam):
    _param = "api-token"
    def __init__(self, token):
        self._param_value = token

# using the subclass
token_param = ExampleApiTokenParam(TOKEN)
api_consumer = SomeApiConsumerClass(BASE_URL, auth=token_param)

Source code in uplink/auth.py
def __init__(self, param, token):
    self._param = param
    self._param_value = token