Skip to content

Commit

Permalink
Update sample for 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tribble committed Aug 2, 2024
1 parent e4c08eb commit 6b32200
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
47 changes: 38 additions & 9 deletions python-flask-admin-portal-example/app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import os

from typing import TypeGuard, get_args
from flask import Flask, redirect, render_template, request, url_for
import workos
from workos import client as workos_client
from workos import portal
from flask_lucide import Lucide
import workos.organizations
import workos.portal
import workos.resources
import workos.resources.organizations
import workos.resources.portal
import workos.resources.sso


# Flask Setup
Expand All @@ -14,10 +19,16 @@

# WorkOS Setup
workos.api_key = os.getenv("WORKOS_API_KEY")
workos.project_id = os.getenv("WORKOS_CLIENT_ID")
workos.client_id = os.getenv("WORKOS_CLIENT_ID")
workos.base_api_url = "http://localhost:7000/" if DEBUG else workos.base_api_url


def is_portal_link_intent(
value: str,
) -> TypeGuard[workos.portal.PortalLinkIntent]:
return value in get_args(workos.portal.PortalLinkIntent)


@app.route("/")
def index():
return render_template("index.html")
Expand All @@ -32,21 +43,39 @@ def provision_enterprise():

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

# Otherwise create a new Organization and set the global org_id
else:
domain_data = list(
map(
lambda domain: workos.organizations.DomainDataInput(
{"domain": domain, "state": "verified"}
),
organization_domains,
)
)

organization = workos_client.organizations.create_organization(
{"name": organization_name, "domains": organization_domains}
name=organization_name,
domain_data=domain_data,
)
org_id = organization["id"]
org_id = organization.id

return render_template("org_logged_in.html")


@app.route("/launch_admin_portal", methods=["GET", "POST"])
def launch_admin_portal():
intent = request.args.get("intent")
portal_link = workos_client.portal.generate_link(organization=org_id, intent=intent)
return redirect(portal_link["link"])
if intent is None:
return "Missing intent parameter", 400

if not is_portal_link_intent(intent):
return "Invalid intent parameter", 400

portal_link = workos_client.portal.generate_link(
organization_id=org_id, intent=intent
)
return redirect(portal_link.link)
3 changes: 2 additions & 1 deletion python-flask-admin-portal-example/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ MarkupSafe==2.0.1
requests==2.26.0
urllib3==1.26.7
Werkzeug==2.0.1
workos>=1.23.3
../../SDKs/workos-python
workos>=4.0.0
python-dotenv
flask-lucide==0.2.0

0 comments on commit 6b32200

Please sign in to comment.