Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support pydantic v2 #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions fastapi_security/entities.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
from datetime import datetime
from enum import Enum
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Annotated

from pydantic import BaseModel, Field
from pydantic.functional_validators import BeforeValidator

from pydantic import BaseModel, Field, validator

__all__ = ("User",)

def filtered_str_to_list(v: str) -> List[str]:
return [s for s in v.split(' ') if s] if isinstance(v, str) else v

def str_to_list(v: str) -> List[str]:
return v.split(' ') if isinstance(v, str) else v

FilteredStringList = Annotated[List[str], BeforeValidator(filtered_str_to_list)]
StringList = Annotated[List[str], BeforeValidator(str_to_list)]

class JwtAccessToken(BaseModel):
iss: str = Field(..., description="Issuer")
sub: str = Field(..., description="Subject")
aud: List[str] = Field(..., description="Audience")
aud: StringList = Field(..., description="Audience")
iat: datetime = Field(..., description="Issued At")
exp: datetime = Field(..., description="Expiration Time")
azp: str = Field(
Expand All @@ -21,28 +31,13 @@ class JwtAccessToken(BaseModel):
"",
description="Grant type (auth0 specific, see https://auth0.com/docs/applications/concepts/application-grant-types)",
)
scope: List[str] = Field("", description="Scope Values")
permissions: List[str] = Field(
scope: FilteredStringList = Field([], description="Scope Values")
permissions: StringList = Field(
[],
description="Permissions (auth0 specific, intended for first-party app authorization)",
)
raw: str = Field(..., description="The raw access token")

@validator("aud", pre=True, always=True)
def aud_to_list(cls, v):
return v.split(" ") if isinstance(v, str) else v

@validator("scope", pre=True, always=True)
def scope_to_list(cls, v):
if isinstance(v, str):
return [s for s in v.split(" ") if s]
else:
return v

@validator("permissions", pre=True, always=True)
def permissions_to_list(cls, v):
return v.split(" ") if isinstance(v, str) else v

def is_client_credentials(self):
return self.gty == "client-credentials"

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ classifiers = [
python = ">=3.7,<4.0"
aiohttp = { version = "^3", optional = true }
fastapi = "^0"
pydantic = "^1"
pydantic = "^2"
PyJWT = { version = "^2", extras = ["crypto"], optional = true }

[tool.poetry.extras]
Expand Down