Skip to content

Commit 796cd92

Browse files
awolfdenAdam WolfmanAdam Wolfman
authored
Refactor Flask SSO app to use Sessions (#6)
* Update Flask apps with caveat regarding port use with Mac OS Monterey and removed buggy user image logic from logged in page on SSO app * Refactor Flask SSO app to use sessions and clean up unused static files on dsync app * Update readme to reflect need for secret key env variable Co-authored-by: Adam Wolfman <[email protected]> Co-authored-by: Adam Wolfman <[email protected]>
1 parent 53be174 commit 796cd92

File tree

6 files changed

+35
-9
lines changed

6 files changed

+35
-9
lines changed

python-flask-directory-sync-example/app.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ def directory_groups():
5252

5353
return render_template('groups.html', groups=groups)
5454

55+
@app.route('/update_org', methods=['GET', 'POST'])
56+
def update_org():
57+
print(request)
58+
response = workos_client.organizations.update_organization(
59+
organization="org_01FGM2T96YX19Z4HENZ1AC7848",
60+
name="Planet Express",
61+
domains=["newboom.com", "workos.com", "hotmail.com", "boom.com"]
62+
)
63+
print(json.dumps(response))
64+
return json.dumps(response)
65+
5566
@app.route('/webhooks', methods=['GET', 'POST'])
5667
def webhooks():
5768
print(request)
Binary file not shown.
Binary file not shown.

python-flask-sso-example/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ An example Flask application demonstrating how to use the [WorkOS Python SDK](ht
5454

5555
8. Once the Nano text editor opens, you can directly edit the `.env` file by listing the environment variables:
5656
```bash
57-
export WORKOS_API_KEY=<value found in step 6>
58-
export WORKOS_CLIENT_ID=<value found in step 6>
57+
WORKOS_API_KEY=<value found in step 6>
58+
WORKOS_CLIENT_ID=<value found in step 6>
59+
APP_SECRET_KEY=<any string value you\'d like>
5960
```
6061

6162
To exit the Nano text editor, type `CTRL + x`. When prompted to "Save modified buffer", type `Y`, then press the `Enter` or `Return` key.

python-flask-sso-example/app.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import os
22

3-
from flask import (Flask, redirect, render_template, request, url_for)
3+
from flask import (Flask, session, redirect, render_template, request, url_for)
44
import workos
55

66

77
# Flask Setup
88
DEBUG = False
99
app = Flask(__name__)
10+
app.secret_key = os.getenv('APP_SECRET_KEY')
1011

1112
# WorkOS Setup
1213

@@ -15,11 +16,16 @@
1516
workos.base_api_url = 'http://localhost:7000/' if DEBUG else workos.base_api_url
1617

1718
# Enter Connection ID here
19+
1820
CUSTOMER_CONNECTION_ID = 'xxx'
1921

22+
2023
@app.route('/')
2124
def login():
22-
return render_template('login.html')
25+
if session:
26+
return render_template('login_successful.html', first_name=session['first_name'], raw_profile=session['raw_profile'])
27+
else:
28+
return render_template('login.html')
2329

2430
@app.route('/auth')
2531
def auth():
@@ -36,11 +42,18 @@ def auth():
3642
@app.route('/auth/callback')
3743
def auth_callback():
3844
code = request.args.get('code')
39-
print(code)
4045
profile = workos.client.sso.get_profile_and_token(code)
4146
p_profile = profile.to_dict()
42-
first_name = p_profile['profile']['first_name']
43-
44-
raw_profile = p_profile['profile']
47+
session['first_name'] = p_profile['profile']['first_name']
48+
session['raw_profile'] = p_profile['profile']
49+
session['session_id'] = p_profile['profile']['id']
50+
print('session', session)
51+
return redirect('/')
52+
4553

46-
return render_template('login_successful.html', first_name=first_name, raw_profile=raw_profile)
54+
@app.route('/logout')
55+
def logout():
56+
# remove the username from the session if it is there
57+
session.clear()
58+
print(session)
59+
return redirect('/')

python-flask-sso-example/templates/login_successful.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ <h2>Raw Profile Response Details</h2>
3131
<div class="text_box">
3232
<p>{{raw_profile}}</p>
3333
</div>
34+
<a href="/logout"><button class="button">Log Out</button></a>
3435
</div>
3536

3637
</div>

0 commit comments

Comments
 (0)