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
12
13
from starlette .responses import Response
13
14
14
15
from fastapi_oauth2 .client import OAuth2Client
15
16
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
+ from tests .idp import TestOAuth2
19
+ from tests .idp import get_idp
18
20
19
21
package_path = backends .__path__ [0 ]
20
22
@@ -47,6 +49,20 @@ def fixture_wrapper(authentication: OAuth2 = None):
47
49
application = FastAPI ()
48
50
app_router = APIRouter ()
49
51
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
+
50
66
@app_router .get ("/user" )
51
67
def user (request : Request , _ : str = Depends (oauth2 )):
52
68
return request .user
@@ -73,10 +89,15 @@ def auth(request: Request):
73
89
return response
74
90
75
91
application .include_router (app_router )
76
- application .include_router ( oauth2_router )
92
+ application .mount ( "" , get_idp () )
77
93
application .add_middleware (OAuth2Middleware , config = {
78
94
"allow_http" : True ,
79
95
"clients" : [
96
+ OAuth2Client (
97
+ backend = TestOAuth2 ,
98
+ client_id = "test_id" ,
99
+ client_secret = "test_secret" ,
100
+ ),
80
101
OAuth2Client (
81
102
backend = GithubOAuth2 ,
82
103
client_id = "test_id" ,
0 commit comments