9
9
from fastapi import Request
10
10
from social_core .backends .github import GithubOAuth2
11
11
from social_core .backends .oauth import BaseOAuth2
12
- from starlette .responses import RedirectResponse
13
12
from starlette .responses import Response
14
13
15
14
from fastapi_oauth2 .client import OAuth2Client
16
15
from fastapi_oauth2 .middleware import OAuth2Middleware
16
+ from fastapi_oauth2 .router import router as oauth2_router
17
17
from fastapi_oauth2 .security import OAuth2
18
18
from tests .idp import TestOAuth2
19
19
from tests .idp import get_idp
@@ -41,28 +41,18 @@ def backends():
41
41
42
42
@pytest .fixture
43
43
def get_app ():
44
- def fixture_wrapper (authentication : OAuth2 = None ):
44
+ def fixture_wrapper (
45
+ authentication : OAuth2 = None , # type of security
46
+ with_idp = False , # used to test oauth2 flow
47
+ with_ssr = True , # used to test oauth2 flow
48
+ ):
45
49
if not authentication :
46
50
authentication = OAuth2 ()
47
51
48
52
oauth2 = authentication
49
53
application = FastAPI ()
50
54
app_router = APIRouter ()
51
55
52
- @app_router .get ("/oauth2/{provider}/auth" )
53
- async def login (request : Request , provider : str ):
54
- return await request .auth .clients [provider ].login_redirect (request )
55
-
56
- @app_router .get ("/oauth2/{provider}/token" )
57
- async def token (request : Request , provider : str ):
58
- return await request .auth .clients [provider ].token_redirect (request , app = get_idp ())
59
-
60
- @app_router .get ("/oauth2/logout" )
61
- def logout (request : Request ):
62
- response = RedirectResponse (request .base_url )
63
- response .delete_cookie ("Authorization" )
64
- return response
65
-
66
56
@app_router .get ("/user" )
67
57
def user (request : Request , _ : str = Depends (oauth2 )):
68
58
return request .user
@@ -88,9 +78,21 @@ def auth(request: Request):
88
78
)
89
79
return response
90
80
81
+ if with_idp :
82
+ @app_router .get ("/oauth2/{provider}/token" )
83
+ async def token (request : Request , provider : str ):
84
+ if request .auth .ssr :
85
+ return await request .auth .clients [provider ].token_redirect (request , app = get_idp ())
86
+ return await request .auth .clients [provider ].token_data (request )
87
+
91
88
application .include_router (app_router )
92
- application .mount ("" , get_idp ())
89
+ application .include_router (oauth2_router )
90
+
91
+ if with_idp :
92
+ application .mount ("" , get_idp ())
93
+
93
94
application .add_middleware (OAuth2Middleware , config = {
95
+ "enable_ssr" : with_ssr ,
94
96
"allow_http" : True ,
95
97
"clients" : [
96
98
OAuth2Client (
0 commit comments