Skip to content

Commit 9ea73db

Browse files
authored
Update sample apps for Python SDK v5 (#59)
Update sample apps for Python SDK v5
1 parent 561a369 commit 9ea73db

File tree

22 files changed

+544
-240
lines changed

22 files changed

+544
-240
lines changed
+32-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1+
from email.mime import base
12
import os
2-
3-
from flask import Flask, redirect, render_template, request, url_for
3+
from flask import Flask, redirect, render_template, request
44
import workos
5-
from workos import client as workos_client
6-
from workos import portal
75
from flask_lucide import Lucide
6+
from workos.types import DomainDataInput
87

98

109
# Flask Setup
11-
DEBUG = False
1210
app = Flask(__name__)
1311
lucide = Lucide(app)
1412

1513
# WorkOS Setup
16-
workos.api_key = os.getenv("WORKOS_API_KEY")
17-
workos.project_id = os.getenv("WORKOS_CLIENT_ID")
18-
workos.base_api_url = "http://localhost:7000/" if DEBUG else workos.base_api_url
14+
base_api_url = os.getenv("WORKOS_BASE_API_URL")
15+
workos_client = workos.WorkOSClient(
16+
api_key=os.getenv("WORKOS_API_KEY"),
17+
client_id=os.getenv("WORKOS_CLIENT_ID"),
18+
base_url=base_api_url,
19+
)
1920

2021

2122
@app.route("/")
@@ -32,21 +33,38 @@ def provision_enterprise():
3233

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

3839
# Otherwise create a new Organization and set the global org_id
3940
else:
41+
domain_data = list(
42+
map(
43+
lambda domain: DomainDataInput(domain=domain, state="verified"),
44+
organization_domains,
45+
)
46+
)
47+
4048
organization = workos_client.organizations.create_organization(
41-
{"name": organization_name, "domains": organization_domains}
49+
name=organization_name,
50+
domain_data=domain_data,
4251
)
43-
org_id = organization["id"]
52+
org_id = organization.id
4453

4554
return render_template("org_logged_in.html")
4655

4756

4857
@app.route("/launch_admin_portal", methods=["GET", "POST"])
4958
def launch_admin_portal():
5059
intent = request.args.get("intent")
51-
portal_link = workos_client.portal.generate_link(organization=org_id, intent=intent)
52-
return redirect(portal_link["link"])
60+
61+
if intent is None:
62+
return "Missing intent parameter", 400
63+
64+
if not intent in tuple(("audit_logs", "dsync", "log_streams", "sso")):
65+
return "Invalid intent parameter", 400
66+
67+
portal_link = workos_client.portal.generate_link(
68+
organization_id=org_id, intent=intent
69+
)
70+
return redirect(portal_link.link)
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
certifi==2021.5.30
22
charset-normalizer==2.0.6
33
click==8.0.1
4-
Flask==2.0.1
4+
Flask==2.0.3
55
idna==3.2
66
itsdangerous==2.0.1
77
Jinja2==3.0.1
88
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+
workos>=5.0.0
1313
python-dotenv
1414
flask-lucide==0.2.0

python-flask-audit-logs-example/app.py

+63-50
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
import json
22
import os
3-
from urllib.parse import urlparse, parse_qs
4-
from flask import Flask, session, redirect, render_template, request, url_for
3+
from flask import Flask, session, redirect, render_template, request
54
import workos
65
from datetime import datetime, timedelta
76
from audit_log_events import (
87
user_organization_set,
98
)
9+
from workos.audit_logs import AuditLogEvent
1010
from flask_lucide import Lucide
1111

1212

1313
# Flask Setup
14-
DEBUG = False
1514
app = Flask(__name__)
1615
app.secret_key = os.getenv("APP_SECRET_KEY")
1716

1817
lucide = Lucide(app)
1918

2019

2120
# WorkOS Setup
22-
workos.api_key = os.getenv("WORKOS_API_KEY")
23-
workos.project_id = os.getenv("WORKOS_CLIENT_ID")
24-
workos.base_api_url = "http://localhost:7000/" if DEBUG else workos.base_api_url
21+
base_api_url = os.getenv("WORKOS_BASE_API_URL")
22+
workos_client = workos.WorkOSClient(
23+
api_key=os.getenv("WORKOS_API_KEY"),
24+
client_id=os.getenv("WORKOS_CLIENT_ID"),
25+
base_url=base_api_url,
26+
)
2527

2628

2729
def to_pretty_json(value):
@@ -34,14 +36,14 @@ def to_pretty_json(value):
3436
@app.route("/", methods=["POST", "GET"])
3537
def index():
3638
try:
37-
link = workos.client.portal.generate_link(
38-
organization=session["organization_id"], intent="audit_logs"
39+
link = workos_client.portal.generate_link(
40+
organization_id=session["organization_id"], intent="audit_logs"
3941
)
4042
today = datetime.today()
4143
last_month = today - timedelta(days=30)
4244
return render_template(
4345
"send_events.html",
44-
link=link["link"],
46+
link=link.link,
4547
organization_id=session["organization_id"],
4648
org_name=session["organization_name"],
4749
last_month_iso=last_month.isoformat(),
@@ -50,29 +52,29 @@ def index():
5052
except KeyError:
5153
before = request.args.get("before")
5254
after = request.args.get("after")
53-
organizations = workos.client.organizations.list_organizations(
54-
before=before, after=after, limit=5, order=None
55+
organizations = workos_client.organizations.list_organizations(
56+
before=before, after=after, limit=5, order="desc"
5557
)
56-
before = organizations["listMetadata"]["before"]
57-
after = organizations["listMetadata"]["after"]
58+
before = organizations.list_metadata.before
59+
after = organizations.list_metadata.after
5860
return render_template(
5961
"login.html",
60-
organizations=organizations["data"],
62+
organizations=organizations.data,
6163
before=before,
6264
after=after,
6365
)
6466

6567

6668
@app.route("/set_org", methods=["POST", "GET"])
6769
def set_org():
68-
organization_id = request.args.get("id")
70+
organization_id = request.args.get("id") or request.form["organization_id"]
71+
6972
session["organization_id"] = organization_id
70-
organization_set = workos.client.audit_logs.create_event(
71-
organization_id, user_organization_set
73+
workos_client.audit_logs.create_event(
74+
organization_id=organization_id, event=user_organization_set
7275
)
73-
org = workos.client.organizations.get_organization(organization_id)
74-
org_name = org["name"]
75-
session["organization_name"] = org_name
76+
org = workos_client.organizations.get_organization(organization_id)
77+
session["organization_name"] = org.name
7678
return redirect("/")
7779

7880

@@ -87,28 +89,32 @@ def send_event():
8789
)
8890
organization_id = session["organization_id"]
8991

90-
event = {
91-
"action": "user.organization_deleted",
92-
"version": int(event_version),
93-
"occurred_at": datetime.now().isoformat(),
94-
"actor": {
95-
"type": actor_type,
96-
"name": actor_name,
97-
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
98-
},
99-
"targets": [
100-
{
101-
"type": target_type,
102-
"name": target_name,
103-
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
92+
event = AuditLogEvent(
93+
{
94+
"action": "user.organization_deleted",
95+
"version": int(event_version),
96+
"occurred_at": datetime.now().isoformat(),
97+
"actor": {
98+
"type": actor_type,
99+
"name": actor_name,
100+
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
104101
},
105-
],
106-
"context": {
107-
"location": "123.123.123.123",
108-
"user_agent": "Chrome/104.0.0.0",
109-
},
110-
}
111-
organization_set = workos.client.audit_logs.create_event(organization_id, event)
102+
"targets": [
103+
{
104+
"type": target_type,
105+
"name": target_name,
106+
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
107+
},
108+
],
109+
"context": {
110+
"location": "123.123.123.123",
111+
"user_agent": "Chrome/104.0.0.0",
112+
},
113+
}
114+
)
115+
organization_set = workos_client.audit_logs.create_event(
116+
organization_id=organization_id, event=event
117+
)
112118
return redirect("/")
113119

114120

@@ -147,33 +153,40 @@ def get_events():
147153

148154
try:
149155

150-
create_export_response = workos.client.audit_logs.create_export(
151-
organization=organization_id,
156+
create_export_response = workos_client.audit_logs.create_export(
157+
organization_id=organization_id,
152158
range_start=request.form["range-start"],
153159
range_end=request.form["range-end"],
154160
actions=actions,
155-
actors=actors,
161+
actor_names=actors,
156162
targets=targets,
157163
)
158-
session["export_id"] = create_export_response.to_dict()["id"]
164+
session["export_id"] = create_export_response.id
159165

160166
return redirect("export_events")
161167
except Exception as e:
162168
print(str(e))
163169
return redirect("/")
164170
if event_type == "access_csv":
165171
export_id = session["export_id"]
166-
fetch_export_response = workos.client.audit_logs.get_export(export_id)
167-
return redirect(fetch_export_response.to_dict()["url"])
172+
fetch_export_response = workos_client.audit_logs.get_export(export_id)
173+
if fetch_export_response.url is None:
174+
return redirect("/")
175+
176+
return redirect(fetch_export_response.url)
168177

169178

170179
@app.route("/events", methods=["GET"])
171180
def events():
172-
link = workos.client.portal.generate_link(
173-
organization=session["organization_id"], intent=request.args.get("intent")
181+
intent = request.args.get("intent")
182+
if not intent == "audit_logs":
183+
return redirect("/")
184+
185+
link = workos_client.portal.generate_link(
186+
organization_id=session["organization_id"], intent=intent
174187
)
175188

176-
return redirect(link["link"])
189+
return redirect(link.link)
177190

178191

179192
@app.route("/logout")
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
from datetime import datetime
2+
from workos.audit_logs import AuditLogEvent
23

3-
user_organization_set = {
4-
"action": "user.organization_set",
5-
"occurred_at": datetime.now().isoformat(),
6-
"actor": {
7-
"type": "user",
8-
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
9-
},
10-
"targets": [
11-
{
12-
"type": "team",
13-
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
4+
user_organization_set = AuditLogEvent(
5+
{
6+
"action": "user.organization_set",
7+
"occurred_at": datetime.now().isoformat(),
8+
"actor": {
9+
"type": "user",
10+
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
1411
},
15-
],
16-
"context": {
17-
"location": "123.123.123.123",
18-
"user_agent": "Chrome/104.0.0.0",
19-
},
20-
}
12+
"targets": [
13+
{
14+
"type": "organization",
15+
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
16+
},
17+
],
18+
"context": {
19+
"location": "123.123.123.123",
20+
"user_agent": "Chrome/104.0.0.0",
21+
},
22+
}
23+
)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Flask==2.0.3
22
Jinja2==3.1.1
3-
workos>=1.23.3
3+
workos>=5.0.0
44
python-dotenv
5-
flask-lucide==0.2.0
5+
flask-lucide==0.2.0
6+
Werkzeug==2.0.1

0 commit comments

Comments
 (0)