Skip to content

Commit 31a2891

Browse files
awolfdenAdam Wolfman
andauthored
Update UI/UX for Flask SSO app (#38)
* Update UI/UX for flask SSO app * Remove print statements * Update readme --------- Co-authored-by: Adam Wolfman <[email protected]>
1 parent 3a9205b commit 31a2891

File tree

10 files changed

+389
-213
lines changed

10 files changed

+389
-213
lines changed

python-flask-sso-example/README.md

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# python-flask-sso-example
2+
23
An example Flask application demonstrating how to use the [WorkOS Python SDK](https://github.com/workos/workos-python) to authenticate users via SSO.
34

45
## Prerequisites
5-
- Python 3.6+
66

7+
- Python 3.6+
78

89
## Flask Project Setup
910

1011
1. Clone the main git repo for these Python example apps using your preferred secure method (HTTPS or SSH).
12+
1113
```bash
1214
# HTTPS
1315
$ git clone https://github.com/workos/python-flask-example-applications.git
@@ -21,33 +23,39 @@ An example Flask application demonstrating how to use the [WorkOS Python SDK](ht
2123
```
2224

2325
2. Navigate to the sso app within the cloned repo.
26+
2427
```bash
2528
$ cd python-flask-example-applications/python-flask-sso-example
2629
```
2730

2831
3. Create and source a Python virtual environment. You should then see `(env)` at the beginning of your command-line prompt.
32+
2933
```bash
3034
$ python3 -m venv env
3135
$ source env/bin/activate
3236
(env) $
3337
```
3438

3539
4. Install the cloned app's dependencies.
40+
3641
```bash
3742
(env) $ pip install -r requirements.txt
3843
```
3944

4045
5. Obtain and make note of the following values. In the next step, these will be set as environment variables.
46+
4147
- Your [WorkOS API key](https://dashboard.workos.com/api-keys)
4248
- Your [SSO-specific, WorkOS Client ID](https://dashboard.workos.com/configuration)
4349

4450
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.)
51+
4552
```bash
4653
(env) $ touch .env
4754
(env) $ nano .env
4855
```
4956

5057
7. Once the Nano text editor opens, you can directly edit the `.env` file by listing the environment variables:
58+
5159
```bash
5260
WORKOS_API_KEY=<value found in step 6>
5361
WORKOS_CLIENT_ID=<value found in step 6>
@@ -57,43 +65,46 @@ An example Flask application demonstrating how to use the [WorkOS Python SDK](ht
5765
To exit the Nano text editor, type `CTRL + x`. When prompted to "Save modified buffer", type `Y`, then press the `Enter` or `Return` key.
5866

5967
8. Source the environment variables so they are accessible to the operating system.
68+
6069
```bash
6170
(env) $ source .env
6271
```
6372

6473
You can ensure the environment variables were set correctly by running the following commands. The output should match the corresponding values.
74+
6575
```bash
6676
(env) $ echo $WORKOS_API_KEY
6777
(env) $ echo $WORKOS_CLIENT_ID
6878
```
6979

70-
9. In `python-flask-sso-example/app.py` change the `CUSTOMER_CONNECTION_ID` string value to the connection you will be testing the login for. This can be found in your WorkOS Dashboard.
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.
7181

7282
10. The final setup step is to start the server.
73-
```bash
74-
(env) $ flask run
75-
```
7683

77-
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.
78-
```bash
79-
(env) $ flask run -p 5001
80-
```
84+
```bash
85+
(env) $ flask run
86+
```
8187

82-
You'll know the server is running when you see no errors in the CLI, and output similar to the following is displayed:
88+
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.
8389

84-
```bash
85-
* Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.
86-
* Environment: production
87-
WARNING: This is a development server. Do not use it in a production deployment.
88-
Use a production WSGI server instead.
89-
* Debug mode: off
90-
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
91-
```
90+
```bash
91+
(env) $ flask run -p 5001
92+
```
93+
94+
You'll know the server is running when you see no errors in the CLI, and output similar to the following is displayed:
9295

93-
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!
96+
```bash
97+
* Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.
98+
* Environment: production
99+
WARNING: This is a development server. Do not use it in a production deployment.
100+
Use a production WSGI server instead.
101+
* Debug mode: off
102+
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
103+
```
94104

95-
You can stop the local Flask server for now by entering `CTRL + c` on the command line.
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!
96106

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

98109
## SSO Setup with WorkOS
99110

@@ -107,15 +118,15 @@ If you get stuck, please reach out to us at [email protected] so we can help.
107118

108119
11. Naviagte to the `python-flask-sso-example` directory. Source the virtual environment we created earlier, if it isn't still activated from the steps above. Start the Flask server locally.
109120

110-
```bash
111-
$ cd ~/Desktop/python-flask-sso-example/
112-
$ source env/bin/activate
113-
(env) $ flask run
114-
```
121+
```bash
122+
$ cd ~/Desktop/python-flask-sso-example/
123+
$ source env/bin/activate
124+
(env) $ flask run
125+
```
115126

116-
Once running, navigate to `localhost:5000`, or `localhost:5001` depending on which port you launched the server, to test out the SSO workflow.
127+
Once running, navigate to `localhost:5000`, or `localhost:5001` depending on which port you launched the server, to test out the SSO workflow.
117128

118-
Hooray!
129+
Hooray!
119130

120131
## Need help?
121132

python-flask-sso-example/app.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
workos.project_id = os.getenv("WORKOS_CLIENT_ID")
1717
workos.base_api_url = "http://localhost:7000/" if DEBUG else workos.base_api_url
1818

19-
# Enter Connection ID here
19+
# Enter Organization ID here
2020

21-
CUSTOMER_CONNECTION_ID = ""
21+
CUSTOMER_ORGANIZATION_ID = ""
2222

2323

2424
def to_pretty_json(value):
@@ -40,14 +40,19 @@ def login():
4040
return render_template("login.html")
4141

4242

43-
@app.route("/auth")
43+
@app.route("/auth", methods=["POST"])
4444
def auth():
4545

46-
authorization_url = workos.client.sso.get_authorization_url(
47-
redirect_uri=url_for("auth_callback", _external=True),
48-
state={},
49-
connection=CUSTOMER_CONNECTION_ID,
50-
)
46+
login_type = request.form.get("login_method")
47+
48+
params = {"redirect_uri": url_for("auth_callback", _external=True), "state": {}}
49+
50+
if login_type == "saml":
51+
params["organization"] = CUSTOMER_ORGANIZATION_ID
52+
else:
53+
params["provider"] = login_type
54+
55+
authorization_url = workos.client.sso.get_authorization_url(**params)
5156

5257
return redirect(authorization_url)
5358

Binary file not shown.
Loading
Loading
Loading

0 commit comments

Comments
 (0)