forked from tomasvotava/fastapi-sso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.py
41 lines (31 loc) · 968 Bytes
/
google.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
32
33
34
35
36
37
38
39
40
41
"""Google Login Example
"""
import os
import uvicorn
from fastapi import FastAPI, Request
from fastapi_sso.sso.google import GoogleSSO
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
CLIENT_ID = os.environ["CLIENT_ID"]
CLIENT_SECRET = os.environ["CLIENT_SECRET"]
app = FastAPI()
sso = GoogleSSO(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri="http://localhost:5000/auth/callback",
allow_insecure_http=True,
use_state=False,
)
@app.get("/auth/login")
async def auth_init():
"""Initialize auth and redirect"""
return await sso.get_login_redirect(params={"prompt": "consent", "access_type": "offline"})
@app.get("/auth/callback")
async def auth_callback(request: Request):
"""Verify login"""
user = await sso.verify_and_process(request)
return user
if __name__ == "__main__":
try:
uvicorn.run(app="examples.google:app", host="127.0.0.1", port=5000)
except KeyboardInterrupt:
pass