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
13
13
# Flask Setup
14
- DEBUG = False
15
14
app = Flask (__name__ )
16
15
app .secret_key = os .getenv ("APP_SECRET_KEY" )
17
16
18
17
lucide = Lucide (app )
19
18
20
19
21
20
# 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
+ )
25
27
26
28
27
29
def to_pretty_json (value ):
@@ -34,14 +36,14 @@ def to_pretty_json(value):
34
36
@app .route ("/" , methods = ["POST" , "GET" ])
35
37
def index ():
36
38
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"
39
41
)
40
42
today = datetime .today ()
41
43
last_month = today - timedelta (days = 30 )
42
44
return render_template (
43
45
"send_events.html" ,
44
- link = link [ " link" ] ,
46
+ link = link . link ,
45
47
organization_id = session ["organization_id" ],
46
48
org_name = session ["organization_name" ],
47
49
last_month_iso = last_month .isoformat (),
@@ -50,29 +52,29 @@ def index():
50
52
except KeyError :
51
53
before = request .args .get ("before" )
52
54
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"
55
57
)
56
- before = organizations [ "listMetadata" ][ " before" ]
57
- after = organizations [ "listMetadata" ][ " after" ]
58
+ before = organizations . list_metadata . before
59
+ after = organizations . list_metadata . after
58
60
return render_template (
59
61
"login.html" ,
60
- organizations = organizations [ " data" ] ,
62
+ organizations = organizations . data ,
61
63
before = before ,
62
64
after = after ,
63
65
)
64
66
65
67
66
68
@app .route ("/set_org" , methods = ["POST" , "GET" ])
67
69
def set_org ():
68
- organization_id = request .args .get ("id" )
70
+ organization_id = request .args .get ("id" ) or request .form ["organization_id" ]
71
+
69
72
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
72
75
)
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
76
78
return redirect ("/" )
77
79
78
80
@@ -87,28 +89,32 @@ def send_event():
87
89
)
88
90
organization_id = session ["organization_id" ]
89
91
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" ,
104
101
},
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
+ )
112
118
return redirect ("/" )
113
119
114
120
@@ -147,33 +153,40 @@ def get_events():
147
153
148
154
try :
149
155
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 ,
152
158
range_start = request .form ["range-start" ],
153
159
range_end = request .form ["range-end" ],
154
160
actions = actions ,
155
- actors = actors ,
161
+ actor_names = actors ,
156
162
targets = targets ,
157
163
)
158
- session ["export_id" ] = create_export_response .to_dict ()[ "id" ]
164
+ session ["export_id" ] = create_export_response .id
159
165
160
166
return redirect ("export_events" )
161
167
except Exception as e :
162
168
print (str (e ))
163
169
return redirect ("/" )
164
170
if event_type == "access_csv" :
165
171
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 )
168
177
169
178
170
179
@app .route ("/events" , methods = ["GET" ])
171
180
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
174
187
)
175
188
176
- return redirect (link [ " link" ] )
189
+ return redirect (link . link )
177
190
178
191
179
192
@app .route ("/logout" )
0 commit comments