Skip to content

Oauth Accounts

oauth_accounts

OAuth account adapter interface.

Defines the abstract interface for OAuth account storage and management. Handles linking OAuth provider accounts to internal user accounts.

Classes

OAuthAccountAdapter

Bases: ABC

Abstract base class for OAuth account database operations.

Implementations must provide database-specific logic for OAuth account management. The core business logic remains database-agnostic.

Functions
create abstractmethod
create(
    *,
    user_id: UUID,
    provider: str,
    provider_user_id: str,
    access_token_hash: str,
    refresh_token_hash: str | None = None,
    expires_at: datetime | None = None,
    email: str | None = None,
    name: str | None = None,
    avatar_url: str | None = None
) -> Any

Create a new OAuth account link.

Parameters:

Name Type Description Default
user_id UUID

Internal user ID

required
provider str

OAuth provider name (google, github, etc.)

required
provider_user_id str

User ID from OAuth provider

required
access_token_hash str

Hashed OAuth access token

required
refresh_token_hash str | None

Optional hashed OAuth refresh token

None
expires_at datetime | None

Token expiration time

None
email str | None

Email from provider (for reference)

None
name str | None

Display name from provider

None
avatar_url str | None

Profile picture URL

None

Returns:

Type Description
Any

Created OAuth account object

Source code in fastauth/adapters/base/oauth_accounts.py
@abstractmethod
def create(
    self,
    *,
    user_id: uuid.UUID,
    provider: str,
    provider_user_id: str,
    access_token_hash: str,
    refresh_token_hash: str | None = None,
    expires_at: datetime | None = None,
    email: str | None = None,
    name: str | None = None,
    avatar_url: str | None = None,
) -> Any:
    """
    Create a new OAuth account link.

    Args:
        user_id: Internal user ID
        provider: OAuth provider name (google, github, etc.)
        provider_user_id: User ID from OAuth provider
        access_token_hash: Hashed OAuth access token
        refresh_token_hash: Optional hashed OAuth refresh token
        expires_at: Token expiration time
        email: Email from provider (for reference)
        name: Display name from provider
        avatar_url: Profile picture URL

    Returns:
        Created OAuth account object
    """
    ...
get_by_provider_user_id abstractmethod
get_by_provider_user_id(*, provider: str, provider_user_id: str) -> Any

Get OAuth account by provider and provider user ID.

Parameters:

Name Type Description Default
provider str

OAuth provider name

required
provider_user_id str

User ID from OAuth provider

required

Returns:

Type Description
Any

OAuth account if found, None otherwise

Source code in fastauth/adapters/base/oauth_accounts.py
@abstractmethod
def get_by_provider_user_id(self, *, provider: str, provider_user_id: str) -> Any:
    """
    Get OAuth account by provider and provider user ID.

    Args:
        provider: OAuth provider name
        provider_user_id: User ID from OAuth provider

    Returns:
        OAuth account if found, None otherwise
    """
    ...
get_by_user_id abstractmethod
get_by_user_id(*, user_id: UUID, provider: str | None = None) -> list[Any]

Get OAuth accounts for a user, optionally filtered by provider.

Parameters:

Name Type Description Default
user_id UUID

User's unique identifier

required
provider str | None

Optional provider name to filter by

None

Returns:

Type Description
list[Any]

List of OAuth account objects

Source code in fastauth/adapters/base/oauth_accounts.py
@abstractmethod
def get_by_user_id(
    self, *, user_id: uuid.UUID, provider: str | None = None
) -> list[Any]:
    """
    Get OAuth accounts for a user, optionally filtered by provider.

    Args:
        user_id: User's unique identifier
        provider: Optional provider name to filter by

    Returns:
        List of OAuth account objects
    """
    ...
update_tokens abstractmethod
update_tokens(
    *,
    account_id: UUID,
    access_token_hash: str,
    refresh_token_hash: str | None = None,
    expires_at: datetime | None = None
) -> None

Update OAuth tokens for an account.

Parameters:

Name Type Description Default
account_id UUID

OAuth account ID

required
access_token_hash str

New hashed access token

required
refresh_token_hash str | None

New hashed refresh token (if applicable)

None
expires_at datetime | None

New expiration time

None
Source code in fastauth/adapters/base/oauth_accounts.py
@abstractmethod
def update_tokens(
    self,
    *,
    account_id: uuid.UUID,
    access_token_hash: str,
    refresh_token_hash: str | None = None,
    expires_at: datetime | None = None,
) -> None:
    """
    Update OAuth tokens for an account.

    Args:
        account_id: OAuth account ID
        access_token_hash: New hashed access token
        refresh_token_hash: New hashed refresh token (if applicable)
        expires_at: New expiration time
    """
    ...
delete abstractmethod
delete(*, account_id: UUID) -> None

Remove OAuth account link.

Parameters:

Name Type Description Default
account_id UUID

OAuth account ID to delete

required
Source code in fastauth/adapters/base/oauth_accounts.py
@abstractmethod
def delete(self, *, account_id: uuid.UUID) -> None:
    """
    Remove OAuth account link.

    Args:
        account_id: OAuth account ID to delete
    """
    ...