1
1
import os
2
-
2
+ from typing import TypeGuard , get_args
3
3
from flask import Flask , redirect , render_template , request , url_for
4
4
import workos
5
5
from workos import client as workos_client
6
- from workos import portal
7
6
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
8
13
9
14
10
15
# Flask Setup
14
19
15
20
# WorkOS Setup
16
21
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" )
18
23
workos .base_api_url = "http://localhost:7000/" if DEBUG else workos .base_api_url
19
24
20
25
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
+
21
32
@app .route ("/" )
22
33
def index ():
23
34
return render_template ("index.html" )
@@ -32,21 +43,39 @@ def provision_enterprise():
32
43
33
44
# Check if a matching domain already exists and set global org_id if there is a match
34
45
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
37
48
38
49
# Otherwise create a new Organization and set the global org_id
39
50
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
+
40
60
organization = workos_client .organizations .create_organization (
41
- {"name" : organization_name , "domains" : organization_domains }
61
+ name = organization_name ,
62
+ domain_data = domain_data ,
42
63
)
43
- org_id = organization [ "id" ]
64
+ org_id = organization . id
44
65
45
66
return render_template ("org_logged_in.html" )
46
67
47
68
48
69
@app .route ("/launch_admin_portal" , methods = ["GET" , "POST" ])
49
70
def launch_admin_portal ():
50
71
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 )
0 commit comments