Skip to content

Commit 5e39001

Browse files
committed
add template environment variables file and updated flask auth code
1 parent 1dcda1c commit 5e39001

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,4 @@ monitor-django-apps/djmonitor/db.sqlite3
9797

9898
# Flask auth Okta post
9999
flask-auth-okta/openidconnect_secrets.json
100+
flask-auth-okta/.env

flask-auth-okta/app.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,54 @@
1+
# imports for both Flask and Okta connection
12
from os import environ
2-
from flask import Flask, Response
3+
from flask import Flask, Response, redirect, g, url_for
4+
from flask_oidc import OpenIDConnect
5+
from okta import UsersClient
36

47

58
app = Flask(__name__)
6-
app.config["DEBUG"] = True
79
# secret credentials for Okta connection
810
app.config["OIDC_CLIENT_SECRETS"] = "openidconnect_secrets.json"
911
app.config["OIDC_COOKIE_SECURE"] = False
1012
app.config["OIDC_CALLBACK_ROUTE"] = "/oidc/callback"
1113
app.config["OIDC_SCOPES"] = ["openid", "email", "profile"]
1214
app.config["SECRET_KEY"] = environ.get("SECRET_KEY")
1315
app.config["OIDC_ID_TOKEN_COOKIE_NAME"] = "oidc_token"
14-
# instantiate Open ID client to handle user session
16+
# instantiate OpenID client to handle user session
1517
oidc = OpenIDConnect(app)
1618
# Okta client will determine if a user has an appropriate account
1719
okta_client = UsersClient(environ.get("OKTA_ORG_URL"),
1820
environ.get("OKTA_AUTH_TOKEN"))
1921

2022

23+
@app.before_request
24+
def before_request():
25+
if oidc.user_loggedin:
26+
g.user = okta_client.get_user(oidc.user_getfield("sub"))
27+
else:
28+
g.user = None
29+
30+
2131
@app.route("/lair")
32+
@oidc.require_login
2233
def lair():
23-
return Response("Thundercats (supposed to be hidden) lair.")
34+
thundercats_lair = '<html><head><title>Thundercats, hoooo!</title></head><body><h1>Thundercats now hidden lair.</h1><iframe src="https://giphy.com/embed/ahXtBEbHiraxO" width="480" height="273" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/retro-cartoons-thundercats-ahXtBEbHiraxO">via GIPHY</a></p></body></html>'
35+
return Response(thundercats_lair)
2436

2537

2638
@app.route("/")
2739
def landing_page():
2840
return Response("Thundercats, Thundercats, hoooooooooooo!")
2941

42+
43+
@app.route("/login")
44+
@oidc.require_login
45+
def login():
46+
"""Force user to login and then redirect them to the lair.
47+
"""
48+
return redirect(url_for(".lair"))
49+
50+
51+
@app.route("/logout")
52+
def logout():
53+
oidc.logout()
54+
return redirect(url_for(".landing_page"))

flask-auth-okta/template.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export FLASK_ENV=development
2+
export SECRET_KEY=''
3+
export OKTA_ORG_URL=''
4+
export OKTA_AUTH_TOKEN=''

0 commit comments

Comments
 (0)