nbiatoolkit.auth

Module Contents

Classes

OAuth2

OAuth2 class for handling authentication and access token retrieval.

Functions

encrypt_credentials(→ Tuple[str, str])

Encrypts the given username and password using the provided key.

decrypt_credentials(→ tuple[str, str])

Decrypts the encrypted username and password using the provided key.

nbiatoolkit.auth.encrypt_credentials(key: bytes, username: str, password: str) Tuple[str, str][source]

Encrypts the given username and password using the provided key.

Parameters:
  • key (bytes) – The encryption key.

  • username (str) – The username to be encrypted.

  • password (str) – The password to be encrypted.

Returns:

A tuple containing the encrypted username and password.

Return type:

Tuple[str, str]

nbiatoolkit.auth.decrypt_credentials(key: bytes, encrypted_username: str, encrypted_password: str) tuple[str, str][source]

Decrypts the encrypted username and password using the provided key.

Parameters:
  • key (bytes) – The encryption key used to decrypt the credentials.

  • encrypted_username (str) – The encrypted username.

  • encrypted_password (str) – The encrypted password.

Returns:

A tuple containing the decrypted username and password.

Return type:

tuple[str, str]

class nbiatoolkit.auth.OAuth2(username: str = 'nbia_guest', password: str = '', client_id: str = 'NBIA', base_url: str | nbiatoolkit.utils.NBIA_BASE_URLS = NBIA_BASE_URLS.NBIA)[source]

OAuth2 class for handling authentication and access token retrieval.

This class provides methods to authenticate with the NBIA API using OAuth2 and retrieve the access token required for accessing the API.

Defaults to using the NBIA Guest for accessing public collections. If you have a username and password which has been granted access to the collections tagged with “limited access” you can use those credentials to access those collections.

client_id

The client ID for authentication.

Type:

str

username

The username for authentication.

Type:

str

password

The password for authentication.

Type:

str

access_token

The access token retrieved from the API.

Type:

str or None

api_headers

The authentication headers containing the access token.

Type:

dict or None

expiry_time

The expiry time of the access token.

Type:

str or None

refresh_token

The refresh token for obtaining a new access token.

Type:

str or None

refresh_expiry

The expiry time of the refresh token.

Type:

int or None

scope

The scope of the access token.

Type:

str or None

getToken()

Authenticates with the API. Returns API headers containing the access token.

Example Usage
-------------
>>> from nbiatoolkit.auth import OAuth2
To use the NBIA Guest account:
>>> oauth = OAuth2()
To use a custom account:
>>> oauth = OAuth2(username="my_username", password="my_password")

Notes

This class is mainly for developers looking to add functionality to the nbiatoolkit package. If you are a user looking to access the NBIA API, you can use the NBIAClient class without knowledge of this class.

As there are many packages for handling OAuth2 authentication, this class was for myself to learn how OAuth2 works and to provide a simple way to authenticate with the NBIA API. If you have any suggestions for improving this class, please open an issue on the GitHub repository.

property fernet_key: bytes
property access_token: str | None
property api_headers: dict[str, str]
property token_expiration_time
property refresh_expiration_time
property token_scope
is_logged_out() bool[source]
is_token_expired() bool[source]
refresh_token_or_request_new() None[source]
_refresh_access_token() None[source]
request_new_access_token()[source]
set_token_data(token_data: dict)[source]
__repr__() str | None[source]

Return repr(self).

__str__()[source]

Return str(self).

logout() None[source]

Logs out the user and revokes the access token.

This method sends a request to the NBIA API to revoke the access token and logs out the user.

Notes

This method is not yet implemented in the NBIA API.