Skip to content

Commit 0d602a7

Browse files
committed
Provide configs from client code
1 parent 5c785b5 commit 0d602a7

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import json
2+
import os
23

4+
from dotenv import load_dotenv
35
from fastapi import FastAPI, Request, APIRouter
46
from fastapi.responses import HTMLResponse
57
from fastapi.templating import Jinja2Templates
@@ -10,6 +12,7 @@
1012
from fastapi_oauth2.middleware import OAuth2Middleware
1113
from fastapi_oauth2.router import router as oauth2_router
1214

15+
load_dotenv()
1316
router = APIRouter()
1417
templates = Jinja2Templates(directory="templates")
1518

@@ -25,12 +28,15 @@ async def root(request: Request):
2528
app.include_router(oauth2_router)
2629
app.add_middleware(OAuth2Middleware, config={
2730
"allow_http": True,
31+
"jwt_secret": os.getenv("JWT_SECRET"),
32+
"jwt_expires": os.getenv("JWT_EXPIRES"),
33+
"jwt_algorithm": os.getenv("JWT_ALGORITHM"),
2834
"clients": [
2935
OAuth2Client(
3036
backend=GithubOAuth2,
31-
client_id="eccd08d6736b7999a32a",
32-
client_secret="642999c1c5f2b3df8b877afdc78252ef5b594d31",
33-
redirect_uri="http://127.0.0.1:8000/",
37+
client_id=os.getenv("OAUTH2_CLIENT_ID"),
38+
client_secret=os.getenv("OAUTH2_CLIENT_SECRET"),
39+
# redirect_uri="http://127.0.0.1:8000/",
3440
scope=["user:email"],
3541
),
3642
]

src/fastapi_oauth2/config.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
import os
22
from typing import List
33

4-
from dotenv import load_dotenv
5-
64
from .client import OAuth2Client
75

8-
load_dotenv()
9-
10-
OAUTH2_CLIENT_ID = os.getenv("OAUTH2_CLIENT_ID")
11-
OAUTH2_CLIENT_SECRET = os.getenv("OAUTH2_CLIENT_SECRET")
12-
OAUTH2_CALLBACK_URL = os.getenv("OAUTH2_CALLBACK_URL")
13-
14-
JWT_SECRET = os.getenv("JWT_SECRET")
15-
JWT_ALGORITHM = os.getenv("JWT_ALGORITHM")
16-
JWT_EXPIRES = int(os.getenv("JWT_EXPIRES", "15"))
17-
186

197
class OAuth2Config:
208
allow_http: bool
@@ -32,8 +20,10 @@ def __init__(
3220
jwt_algorithm: str = "HS256",
3321
clients: List[OAuth2Client] = None,
3422
):
23+
if allow_http:
24+
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
3525
self.allow_http = allow_http
3626
self.jwt_secret = jwt_secret
37-
self.jwt_expires = jwt_expires
27+
self.jwt_expires = int(jwt_expires)
3828
self.jwt_algorithm = jwt_algorithm
3929
self.clients = clients or []

0 commit comments

Comments
 (0)