Skip to content

Commit c0fbe1a

Browse files
Joël FranušićJoël Franušić
authored andcommitted
Add tweaks to README.md and add error handling to app.py
1 parent 3784d75 commit c0fbe1a

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

python-flask-sso-example/README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ An example Flask application demonstrating how to use the [WorkOS Python SDK](ht
4444

4545
5. Obtain and make note of the following values. In the next step, these will be set as environment variables.
4646

47-
- Your [WorkOS API key](https://dashboard.workos.com/api-keys)
48-
- Your [SSO-specific, WorkOS Client ID](https://dashboard.workos.com/configuration)
47+
- Your [WorkOS API key and Client ID](https://dashboard.workos.com/get-started)
4948

50-
6. Ensure you're in the root directory for the example app, `python-flask-sso-example/`. Create a `.env` file to securely store the environment variables. Open this file with the Nano text editor. (This file is listed in this repo's `.gitignore` file, so your sensitive information will not be checked into version control.)
49+
6. Ensure you're in the root directory for the example app, `python-flask-sso-example/`.
50+
7. Create a `.env` file to securely store the environment variables. Open this file with the Nano text editor. (This file is listed in this repo's `.gitignore` file, so your sensitive information will not be checked into version control.)
5151

5252
```bash
5353
(env) $ touch .env
@@ -57,11 +57,17 @@ An example Flask application demonstrating how to use the [WorkOS Python SDK](ht
5757
7. Once the Nano text editor opens, you can directly edit the `.env` file by listing the environment variables:
5858

5959
```bash
60-
WORKOS_API_KEY=<value found in step 6>
61-
WORKOS_CLIENT_ID=<value found in step 6>
60+
WORKOS_API_KEY=<value found in step 5>
61+
WORKOS_CLIENT_ID=<value found in step 5>
6262
APP_SECRET_KEY=<any string value you\'d like>
6363
```
6464

65+
If you are unsure what to use for the `APP_SECRET_KEY`, you can generate a random UUID using Python.
66+
67+
```bash
68+
(env) $ python3 -c "import uuid; print(uuid.uuid4())"
69+
```
70+
6571
To exit the Nano text editor, type `CTRL + x`. When prompted to "Save modified buffer", type `Y`, then press the `Enter` or `Return` key.
6672

6773
8. Source the environment variables so they are accessible to the operating system.
@@ -73,22 +79,22 @@ An example Flask application demonstrating how to use the [WorkOS Python SDK](ht
7379
You can ensure the environment variables were set correctly by running the following commands. The output should match the corresponding values.
7480

7581
```bash
76-
(env) $ echo $WORKOS_API_KEY
77-
(env) $ echo $WORKOS_CLIENT_ID
82+
(env) $ echo $WORKOS_API_KEY | grep sk_test_
83+
(env) $ echo $WORKOS_CLIENT_ID | grep client_
7884
```
7985

80-
9. In `python-flask-sso-example/app.py` change the `CUSTOMER_ORGANIZATION_ID` string value to the organization you will be testing the login for. This can be found in your WorkOS Dashboard.
86+
9. In `python-flask-sso-example/app.py` change the `CUSTOMER_ORGANIZATION_ID` string value to the organization you will be testing the login for. This can be found in your WorkOS Dashboard by clicking on the "Organizations" link on the left side of the dashboard.
8187

8288
10. The final setup step is to start the server.
8389

8490
```bash
85-
(env) $ flask run
91+
(env) $ flask run -h localhost
8692
```
8793

8894
If you are using macOS Monterey, port 5000 is not available and you'll need to start the app on a different port with this slightly different command.
8995
9096
```bash
91-
(env) $ flask run -p 5001
97+
(env) $ flask run -h localhost -p 5001
9298
```
9399
94100
You'll know the server is running when you see no errors in the CLI, and output similar to the following is displayed:
@@ -102,15 +108,15 @@ Use a production WSGI server instead.
102108
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
103109
```
104110

105-
Navigate to `localhost:5000`, or `localhost:5001` depending on which port you launched the server, in your web browser. You should see a "Login" button. If you click this link, you'll be redirected to an HTTP `404` page because we haven't set up SSO yet!
111+
Navigate to `localhost:5000`, or `localhost:5001` depending on which port you launched the server, in your web browser. You should see a "Login" button. If you click this link, you'll be redirected to an HTTP `404` page saying "Invalid redirect URI" because we haven't set up SSO yet!
106112

107113
You can stop the local Flask server for now by entering `CTRL + c` on the command line.
108114

109115
## SSO Setup with WorkOS
110116

111117
Follow the [SSO authentication flow instructions](https://workos.com/docs/sso/guide/introduction) to set up an SSO connection.
112118

113-
When you get to the step where you provide the `REDIRECT_URI` value, use http://localhost:5000/auth/callback.
119+
When you get to the step with the "Add Redirect URI" value, use `http://localhost:5000/auth/callback`
114120

115121
If you get stuck, please reach out to us at [email protected] so we can help.
116122

python-flask-sso-example/app.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import os
3-
from flask import Flask, session, redirect, render_template, request, url_for
3+
from flask import Flask, flash, redirect, render_template, request, session, url_for
44
import workos
55

66

@@ -37,7 +37,15 @@ def login():
3737
raw_profile=session["raw_profile"],
3838
)
3939
except KeyError:
40-
return render_template("login.html")
40+
if "error" in session:
41+
return render_template(
42+
"login.html",
43+
error=session.pop("error"),
44+
error_description=session.pop("error_description"),
45+
error_uri=session.pop("error_uri"),
46+
)
47+
else:
48+
return render_template("login.html")
4149

4250

4351
@app.route("/auth", methods=["POST"])
@@ -69,6 +77,10 @@ def auth():
6977
@app.route("/auth/callback")
7078
def auth_callback():
7179

80+
if "error" in request.args:
81+
session["error_description"] = request.args.get("error_description")
82+
session["error_uri"] = request.args.get("error_uri")
83+
session["error"] = request.args.get("error")
7284
code = request.args.get("code")
7385
# Why do I always get an error that the target does not belong to the target organization?
7486
if code is None:

python-flask-sso-example/static/login.css

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,12 @@ h1 {
371371
}
372372

373373
.error_message {
374-
color: #6363f1;
375-
margin-top: 0px;
376-
font-size: 12px;
374+
background-color: #f8d7da;
375+
border: 1px solid #e74c3c;
376+
padding: 10px;
377+
margin-bottom: 20px;
378+
border-radius: 5px;
379+
color: #c0291b;
377380
}
378381

379382
#noborder {

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,27 @@
2424
<a href="https://workos.com/" target="_blank"><button class='button button-outline'>WorkOS</button></a>
2525
</div>
2626
</div>
27+
{% if error_description %}
28+
<div class="flex flex_column error_message">
29+
<span>
30+
Error: <a href="{{ error_uri }}">
31+
{{ error }}
32+
</a>
33+
</span>
34+
<span>
35+
{{ error_description }}
36+
</span>
37+
</div>
38+
{% endif %}
2739
<div class="flex flex_column height-80vh">
28-
<div class='flex height-40vh'>
40+
<div class='flex height-40vh'>
2941
<div class="card height-315 width-335">
3042
<form method="POST" action="{{ url_for('auth') }}" class="mb-0">
3143
<div class='flex_column'>
3244
<div>
3345
<span>Log in with SSO</span>
3446
</div>
35-
<hr style="width:100%; margin-top: 15px; margin-bottom: 20px;">
47+
<hr style="width:100%; margin-top: 15px; margin-bottom: 20px;">
3648
<button id="Google" name="login_method" value="GoogleOAuth" class="card login_button google_button">
3749
<span>Google OAuth</span>
3850
</button>

0 commit comments

Comments
 (0)