Skip to content

Authentication

AuthenCard

Methods:

Name Description
__init__

Initialize the Authen class with a secret key for JWT signing.

from_config

Initialize UserGenerator instance from a JSON configuration file.

verify_access_token

Verify a JWT access token by sending a request to the FastAPI verify-token endpoint.

Attributes:

Name Type Description
token
api_url

token instance-attribute

token = token

api_url instance-attribute

api_url = api_url

__init__

__init__(token: str, api_url: str)

Initialize the Authen class with a secret key for JWT signing.

Parameters:

Name Type Description Default
token str

The JWT token to authenticate with the AuthenCard

required
api_url str

The API URL of the AuthenCard API

required

from_config classmethod

from_config(config_path: str = 'authen/secret.json')

Initialize UserGenerator instance from a JSON configuration file.

Parameters:

Name Type Description Default
config_path str

Path to the JSON configuration file (default: authen/secret.json).

'authen/secret.json'

Returns: An instance of UserGenerator with values loaded from the config file. Raises: FileNotFoundError: If the config file doesn't exist. KeyError: If required fields are missing in the config file. json.JSONDecodeError: If the config file is invalid JSON.

verify_access_token

verify_access_token(token: str = None, api_url: str = 'http://localhost:8000/verify-token') -> Optional[Dict]

Verify a JWT access token by sending a request to the FastAPI verify-token endpoint.

Parameters:

Name Type Description Default
token str

The JWT token to verify

None
api_url str

The URL of the FastAPI verify-token endpoint (default: http://localhost:8000/verify-token)

'http://localhost:8000/verify-token'

Returns:

Type Description
Optional[Dict]

Optional[Dict]: Token payload with username, expires, and issued_at if valid, None if invalid

Raises:

Type Description
RequestException

If the API request fails

server

Classes:

Name Description
Token
TokenData
User
UserInDB
UserCreate

Functions:

Name Description
verify_password

Verify a plain password against a hashed password.

get_password_hash

Hash a password using bcrypt.

get_user

Retrieve user from database by username.

authenticate_user

Authenticate user by verifying username and password.

create_access_token

Create a JWT access token.

get_current_user

Get the current user from JWT token.

get_current_active_user

Ensure the current user is active.

login_for_access_token

Authenticate user and return access token.

create_user

Register a new user.

read_users_me

Get current user's information.

protected_route

Example of a protected endpoint.

verify_token

Verify JWT access token and return its payload.

Attributes:

Name Type Description
secret
SECRET_KEY
ALGORITHM
ACCESS_TOKEN_EXPIRE_MINUTES
fake_users_db
app
pwd_context
oauth2_scheme

secret module-attribute

secret = load(f)

SECRET_KEY module-attribute

SECRET_KEY = secret['secret_key']

ALGORITHM module-attribute

ALGORITHM = secret['algorithm']

ACCESS_TOKEN_EXPIRE_MINUTES module-attribute

ACCESS_TOKEN_EXPIRE_MINUTES = 30

fake_users_db module-attribute

fake_users_db = {secret['username']: {'username': secret['username'], 'full_name': 'Your Full Name', 'email': 'your_email@example.com', 'hashed_password': secret['hashed_password'], 'disabled': False}}

app module-attribute

app = FastAPI()

pwd_context module-attribute

pwd_context = CryptContext(schemes=['bcrypt'], deprecated='auto')

oauth2_scheme module-attribute

oauth2_scheme = OAuth2PasswordBearer(tokenUrl='token')

Token

Bases: BaseModel

Attributes:

Name Type Description
access_token str
token_type str

access_token instance-attribute

access_token: str

token_type class-attribute instance-attribute

token_type: str = 'bearer'

TokenData

Bases: BaseModel

Attributes:

Name Type Description
hashed_password Union[str, None]
expires Union[datetime, None]
issued_at Union[datetime, None]

hashed_password class-attribute instance-attribute

hashed_password: Union[str, None] = None

expires class-attribute instance-attribute

expires: Union[datetime, None] = None

issued_at class-attribute instance-attribute

issued_at: Union[datetime, None] = None

User

Bases: BaseModel

Attributes:

Name Type Description
username str
email Union[str, None]
full_name Union[str, None]
disabled Union[bool, None]

username instance-attribute

username: str

email class-attribute instance-attribute

email: Union[str, None] = None

full_name class-attribute instance-attribute

full_name: Union[str, None] = None

disabled class-attribute instance-attribute

disabled: Union[bool, None] = None

UserInDB

Bases: User

Attributes:

Name Type Description
hashed_password str
username str
email Union[str, None]
full_name Union[str, None]
disabled Union[bool, None]

hashed_password instance-attribute

hashed_password: str

username instance-attribute

username: str

email class-attribute instance-attribute

email: Union[str, None] = None

full_name class-attribute instance-attribute

full_name: Union[str, None] = None

disabled class-attribute instance-attribute

disabled: Union[bool, None] = None

UserCreate

Bases: BaseModel

Attributes:

Name Type Description
username str
email str
full_name str
password str

username instance-attribute

username: str

email instance-attribute

email: str

full_name instance-attribute

full_name: str

password instance-attribute

password: str

verify_password

verify_password(plain_password, hashed_password)

Verify a plain password against a hashed password.

get_password_hash

get_password_hash(password)

Hash a password using bcrypt.

get_user

get_user(db, username: str)

Retrieve user from database by username.

authenticate_user

authenticate_user(fake_db, username: str, password: str)

Authenticate user by verifying username and password.

create_access_token

create_access_token(data: dict, expires_delta: Union[timedelta, None] = None)

Create a JWT access token.

get_current_user async

get_current_user(token: Annotated[str, Depends(oauth2_scheme)])

Get the current user from JWT token.

get_current_active_user async

get_current_active_user(current_user: Annotated[User, Depends(get_current_user)])

Ensure the current user is active.

login_for_access_token async

login_for_access_token(form_data: Annotated[OAuth2PasswordRequestForm, Depends()])

Authenticate user and return access token.

create_user async

create_user(user: UserCreate)

Register a new user.

read_users_me async

read_users_me(current_user: Annotated[User, Depends(get_current_active_user)])

Get current user's information.

protected_route async

protected_route(current_user: Annotated[User, Depends(get_current_active_user)])

Example of a protected endpoint.

verify_token async

verify_token(token: Token)

Verify JWT access token and return its payload.