re
swadhinbiswas committed 2 years ago
85d725d
1 parent bcf16bd
commit 85d725d
1 file changed
+68 -24
Showing 1 changed file+68-24
▼backend/grabdatafromapi/test.py
+68-24
| @@ -6,17 +6,18 @@ from pydantic_settings import BaseSettings,SettingsConfigDict | ||
6 | 6 | from starlette.middleware.sessions import SessionMiddleware |
7 | 7 | |
8 | 8 | |
9 | + | |
9 | 10 | app = FastAPI() |
10 | 11 | |
11 | 12 | class Settings(BaseSettings): |
12 | 13 | app_name: str = "Awesome API" |
13 | - session_secret: str | |
14 | - domain: str | |
15 | - clinet_id: str | |
16 | - client_secret: str | |
17 | - audience: str | |
14 | + session_secret: str="eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6InFiNjdBZlVtczdYSkVGemQtaWpsNiJ9.eyJpc3MiOiJodHRwczovL3Rlc3Rmb3Jwcm9qZWN0c3gudXMuYXV0aDAuY29tLyIsInN1YiI6IjB4T1owYlFENUZZWm5QcWhudnlNbU9YbEZ4RktnbFZQQGNsaWVudHMiLCJhdWQiOiJodHRwczovL3N3YWRoaW4ubXkuaWQvYXBpIiwiaWF0IjoxNzEwNTAwNjU1LCJleHAiOjE3MTA1ODcwNTUsImd0eSI6ImNsaWVudC1jcmVkZW50aWFscyIsImF6cCI6IjB4T1owYlFENUZZWm5QcWhudnlNbU9YbEZ4RktnbFZQIiwicGVybWlzc2lvbnMiOltdfQ.K8LfPg-1d4FelOsGLdr0_QS0tKZUlMotugI_zb20mEuF1Ql12Btx9rlxbYcTn2g-Hg7VYkMdK4Qw8fq9B_LelZVgcevGXqn6r6m42AKb4xCCzBUglWbgog0LQT2ogaYSRWsjrh6OorOfD-hG5Mfzpu-Gn8U92EkZYpIbPfdCvElssf-OLDOLGa2WrcP4eWpLMU26EUEX6DFCG_sJLb3ZhMC3UIYIj21vjntg2Pvs_4GYMBSLe2iG79QbB5VskD0Phq_qjn99oTePK3f0vfQy68OUt0xb7tb0ciYOKaELBS2b9n4hQpHAvGcVpD_czkbfX99H78padGJqPRJ06EdF4Q" | |
15 | + domain: str="testforprojectsx.us.auth0.com" | |
16 | + client_id: str="0xOZ0bQD5FYZnPqhnvyMmOXlFxFKglVP" | |
17 | + client_secret: str="aJTFI-G-I6pyd2r5pnKMTCEKyNgAztTb-F_5hAhEIVfA-rHcKVudsXlUhlopdDAd" | |
18 | + audience: str="https://swadhin.my.id/api" | |
18 | 19 | |
19 | - model_config=SettingsConfigDict(env_file=".env",env_prefix="auth0_") | |
20 | + #model_config=SettingsConfigDict(env_file=".env",env_prefix="auth0_") | |
20 | 21 | |
21 | 22 | |
22 | 23 | |
| @@ -24,9 +25,9 @@ auth0_config=Settings() | ||
24 | 25 | app.add_middleware(SessionMiddleware,secret_key=auth0_config.session_secret) |
25 | 26 | |
26 | 27 | |
27 | -auth=OAuth() | |
28 | -auth.register( | |
29 | - name="auth0", | |
28 | +authx=OAuth() | |
29 | +authx.register( | |
30 | + "auth0", | |
30 | 31 | client_id=auth0_config.client_id, |
31 | 32 | client_secret=auth0_config.client_secret, |
32 | 33 | server_metadata_url=f"https://{auth0_config.domain}/.well-known/openid-configuration", |
| @@ -40,24 +41,67 @@ def proctect(request:Request): | ||
40 | 41 | |
41 | 42 | def get_abs_path(route:str): |
42 | 43 | app_domain="http://localhost:8000" |
43 | - | |
44 | - return f"{app_domain}{app.url_path_for(route)}" | |
44 | + domainx=f"{app_domain}{app.url_path_for(route)}" | |
45 | + print(domainx) | |
46 | + return domainx | |
45 | 47 | |
46 | 48 | |
47 | -@app.route("") | |
49 | +@app.get("/") | |
48 | 50 | def home(): |
49 | - return {"message":"Hello World", | |
50 | - "login":get_abs_path("login"), | |
51 | - "logout":get_abs_path("logout"), | |
52 | - "profile":get_abs_path("profile") | |
53 | - } | |
54 | - | |
51 | + return {"message":"Hello World"} | |
52 | + | |
53 | +@app.get("/profile",dependencies=[Depends(proctect)]) | |
54 | +def profile(request:Request): | |
55 | + return request.session['token']['userinfo'] | |
56 | + | |
55 | 57 | |
56 | 58 | |
57 | -@app.route("/login") | |
59 | +@app.get("/login") | |
58 | 60 | async def login(request:Request): |
59 | - return await auth.auth0.authorize_redirect(request, | |
60 | - get_abs_path("callback"), | |
61 | - audience=auth0_config.audience, | |
62 | - ) | |
63 | - | |
61 | + f=authx.auth0.authorize_redirect(request,get_abs_path("login")) | |
62 | + return await f | |
63 | + | |
64 | +@app.get("/logout") | |
65 | +def logout(request:Request): | |
66 | + respose=RedirectResponse( | |
67 | + url="https://"+auth0_config.domain | |
68 | + +"/v2/logout?" | |
69 | + +urlencode({ | |
70 | + "returnTo":get_abs_path("home"), | |
71 | + "client_id":auth0_config.client_id, | |
72 | + }, | |
73 | + quote_via=quote_plus | |
74 | + ) | |
75 | + ) | |
76 | + request.session.clear() | |
77 | + return respose | |
78 | + | |
79 | + | |
80 | +@app.get("/callback") | |
81 | +async def callback(request): | |
82 | + token=await auth.auth0.authorize_access_token(request) | |
83 | + user=await auth.auth0.parse_id_token(request,token) | |
84 | + | |
85 | + request.session['token']=token | |
86 | + | |
87 | + return RedirectResponse(url=app.url_path_for("profile")) | |
88 | + | |
89 | +if __name__=="__main__": | |
90 | + import uvicorn | |
91 | + uvicorn.run(app,host="localhost",port=8000) | |
92 | + | |
93 | + | |
94 | + """ | |
95 | + | |
96 | + | |
97 | + https://testforprojectsx.us.auth0.com/authorize?response_type=code&client_id=0xOZ0bQD5FYZnPqhnvyMmOXlFxFKglVP&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fcallback&scope=openid+profile+email&state=TW9lIZYEsdESDppVodQE72wdT4j77p&audience=https%3A%2F%2Fswadhin.my.id%2Fapi&nonce=kJcF7V9renvLnCA4ap8F | |
98 | + | |
99 | + | |
100 | + | |
101 | + | |
102 | + | |
103 | + https://testforprojectsx.us.auth0.com/authorize?response_type=code&client_id=0xOZ0bQD5FYZnPqhnvyMmOXlFxFKglVP&redirect_uri=%2Fcallback&scope=openid+profile+email&state=iiXrFdF0AQ5hdifw2ZZY9YVS83p5aX&audience=https%3A%2F%2Fswadhin.my.id%2Fapi&nonce=RdpuWgjWmdUF9N6FyPC6 | |
104 | + | |
105 | + | |
106 | + | |
107 | + """ | |
64 | 108 | |