Skip to content

Authentication Configuration

Sometimes the code hopefully speaks for itself

from pydantic_settings import BaseSettings
from pydantic_settings import SettingsConfigDict


class AuthenticationConfiguration(BaseSettings):
    """
    Simple authentication configuration class
    """
    secret_key: str
    algorithm: str
    access_token_expire_minutes: int
    model_config = SettingsConfigDict(env_file='.env')


AUTH = AuthenticationConfiguration()

We thus load from the environment variables (that could be provided via a .env file) several configuration values:

  • secret_key: The secret_key used in the jws encoding provided by the python-jose library.
  • access_token_expire_minutes: the number of minutes after which the JWT generated by jws expires
  • algorithm: the encoding/decoding algorithm used by jws (HS256 for instance, or RS256)

As you can notice (if you have sharpened eye 😊), we use Pydantic V2 thanks to the effort of Tiangolo to migrate FastAPI and SQLModel to this version.