|
| 1 | +# imports for both Flask and Okta connection |
1 | 2 | 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 |
3 | 6 |
|
4 | 7 |
|
5 | 8 | app = Flask(__name__)
|
6 |
| -app.config["DEBUG"] = True |
7 | 9 | # secret credentials for Okta connection
|
8 | 10 | app.config["OIDC_CLIENT_SECRETS"] = "openidconnect_secrets.json"
|
9 | 11 | app.config["OIDC_COOKIE_SECURE"] = False
|
10 | 12 | app.config["OIDC_CALLBACK_ROUTE"] = "/oidc/callback"
|
11 | 13 | app.config["OIDC_SCOPES"] = ["openid", "email", "profile"]
|
12 | 14 | app.config["SECRET_KEY"] = environ.get("SECRET_KEY")
|
13 | 15 | 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 |
15 | 17 | oidc = OpenIDConnect(app)
|
16 | 18 | # Okta client will determine if a user has an appropriate account
|
17 | 19 | okta_client = UsersClient(environ.get("OKTA_ORG_URL"),
|
18 | 20 | environ.get("OKTA_AUTH_TOKEN"))
|
19 | 21 |
|
20 | 22 |
|
| 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 | + |
21 | 31 | @app.route("/lair")
|
| 32 | +@oidc.require_login |
22 | 33 | 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) |
24 | 36 |
|
25 | 37 |
|
26 | 38 | @app.route("/")
|
27 | 39 | def landing_page():
|
28 | 40 | return Response("Thundercats, Thundercats, hoooooooooooo!")
|
29 | 41 |
|
| 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")) |
0 commit comments