1
1
import json
2
2
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
5
4
import workos
6
5
from datetime import datetime , timedelta
7
6
from audit_log_events import (
8
7
user_organization_set ,
9
8
)
9
+ from workos .audit_logs import AuditLogEvent
10
10
from flask_lucide import Lucide
11
11
12
12
20
20
21
21
# WorkOS Setup
22
22
workos .api_key = os .getenv ("WORKOS_API_KEY" )
23
- workos .project_id = os .getenv ("WORKOS_CLIENT_ID" )
23
+ workos .client_id = os .getenv ("WORKOS_CLIENT_ID" )
24
24
workos .base_api_url = "http://localhost:7000/" if DEBUG else workos .base_api_url
25
25
26
26
@@ -35,13 +35,13 @@ def to_pretty_json(value):
35
35
def index ():
36
36
try :
37
37
link = workos .client .portal .generate_link (
38
- organization = session ["organization_id" ], intent = "audit_logs"
38
+ organization_id = session ["organization_id" ], intent = "audit_logs"
39
39
)
40
40
today = datetime .today ()
41
41
last_month = today - timedelta (days = 30 )
42
42
return render_template (
43
43
"send_events.html" ,
44
- link = link [ " link" ] ,
44
+ link = link . link ,
45
45
organization_id = session ["organization_id" ],
46
46
org_name = session ["organization_name" ],
47
47
last_month_iso = last_month .isoformat (),
@@ -51,28 +51,28 @@ def index():
51
51
before = request .args .get ("before" )
52
52
after = request .args .get ("after" )
53
53
organizations = workos .client .organizations .list_organizations (
54
- before = before , after = after , limit = 5 , order = None
54
+ before = before , after = after , limit = 5 , order = "desc"
55
55
)
56
- before = organizations [ "listMetadata" ][ " before" ]
57
- after = organizations [ "listMetadata" ][ " after" ]
56
+ before = organizations . list_metadata . before
57
+ after = organizations . list_metadata . after
58
58
return render_template (
59
59
"login.html" ,
60
- organizations = organizations [ " data" ] ,
60
+ organizations = organizations . data ,
61
61
before = before ,
62
62
after = after ,
63
63
)
64
64
65
65
66
66
@app .route ("/set_org" , methods = ["POST" , "GET" ])
67
67
def set_org ():
68
- organization_id = request .args .get ("id" )
68
+ organization_id = request .args .get ("id" ) or request .form ["organization_id" ]
69
+
69
70
session ["organization_id" ] = organization_id
70
71
organization_set = workos .client .audit_logs .create_event (
71
72
organization_id , user_organization_set
72
73
)
73
74
org = workos .client .organizations .get_organization (organization_id )
74
- org_name = org ["name" ]
75
- session ["organization_name" ] = org_name
75
+ session ["organization_name" ] = org .name
76
76
return redirect ("/" )
77
77
78
78
@@ -87,27 +87,29 @@ def send_event():
87
87
)
88
88
organization_id = session ["organization_id" ]
89
89
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" ,
90
+ event = AuditLogEvent (
91
+ {
92
+ "action" : "user.organization_deleted" ,
93
+ "version" : int (event_version ),
94
+ "occurred_at" : datetime .now ().isoformat (),
95
+ "actor" : {
96
+ "type" : actor_type ,
97
+ "name" : actor_name ,
98
+ "id" : "user_01GBNJC3MX9ZZJW1FSTF4C5938" ,
99
+ },
100
+ "targets" : [
101
+ {
102
+ "type" : target_type ,
103
+ "name" : target_name ,
104
+ "id" : "team_01GBNJD4MKHVKJGEWK42JNMBGS" ,
105
+ },
106
+ ],
107
+ "context" : {
108
+ "location" : "123.123.123.123" ,
109
+ "user_agent" : "Chrome/104.0.0.0" ,
104
110
},
105
- ],
106
- "context" : {
107
- "location" : "123.123.123.123" ,
108
- "user_agent" : "Chrome/104.0.0.0" ,
109
- },
110
- }
111
+ }
112
+ )
111
113
organization_set = workos .client .audit_logs .create_event (organization_id , event )
112
114
return redirect ("/" )
113
115
@@ -148,14 +150,14 @@ def get_events():
148
150
try :
149
151
150
152
create_export_response = workos .client .audit_logs .create_export (
151
- organization = organization_id ,
153
+ organization_id = organization_id ,
152
154
range_start = request .form ["range-start" ],
153
155
range_end = request .form ["range-end" ],
154
156
actions = actions ,
155
- actors = actors ,
157
+ actor_names = actors ,
156
158
targets = targets ,
157
159
)
158
- session ["export_id" ] = create_export_response .to_dict ()[ "id" ]
160
+ session ["export_id" ] = create_export_response .id
159
161
160
162
return redirect ("export_events" )
161
163
except Exception as e :
@@ -164,16 +166,23 @@ def get_events():
164
166
if event_type == "access_csv" :
165
167
export_id = session ["export_id" ]
166
168
fetch_export_response = workos .client .audit_logs .get_export (export_id )
167
- return redirect (fetch_export_response .to_dict ()["url" ])
169
+ if fetch_export_response .url is None :
170
+ return redirect ("/" )
171
+
172
+ return redirect (fetch_export_response .url )
168
173
169
174
170
175
@app .route ("/events" , methods = ["GET" ])
171
176
def events ():
177
+ intent = request .args .get ("intent" )
178
+ if not intent == "audit_logs" :
179
+ return redirect ("/" )
180
+
172
181
link = workos .client .portal .generate_link (
173
- organization = session ["organization_id" ], intent = request . args . get ( " intent" )
182
+ organization_id = session ["organization_id" ], intent = intent
174
183
)
175
184
176
- return redirect (link [ " link" ] )
185
+ return redirect (link . link )
177
186
178
187
179
188
@app .route ("/logout" )
0 commit comments