Skip to content

Commit e3a7450

Browse files
committed
Remove usage of the OAUTH2_REDIRECT_URL env var
1 parent f0c5434 commit e3a7450

File tree

8 files changed

+50
-63
lines changed

8 files changed

+50
-63
lines changed

.env

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
OAUTH2_CLIENT_ID=eccd08d6736b7999a32a
22
OAUTH2_CLIENT_SECRET=642999c1c5f2b3df8b877afdc78252ef5b594d31
33
OAUTH2_CALLBACK_URL=http://127.0.0.1:8000/oauth2/token
4-
OAUTH2_REDIRECT_URL=http://127.0.0.1:8000/
54

65
JWT_SECRET=secret
76
JWT_ALGORITHM=HS256

src/fastapi_oauth2/config.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
import os
2+
from typing import List
23

34
from dotenv import load_dotenv
45

6+
from .client import OAuth2Client
7+
58
load_dotenv()
69

710
OAUTH2_CLIENT_ID = os.getenv("OAUTH2_CLIENT_ID")
811
OAUTH2_CLIENT_SECRET = os.getenv("OAUTH2_CLIENT_SECRET")
912
OAUTH2_CALLBACK_URL = os.getenv("OAUTH2_CALLBACK_URL")
10-
OAUTH2_REDIRECT_URL = os.getenv("OAUTH2_REDIRECT_URL")
1113

1214
JWT_SECRET = os.getenv("JWT_SECRET")
1315
JWT_ALGORITHM = os.getenv("JWT_ALGORITHM")
1416
JWT_EXPIRES = int(os.getenv("JWT_EXPIRES", "15"))
17+
18+
19+
class OAuth2Config:
20+
allow_http: bool
21+
jwt_secret: str
22+
jwt_expires: int
23+
jwt_algorithm: str
24+
clients: List[OAuth2Client]
25+
26+
def __init__(
27+
self,
28+
*,
29+
allow_http: bool = False,
30+
jwt_secret: str = "",
31+
jwt_expires: int = 900,
32+
jwt_algorithm: str = "HS256",
33+
clients: List[OAuth2Client] = None,
34+
):
35+
self.allow_http = allow_http
36+
self.jwt_secret = jwt_secret
37+
self.jwt_expires = jwt_expires
38+
self.jwt_algorithm = jwt_algorithm
39+
self.clients = clients or []

src/fastapi_oauth2/base.py renamed to src/fastapi_oauth2/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from starlette.requests import Request
99
from starlette.responses import RedirectResponse
1010

11-
from .config import JWT_EXPIRES, OAUTH2_REDIRECT_URL
11+
from .config import JWT_EXPIRES
1212
from .exceptions import OAuth2LoginError
1313
from .utils import jwt_create
1414

@@ -130,7 +130,7 @@ async def token_redirect(
130130
) -> RedirectResponse:
131131
token_data = await self.get_token_data(request, params=params, headers=headers)
132132
access_token = jwt_create(token_data)
133-
response = RedirectResponse(OAUTH2_REDIRECT_URL)
133+
response = RedirectResponse(request.base_url)
134134
response.set_cookie(
135135
"Authorization",
136136
value=f"Bearer {access_token}",

src/fastapi_oauth2/github.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from .base import OAuth2Base
1+
from .core import OAuth2Core
22

33

4-
class GitHubOAuth2(OAuth2Base):
4+
class GitHubOAuth2(OAuth2Core):
55
"""Class providing login via GitHub SSO"""
66

77
scope = ["user:email"]

src/fastapi_oauth2/middleware.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
from typing import Optional, Tuple, Union
1+
from typing import Optional
2+
from typing import Tuple
3+
from typing import Union
24

35
from fastapi.security.utils import get_authorization_scheme_param
4-
from starlette.authentication import AuthenticationBackend, AuthCredentials
6+
from starlette.authentication import AuthCredentials
7+
from starlette.authentication import AuthenticationBackend
58
from starlette.middleware.authentication import AuthenticationMiddleware
69
from starlette.requests import Request
7-
from starlette.types import Send, Receive, Scope, ASGIApp
10+
from starlette.types import ASGIApp
11+
from starlette.types import Receive
12+
from starlette.types import Scope
13+
from starlette.types import Send
814

9-
from .types import OAuth2Config
15+
from .config import OAuth2Config
1016
from .utils import jwt_decode
1117

1218

@@ -24,13 +30,16 @@ async def authenticate(self, request: Request) -> Optional[Tuple["AuthCredential
2430

2531

2632
class OAuth2Middleware:
33+
config: OAuth2Config
34+
auth_middleware: AuthenticationMiddleware
35+
2736
def __init__(self, app: ASGIApp, config: Union[OAuth2Config, dict]) -> None:
2837
if isinstance(config, OAuth2Config):
2938
self.config = config
3039
elif isinstance(config, dict):
3140
self.config = OAuth2Config(**config)
3241
else:
33-
raise ValueError("config does not contain valid parameters")
42+
raise TypeError("config is not a valid type")
3443
self.auth_middleware = AuthenticationMiddleware(app, OAuth2Backend())
3544

3645
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:

src/fastapi_oauth2/router.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
OAUTH2_CLIENT_ID,
88
OAUTH2_CLIENT_SECRET,
99
OAUTH2_CALLBACK_URL,
10-
OAUTH2_REDIRECT_URL,
1110
)
1211

1312
router = APIRouter(prefix="/oauth2")
@@ -19,8 +18,9 @@
1918
)
2019

2120

22-
@router.get("/login")
23-
async def login():
21+
@router.get("/{provider}/auth")
22+
async def login(provider: str):
23+
print(provider)
2424
return await oauth2.login_redirect()
2525

2626

@@ -30,7 +30,7 @@ async def token(request: Request):
3030

3131

3232
@router.get("/logout")
33-
async def logout():
34-
response = RedirectResponse(OAUTH2_REDIRECT_URL)
33+
async def logout(request: Request):
34+
response = RedirectResponse(request.base_url)
3535
response.delete_cookie("Authorization")
3636
return response

src/fastapi_oauth2/types.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

templates/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)