forked from tomasvotava/fastapi-sso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric.py
31 lines (24 loc) · 914 Bytes
/
generic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""This is an example usage of fastapi-sso.
"""
from fastapi import FastAPI, HTTPException
from starlette.requests import Request
from fastapi_sso.sso.google import GoogleSSO
app = FastAPI()
google_sso = GoogleSSO("my-client-id", "my-client-secret", "https://my.awesome-web.com/google/callback")
@app.get("/google/login")
async def google_login():
"""Generate login url and redirect"""
return await google_sso.get_login_redirect()
@app.get("/google/callback")
async def google_callback(request: Request):
"""Process login response from Google and return user info"""
user = await google_sso.verify_and_process(request)
if user is None:
raise HTTPException(401, "Failed to fetch user information")
return {
"id": user.id,
"picture": user.picture,
"display_name": user.display_name,
"email": user.email,
"provider": user.provider,
}