11use async_trait:: async_trait;
22use serde:: { Deserialize , Serialize } ;
33
4- use crate :: admin_portal:: AdminPortal ;
54use crate :: organizations:: OrganizationId ;
5+ use crate :: portal:: { GeneratePortalLinkIntent , Portal } ;
66use crate :: { ResponseExt , WorkOsResult } ;
77
8- /// The intent of an Admin Portal session.
9- #[ derive( Clone , Copy , Debug , Serialize ) ]
10- #[ serde( rename_all = "snake_case" ) ]
11- pub enum AdminPortalIntent {
12- /// The Admin Portal will be used to setup Single Sign-On (SSO).
13- Sso ,
14-
15- /// The Admin Portal wil be used to setup Directory Sync.
16- #[ serde( rename = "dsync" ) ]
17- DirectorySync ,
18- }
19-
20- /// The target of the Admin Portal.
21- #[ derive( Debug , Serialize ) ]
22- #[ serde( untagged, rename_all = "snake_case" ) ]
23- pub enum AdminPortalTarget {
24- /// The Admin Portal session should target an organization.
25- Organization {
26- /// The ID of the organization.
27- #[ serde( rename = "organization" ) ]
28- organization_id : OrganizationId ,
29-
30- /// The intent of the Admin Portal session.
31- intent : AdminPortalIntent ,
32- } ,
33- }
34-
358/// The parameters for [`GeneratePortalLink`].
369#[ derive( Debug , Serialize ) ]
3710pub struct GeneratePortalLinkParams < ' a > {
38- /// The target of the Admin Portal.
39- #[ serde( flatten) ]
40- pub target : & ' a AdminPortalTarget ,
11+ /// The ID of the organization.
12+ #[ serde( rename = "organization" ) ]
13+ pub organization_id : & ' a OrganizationId ,
14+
15+ /// The intent of the Admin Portal.
16+ pub intent : GeneratePortalLinkIntent ,
17+
18+ /// The URL to go to when an admin clicks on your logo in the Admin Portal.
19+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
20+ pub return_url : Option < & ' a str > ,
4121
42- /// The URL to which the Admin Portal should send users when they click on the link
43- /// to return to your application.
22+ /// The URL to redirect the admin to when they finish setup.
4423 #[ serde( skip_serializing_if = "Option::is_none" ) ]
45- pub return_url : Option < String > ,
24+ pub success_url : Option < & ' a str > ,
4625}
4726
4827/// The response for [`GeneratePortalLink`].
4928#[ derive( Debug , Deserialize ) ]
5029pub struct GeneratePortalLinkResponse {
51- /// The generate Admin Portal link .
30+ /// An ephemeral link to initiate the Admin Portal.
5231 pub link : String ,
5332}
5433
@@ -59,29 +38,28 @@ pub enum GeneratePortalLinkError {}
5938/// [WorkOS Docs: Generate a Portal Link](https://workos.com/docs/reference/admin-portal/portal-link/generate)
6039#[ async_trait]
6140pub trait GeneratePortalLink {
62- /// Generates an Admin Portal link .
41+ /// Generate a Portal Link scoped to an Organization .
6342 ///
6443 /// [WorkOS Docs: Generate a Portal Link](https://workos.com/docs/reference/admin-portal/portal-link/generate)
6544 ///
6645 /// # Examples
6746 ///
6847 /// ```
6948 /// # use workos::WorkOsResult;
70- /// # use workos::admin_portal::*;
7149 /// # use workos::organizations::OrganizationId;
50+ /// # use workos::portal::*;
7251 /// use workos::{ApiKey, WorkOs};
7352 ///
7453 /// # async fn run() -> WorkOsResult<(), GeneratePortalLinkError> {
7554 /// let workos = WorkOs::new(&ApiKey::from("sk_example_123456789"));
7655 ///
7756 /// let GeneratePortalLinkResponse { link } = workos
78- /// .admin_portal ()
57+ /// .portal ()
7958 /// .generate_portal_link(&GeneratePortalLinkParams {
80- /// target: &AdminPortalTarget::Organization {
81- /// organization_id: OrganizationId::from("org_01EHZNVPK3SFK441A1RGBFSHRT"),
82- /// intent: AdminPortalIntent::Sso,
83- /// },
59+ /// organization_id: &OrganizationId::from("org_01EHZNVPK3SFK441A1RGBFSHRT"),
60+ /// intent: GeneratePortalLinkIntent::Sso,
8461 /// return_url: None,
62+ /// success_url: None,
8563 /// })
8664 /// .await?;
8765 /// # Ok(())
@@ -94,13 +72,13 @@ pub trait GeneratePortalLink {
9472}
9573
9674#[ async_trait]
97- impl GeneratePortalLink for AdminPortal < ' _ > {
75+ impl GeneratePortalLink for Portal < ' _ > {
9876 async fn generate_portal_link (
9977 & self ,
10078 params : & GeneratePortalLinkParams < ' _ > ,
10179 ) -> WorkOsResult < GeneratePortalLinkResponse , GeneratePortalLinkError > {
10280 let url = self . workos . base_url ( ) . join ( "/portal/generate_link" ) ?;
103- let generate_link_response = self
81+ let response = self
10482 . workos
10583 . client ( )
10684 . post ( url)
@@ -113,12 +91,13 @@ impl GeneratePortalLink for AdminPortal<'_> {
11391 . json :: < GeneratePortalLinkResponse > ( )
11492 . await ?;
11593
116- Ok ( generate_link_response )
94+ Ok ( response )
11795 }
11896}
11997
12098#[ cfg( test) ]
12199mod test {
100+ use mockito:: Matcher ;
122101 use serde_json:: json;
123102 use tokio;
124103
@@ -136,33 +115,34 @@ mod test {
136115 . unwrap ( )
137116 . build ( ) ;
138117
139- server. mock ( "POST" , "/portal/generate_link" )
118+ server
119+ . mock ( "POST" , "/portal/generate_link" )
140120 . match_header ( "Authorization" , "Bearer sk_example_123456789" )
141- . match_body ( r#"{"organization":"org_01EHZNVPK3SFK441A1RGBFSHRT","intent":"sso"}"# )
121+ . match_body ( Matcher :: Json ( json ! ( {
122+ "organization" : "org_01EHZNVPK3SFK441A1RGBFSHRT" ,
123+ "intent" : "sso" ,
124+ } ) ) )
142125 . with_status ( 201 )
143126 . with_body (
144127 json ! ( {
145- "link" : "https://setup.workos.com/portal/launch?secret=JteZqfJZqUcgWGaYCC6iI0gW0 "
128+ "link" : "https://setup.workos.com?token=token "
146129 } )
147130 . to_string ( ) ,
148131 )
149- . create_async ( ) . await ;
132+ . create_async ( )
133+ . await ;
150134
151135 let GeneratePortalLinkResponse { link } = workos
152- . admin_portal ( )
136+ . portal ( )
153137 . generate_portal_link ( & GeneratePortalLinkParams {
154- target : & AdminPortalTarget :: Organization {
155- organization_id : OrganizationId :: from ( "org_01EHZNVPK3SFK441A1RGBFSHRT" ) ,
156- intent : AdminPortalIntent :: Sso ,
157- } ,
138+ organization_id : & OrganizationId :: from ( "org_01EHZNVPK3SFK441A1RGBFSHRT" ) ,
139+ intent : GeneratePortalLinkIntent :: Sso ,
158140 return_url : None ,
141+ success_url : None ,
159142 } )
160143 . await
161144 . unwrap ( ) ;
162145
163- assert_eq ! (
164- link,
165- "https://setup.workos.com/portal/launch?secret=JteZqfJZqUcgWGaYCC6iI0gW0" . to_string( )
166- )
146+ assert_eq ! ( link, "https://setup.workos.com?token=token" . to_string( ) )
167147 }
168148}
0 commit comments