Skip to content

Commit f0c5434

Browse files
committed
Add a few notes about usage expectations
1 parent 40284e8 commit f0c5434

File tree

4 files changed

+54
-36
lines changed

4 files changed

+54
-36
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ uvicorn main:app --reload
2121

2222
- Integrate with any existing FastAPI project (no dependencies of the project should stop the work of
2323
the `fastapi-oauth2`)
24+
* Implementation must allow to provide a context for configurations (also, see how it is done in another projects)
2425
- Use multiple OAuth2 providers at the same time
25-
- Token to user data, user data to token easy conversion
26+
* There need to be provided a way to configure the OAuth2 for multiple providers
27+
- Token -> user data, user data -> token easy conversion
2628
- Customize OAuth2 routes

main.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
from fastapi import FastAPI, Request, APIRouter
44
from fastapi.responses import HTMLResponse
55
from fastapi.templating import Jinja2Templates
6+
from social_core.backends.github import GithubOAuth2
67

78
from demo.router import router as demo_router
89
from fastapi_oauth2.middleware import OAuth2Middleware
910
from fastapi_oauth2.router import router as oauth2_router
11+
from fastapi_oauth2.types import OAuth2Client
1012

1113
router = APIRouter()
1214
templates = Jinja2Templates(directory="templates")
@@ -23,11 +25,12 @@ async def root(request: Request):
2325
app.include_router(oauth2_router)
2426
app.add_middleware(OAuth2Middleware, config={
2527
"allow_http": True,
26-
"providers": {
27-
"github": {
28-
"client_id": "eccd08d6736b7999a32a",
29-
"client_secret": "642999c1c5f2b3df8b877afdc78252ef5b594d31",
30-
"redirect_uri": "http://127.0.0.1:8000/",
31-
},
32-
}
28+
"clients": [
29+
OAuth2Client(
30+
backend=GithubOAuth2,
31+
client_id="eccd08d6736b7999a32a",
32+
client_secret="642999c1c5f2b3df8b877afdc78252ef5b594d31",
33+
redirect_uri="http://127.0.0.1:8000/",
34+
),
35+
]
3336
})

src/fastapi_oauth2/middleware.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from starlette.requests import Request
77
from starlette.types import Send, Receive, Scope, ASGIApp
88

9-
from .types import Config
10-
from .types import ConfigParams
9+
from .types import OAuth2Config
1110
from .utils import jwt_decode
1211

1312

@@ -25,11 +24,11 @@ async def authenticate(self, request: Request) -> Optional[Tuple["AuthCredential
2524

2625

2726
class OAuth2Middleware:
28-
def __init__(self, app: ASGIApp, config: Union[Config, ConfigParams]) -> None:
29-
if isinstance(config, Config):
27+
def __init__(self, app: ASGIApp, config: Union[OAuth2Config, dict]) -> None:
28+
if isinstance(config, OAuth2Config):
3029
self.config = config
3130
elif isinstance(config, dict):
32-
self.config = Config(**config)
31+
self.config = OAuth2Config(**config)
3332
else:
3433
raise ValueError("config does not contain valid parameters")
3534
self.auth_middleware = AuthenticationMiddleware(app, OAuth2Backend())

src/fastapi_oauth2/types.py

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
1-
from enum import Enum
2-
from typing import Dict, TypedDict
1+
from typing import List, Optional, Type
32

3+
from social_core.backends.oauth import BaseOAuth2
44

5-
class OAuth2Provider(str, Enum):
6-
github = "github"
75

8-
9-
class OAuth2Client(Dict[str, str]):
6+
class OAuth2Client:
7+
backend: Type[BaseOAuth2]
108
client_id: str
119
client_secret: str
12-
redirect_uri: str
13-
14-
15-
class ConfigParams(TypedDict):
10+
redirect_uri: Optional[str]
11+
12+
def __init__(
13+
self,
14+
*,
15+
backend: Type[BaseOAuth2],
16+
client_id: str,
17+
client_secret: str,
18+
redirect_uri: Optional[str] = None,
19+
):
20+
self.backend = backend
21+
self.client_id = client_id
22+
self.client_secret = client_secret
23+
self.redirect_uri = redirect_uri
24+
25+
26+
class OAuth2Config:
1627
allow_http: bool
1728
jwt_secret: str
1829
jwt_expires: int
1930
jwt_algorithm: str
20-
providers: Dict[OAuth2Provider, OAuth2Client]
21-
22-
23-
class Config:
24-
allow_http: bool = False
25-
jwt_secret: str = ""
26-
jwt_expires: int = 900
27-
jwt_algorithm: str = "HS256"
28-
providers: Dict[OAuth2Provider, OAuth2Client] = {}
29-
30-
def __init__(self, **kwargs):
31-
for key, value in kwargs.items():
32-
setattr(self, key, value)
31+
clients: List[OAuth2Client]
32+
33+
def __init__(
34+
self,
35+
*,
36+
allow_http: bool = False,
37+
jwt_secret: str = "",
38+
jwt_expires: int = 900,
39+
jwt_algorithm: str = "HS256",
40+
clients: List[OAuth2Client] = None,
41+
):
42+
self.allow_http = allow_http
43+
self.jwt_secret = jwt_secret
44+
self.jwt_expires = jwt_expires
45+
self.jwt_algorithm = jwt_algorithm
46+
self.clients = clients or []

0 commit comments

Comments
 (0)