Skip to content

Commit 6b32200

Browse files
committed
Update sample for 5.0
1 parent e4c08eb commit 6b32200

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed
Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import os
2-
2+
from typing import TypeGuard, get_args
33
from flask import Flask, redirect, render_template, request, url_for
44
import workos
55
from workos import client as workos_client
6-
from workos import portal
76
from flask_lucide import Lucide
7+
import workos.organizations
8+
import workos.portal
9+
import workos.resources
10+
import workos.resources.organizations
11+
import workos.resources.portal
12+
import workos.resources.sso
813

914

1015
# Flask Setup
@@ -14,10 +19,16 @@
1419

1520
# WorkOS Setup
1621
workos.api_key = os.getenv("WORKOS_API_KEY")
17-
workos.project_id = os.getenv("WORKOS_CLIENT_ID")
22+
workos.client_id = os.getenv("WORKOS_CLIENT_ID")
1823
workos.base_api_url = "http://localhost:7000/" if DEBUG else workos.base_api_url
1924

2025

26+
def is_portal_link_intent(
27+
value: str,
28+
) -> TypeGuard[workos.portal.PortalLinkIntent]:
29+
return value in get_args(workos.portal.PortalLinkIntent)
30+
31+
2132
@app.route("/")
2233
def index():
2334
return render_template("index.html")
@@ -32,21 +43,39 @@ def provision_enterprise():
3243

3344
# Check if a matching domain already exists and set global org_id if there is a match
3445
orgs = workos_client.organizations.list_organizations(domains=organization_domains)
35-
if len(orgs["data"]) > 0:
36-
org_id = orgs["data"][0]["id"]
46+
if len(orgs.data) > 0:
47+
org_id = orgs.data[0].id
3748

3849
# Otherwise create a new Organization and set the global org_id
3950
else:
51+
domain_data = list(
52+
map(
53+
lambda domain: workos.organizations.DomainDataInput(
54+
{"domain": domain, "state": "verified"}
55+
),
56+
organization_domains,
57+
)
58+
)
59+
4060
organization = workos_client.organizations.create_organization(
41-
{"name": organization_name, "domains": organization_domains}
61+
name=organization_name,
62+
domain_data=domain_data,
4263
)
43-
org_id = organization["id"]
64+
org_id = organization.id
4465

4566
return render_template("org_logged_in.html")
4667

4768

4869
@app.route("/launch_admin_portal", methods=["GET", "POST"])
4970
def launch_admin_portal():
5071
intent = request.args.get("intent")
51-
portal_link = workos_client.portal.generate_link(organization=org_id, intent=intent)
52-
return redirect(portal_link["link"])
72+
if intent is None:
73+
return "Missing intent parameter", 400
74+
75+
if not is_portal_link_intent(intent):
76+
return "Invalid intent parameter", 400
77+
78+
portal_link = workos_client.portal.generate_link(
79+
organization_id=org_id, intent=intent
80+
)
81+
return redirect(portal_link.link)

python-flask-admin-portal-example/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ MarkupSafe==2.0.1
99
requests==2.26.0
1010
urllib3==1.26.7
1111
Werkzeug==2.0.1
12-
workos>=1.23.3
12+
../../SDKs/workos-python
13+
workos>=4.0.0
1314
python-dotenv
1415
flask-lucide==0.2.0

0 commit comments

Comments
 (0)