-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
71 lines (53 loc) · 1.7 KB
/
server.py
File metadata and controls
71 lines (53 loc) · 1.7 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Python Flask WebApp Authgear integration example
"""
import json
from os import environ as env
from authlib.integrations.flask_client import OAuth
from dotenv import find_dotenv, load_dotenv
from flask import Flask, redirect, render_template, session, url_for
from datetime import datetime
ENV_FILE = find_dotenv()
if ENV_FILE:
load_dotenv(ENV_FILE)
app = Flask(__name__)
app.secret_key = env.get("APP_SECRET_KEY")
app.config.from_object('config')
oauth = OAuth(app)
oauth.register(
"authgear",
client_kwargs={
"scope": "openid offline_access",
"grant_type": "authorization_code"
},
server_metadata_url=f'{env.get("AUTHGEAR_DOMAIN")}/.well-known/openid-configuration',
)
@app.route("/")
def home():
return render_template(
"home.html",
session=session.get("user"),
pretty=json.dumps(session.get("user"), indent=4),
)
@app.route("/user")
def user():
response = oauth.authgear.get(env.get("AUTHGEAR_DOMAIN") + "/oauth2/userinfo", token=session.get("user"))
profile = response.json()
return json.dumps(profile, indent=4)
@app.route("/callback", methods=["GET", "POST"])
def callback():
token = oauth.authgear.authorize_access_token(client_id=env.get("AUTHGEAR_CLIENT_ID"), client_secret=env.get("AUTHGEAR_CLIENT_SECRET"))
session["user"] = token
return redirect("/")
@app.route("/login")
def login():
return oauth.authgear.authorize_redirect(
redirect_uri=url_for("callback", _external=True)
)
@app.route("/logout")
def logout():
session.clear()
return redirect(env.get("AUTHGEAR_DOMAIN")
+ "/oauth2/end_session"
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=env.get("PORT", 3000))