-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
53 lines (40 loc) · 1.25 KB
/
main.py
File metadata and controls
53 lines (40 loc) · 1.25 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
import os
import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi_sso.sso.github import GithubSSO
from dotenv import load_dotenv
load_dotenv()
CLIENT_ID = os.getenv('CLIENT_ID_GH')
CLIENT_SECRET = os.getenv('CLIENT_SECRET_GH')
app = FastAPI()
sso = GithubSSO(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri="http://127.0.0.1:5000/auth/callback",
allow_insecure_http=True,
)
@app.get("/auth/login")
async def auth_init():
"""Initialize auth and redirect"""
async with sso:
return await sso.get_login_redirect()
@app.get("/auth/callback")
async def auth_callback(request: Request):
"""Verify login"""
async with sso:
user = await sso.verify_and_process(request)
return user
@app.get("/", response_class=HTMLResponse) # Specify HTMLResponse as response_class
async def read_root():
"""Return HTML page with login form"""
html = """
<form class="col-lg-2" method="get" action="/auth/login" style="text-align: center;">
<button class="login-btn" type="submit">
Login with GitHub
</button>
</form>
"""
return html
if __name__ == "__main__":
uvicorn.run(app="main:app", host="127.0.0.1", port=5000)