11import json
22import os
3- from urllib .parse import urlparse , parse_qs
43from flask import Flask , session , redirect , render_template , request , url_for
54import workos
65
1312# WorkOS Setup
1413
1514workos .api_key = os .getenv ("WORKOS_API_KEY" )
16- workos .project_id = os .getenv ("WORKOS_CLIENT_ID" )
15+ workos .client_id = os .getenv ("WORKOS_CLIENT_ID" )
1716workos .base_api_url = "http://localhost:7000/" if DEBUG else workos .base_api_url
1817
1918# Enter Organization ID here
2019
21- CUSTOMER_ORGANIZATION_ID = ""
20+ CUSTOMER_ORGANIZATION_ID = "" # Use org_test_idp for testing
2221
2322
2423def to_pretty_json (value ):
@@ -44,15 +43,24 @@ def login():
4443def auth ():
4544
4645 login_type = request .form .get ("login_method" )
47-
48- params = {"redirect_uri" : url_for ("auth_callback" , _external = True ), "state" : {}}
49-
50- if login_type == "saml" :
51- params ["organization" ] = CUSTOMER_ORGANIZATION_ID
52- else :
53- params ["provider" ] = login_type
54-
55- authorization_url = workos .client .sso .get_authorization_url (** params )
46+ if login_type not in (
47+ "saml" ,
48+ "GoogleOAuth" ,
49+ "MicrosoftOAuth" ,
50+ ):
51+ return redirect ("/" )
52+
53+ redirect_uri = url_for ("auth_callback" , _external = True )
54+
55+ authorization_url = (
56+ workos .client .sso .get_authorization_url (
57+ redirect_uri = redirect_uri , organization_id = CUSTOMER_ORGANIZATION_ID
58+ )
59+ if login_type == "saml"
60+ else workos .client .sso .get_authorization_url (
61+ redirect_uri = redirect_uri , provider = login_type
62+ )
63+ )
5664
5765 return redirect (authorization_url )
5866
@@ -61,11 +69,13 @@ def auth():
6169def auth_callback ():
6270
6371 code = request .args .get ("code" )
64- profile = workos .client .sso .get_profile_and_token (code )
65- p_profile = profile .to_dict ()
66- session ["first_name" ] = p_profile ["profile" ]["first_name" ]
67- session ["raw_profile" ] = p_profile ["profile" ]
68- session ["session_id" ] = p_profile ["profile" ]["id" ]
72+ # Why do I always get an error that the target does not belong to the target organization?
73+ if code is None :
74+ return redirect ("/" )
75+ profile = workos .client .sso .get_profile_and_token (code ).profile
76+ session ["first_name" ] = profile .first_name
77+ session ["raw_profile" ] = profile .dict ()
78+ session ["session_id" ] = profile .id
6979 return redirect ("/" )
7080
7181
0 commit comments