apexauthlib 0.1.1__tar.gz → 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {apexauthlib-0.1.1 → apexauthlib-0.1.2}/PKG-INFO +1 -1
- apexauthlib-0.1.2/apexauthlib/entities/__init__.py +3 -0
- apexauthlib-0.1.2/apexauthlib/entities/auth.py +35 -0
- {apexauthlib-0.1.1 → apexauthlib-0.1.2}/apexauthlib/fastapi/auth.py +33 -1
- {apexauthlib-0.1.1 → apexauthlib-0.1.2}/apexauthlib/integration/api.py +6 -4
- {apexauthlib-0.1.1 → apexauthlib-0.1.2}/pyproject.toml +1 -1
- {apexauthlib-0.1.1 → apexauthlib-0.1.2}/LICENSE +0 -0
- {apexauthlib-0.1.1 → apexauthlib-0.1.2}/README.md +0 -0
- {apexauthlib-0.1.1 → apexauthlib-0.1.2}/apexauthlib/__init__.py +0 -0
- {apexauthlib-0.1.1 → apexauthlib-0.1.2}/apexauthlib/fastapi/__init__.py +0 -0
- {apexauthlib-0.1.1 → apexauthlib-0.1.2}/apexauthlib/integration/__init__.py +0 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from typing import Any
|
|
3
|
+
from uuid import uuid4
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(frozen=True)
|
|
7
|
+
class User:
|
|
8
|
+
username: str
|
|
9
|
+
hashed_password: str
|
|
10
|
+
first_name: str
|
|
11
|
+
last_name: str
|
|
12
|
+
email: str
|
|
13
|
+
phone_number: str
|
|
14
|
+
|
|
15
|
+
is_admin: bool
|
|
16
|
+
|
|
17
|
+
id: str = field(default_factory=lambda: str(uuid4()))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class Service:
|
|
22
|
+
service: str
|
|
23
|
+
superuser: str
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class ServiceMetadata:
|
|
28
|
+
user_id: str
|
|
29
|
+
metadata: dict[str, Any]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True)
|
|
33
|
+
class UserMetadata:
|
|
34
|
+
service: str
|
|
35
|
+
metadata: dict[str, Any]
|
|
@@ -6,6 +6,7 @@ from fastapi import APIRouter, Depends, HTTPException
|
|
|
6
6
|
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
|
7
7
|
from starlette import status
|
|
8
8
|
|
|
9
|
+
from apexauthlib.entities import User
|
|
9
10
|
from apexauthlib.integration.api import AuthApiProvider
|
|
10
11
|
|
|
11
12
|
auth_api = APIRouter(tags=["Auth"])
|
|
@@ -22,10 +23,41 @@ TokenDependable = Annotated[str, Depends(oauth2())]
|
|
|
22
23
|
def get_user(
|
|
23
24
|
token: TokenDependable,
|
|
24
25
|
auth: AuthApiProviderDependable,
|
|
26
|
+
) -> User:
|
|
27
|
+
try:
|
|
28
|
+
api = auth.for_token(token)
|
|
29
|
+
return api.user()
|
|
30
|
+
except Exception as e:
|
|
31
|
+
raise HTTPException(
|
|
32
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
33
|
+
detail=str(e),
|
|
34
|
+
headers={"WWW-Authenticate": "Bearer"},
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def get_user_metadata(
|
|
39
|
+
token: TokenDependable,
|
|
40
|
+
auth: AuthApiProviderDependable,
|
|
25
41
|
) -> Any:
|
|
26
42
|
try:
|
|
27
43
|
api = auth.for_token(token)
|
|
28
|
-
return api.metadata_for(api.user())
|
|
44
|
+
return api.metadata_for(api.user().id)
|
|
45
|
+
except Exception as e:
|
|
46
|
+
raise HTTPException(
|
|
47
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
48
|
+
detail=str(e),
|
|
49
|
+
headers={"WWW-Authenticate": "Bearer"},
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def get_user_with_metadata(
|
|
54
|
+
token: TokenDependable,
|
|
55
|
+
auth: AuthApiProviderDependable,
|
|
56
|
+
) -> tuple[User, Any]:
|
|
57
|
+
try:
|
|
58
|
+
api = auth.for_token(token)
|
|
59
|
+
user = api.user()
|
|
60
|
+
return user, api.metadata_for(api.user().id)
|
|
29
61
|
except Exception as e:
|
|
30
62
|
raise HTTPException(
|
|
31
63
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
@@ -3,9 +3,11 @@ from __future__ import annotations
|
|
|
3
3
|
from dataclasses import dataclass
|
|
4
4
|
from typing import Any, Generic, Mapping, TypeVar
|
|
5
5
|
|
|
6
|
-
from apexdevkit.formatter import Formatter
|
|
6
|
+
from apexdevkit.formatter import DataclassFormatter, Formatter
|
|
7
7
|
from apexdevkit.http import FluentHttp, JsonDict
|
|
8
8
|
|
|
9
|
+
from apexauthlib.entities import User
|
|
10
|
+
|
|
9
11
|
ItemT = TypeVar("ItemT")
|
|
10
12
|
|
|
11
13
|
|
|
@@ -46,15 +48,15 @@ class AuthApi(Generic[ItemT]):
|
|
|
46
48
|
formatter: Formatter[Mapping[str, Any], ItemT]
|
|
47
49
|
token: str
|
|
48
50
|
|
|
49
|
-
def user(self) ->
|
|
50
|
-
return
|
|
51
|
+
def user(self) -> User:
|
|
52
|
+
return DataclassFormatter(User).load(
|
|
51
53
|
(
|
|
52
54
|
self.http.with_header("Authorization", f"Bearer {self.token}")
|
|
53
55
|
.get()
|
|
54
56
|
.on_endpoint("/auth/user")
|
|
55
57
|
.on_failure(raises=RuntimeError)
|
|
56
58
|
.json()
|
|
57
|
-
)
|
|
59
|
+
)
|
|
58
60
|
)
|
|
59
61
|
|
|
60
62
|
def metadata_for(self, user_id: str) -> ItemT:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|