Skip to content

Commit

Permalink
Expect client credentials in the form.
Browse files Browse the repository at this point in the history
  • Loading branch information
nekitdev committed Feb 20, 2024
1 parent 836bb3d commit 3b98eb9
Showing 1 changed file with 11 additions and 26 deletions.
37 changes: 11 additions & 26 deletions melody/kit/clients/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
from uuid import UUID

from argon2.exceptions import VerifyMismatchError
from fastapi import Depends
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from fastapi import Depends, Form
from typing_extensions import Annotated

from melody.kit.clients.credentials import ClientCredentials
from melody.kit.core import database, hasher
from melody.kit.errors.auth import (
AuthClientCredentialsExpected,
AuthClientCredentialsInvalid,
AuthClientCredentialsNotFound,
AuthClientCredentialsSecretMismatch,
)
Expand All @@ -24,29 +21,13 @@
"optional_client_credentials_dependency",
)

BASIC_DESCRIPTION = "This is used solely for sending `client_id` and `client_secret` credentials."

basic = HTTPBasic(description=BASIC_DESCRIPTION, auto_error=False)

BasicCredentialsDependency = Annotated[Optional[HTTPBasicCredentials], Depends(basic)]
ClientIDDependency = Annotated[UUID, Form()]
ClientSecretDependency = Annotated[str, Form()]


async def client_credentials_dependency(
basic_credentials: BasicCredentialsDependency,
client_id: ClientIDDependency, client_secret: ClientSecretDependency
) -> ClientCredentials:
if basic_credentials is None:
raise AuthClientCredentialsExpected()

client_id_string = basic_credentials.username

try:
client_id = UUID(client_id_string)

except ValueError:
raise AuthClientCredentialsInvalid()

client_secret = basic_credentials.password

client_info = await database.query_client_info(client_id=client_id)

if client_info is None:
Expand All @@ -65,14 +46,18 @@ async def client_credentials_dependency(

ClientCredentialsDependency = Annotated[ClientCredentials, Depends(client_credentials_dependency)]

OptionalClientIDDependency = Annotated[Optional[UUID], Form()]
OptionalClientSecretDependency = Annotated[Optional[str], Form()]


async def optional_client_credentials_dependency(
basic_credentials: BasicCredentialsDependency,
client_id: OptionalClientIDDependency = None,
client_secret: OptionalClientSecretDependency = None,
) -> Optional[ClientCredentials]:
return (
None
if basic_credentials is None
else await client_credentials_dependency(basic_credentials)
if client_id is None or client_secret is None
else await client_credentials_dependency(client_id, client_secret)
)


Expand Down

0 comments on commit 3b98eb9

Please sign in to comment.