@@ -13,31 +13,37 @@ import {
1313
1414const logger = Logger ( 'cognito.visitor-auth' ) ;
1515
16- type CognitoRuntimeEnvironment = RuntimeEnvironment < { } , CognitoSpaceInstallationConfiguration > ;
16+ type CognitoRuntimeEnvironment = RuntimeEnvironment <
17+ { } ,
18+ CognitoSiteOrSpaceInstallationConfiguration
19+ > ;
1720
1821type CognitoRuntimeContext = RuntimeContext < CognitoRuntimeEnvironment > ;
1922
20- type CognitoSpaceInstallationConfiguration = {
23+ type CognitoSiteOrSpaceInstallationConfiguration = {
2124 client_id ?: string ;
2225 cognito_domain ?: string ;
2326 client_secret ?: string ;
2427} ;
2528
26- type CognitoState = CognitoSpaceInstallationConfiguration ;
29+ type CognitoState = CognitoSiteOrSpaceInstallationConfiguration ;
2730
2831type CognitoProps = {
2932 installation : {
3033 configuration ?: IntegrationInstallationConfiguration ;
3134 } ;
3235 spaceInstallation : {
33- configuration ?: CognitoSpaceInstallationConfiguration ;
36+ configuration ?: CognitoSiteOrSpaceInstallationConfiguration ;
37+ } ;
38+ siteInstallation : {
39+ configuration ?: CognitoSiteOrSpaceInstallationConfiguration ;
3440 } ;
3541} ;
3642
3743export type CognitoAction = { action : 'save.config' } ;
3844
3945const getDomainWithHttps = ( url : string ) : string => {
40- if ( url . startsWith ( 'https://' ) ) {
46+ if ( url . startsWith ( 'https://' ) || url . startsWith ( 'http://' ) ) {
4147 return url ;
4248 } else {
4349 return `https://${ url } ` ;
@@ -52,39 +58,56 @@ const configBlock = createComponent<
5258> ( {
5359 componentId : 'config' ,
5460 initialState : ( props ) => {
61+ const siteOrSpaceInstallation = props . siteInstallation ?? props . spaceInstallation ;
5562 return {
56- client_id : props . spaceInstallation . configuration ?. client_id ?. toString ( ) || '' ,
57- cognito_domain : props . spaceInstallation . configuration ?. cognito_domain ?. toString ( ) || '' ,
58- client_secret : props . spaceInstallation . configuration ?. client_secret ?. toString ( ) || '' ,
63+ client_id : siteOrSpaceInstallation . configuration ?. client_id ?. toString ( ) || '' ,
64+ cognito_domain : siteOrSpaceInstallation . configuration ?. cognito_domain ?. toString ( ) || '' ,
65+ client_secret : siteOrSpaceInstallation . configuration ?. client_secret ?. toString ( ) || '' ,
5966 } ;
6067 } ,
6168 action : async ( element , action , context ) => {
6269 switch ( action . action ) {
6370 case 'save.config' :
6471 const { api, environment } = context ;
65- const spaceInstallation = environment . spaceInstallation ;
72+ const siteOrSpaceInstallation =
73+ environment . siteInstallation ?? environment . spaceInstallation ;
6674
6775 const configurationBody = {
68- ...spaceInstallation . configuration ,
76+ ...siteOrSpaceInstallation . configuration ,
6977 client_id : element . state . client_id ,
7078 client_secret : element . state . client_secret ,
7179 cognito_domain : getDomainWithHttps ( element . state . cognito_domain ) ,
7280 } ;
73- await api . integrations . updateIntegrationSpaceInstallation (
74- spaceInstallation . integration ,
75- spaceInstallation . installation ,
76- spaceInstallation . space ,
77- {
78- configuration : {
79- ...configurationBody ,
80- } ,
81- }
82- ) ;
81+ if ( 'site' in siteOrSpaceInstallation ) {
82+ await api . integrations . updateIntegrationSiteInstallation (
83+ siteOrSpaceInstallation . integration ,
84+ siteOrSpaceInstallation . installation ,
85+ siteOrSpaceInstallation . site ,
86+ {
87+ configuration : {
88+ ...configurationBody ,
89+ } ,
90+ }
91+ ) ;
92+ } else {
93+ await api . integrations . updateIntegrationSpaceInstallation (
94+ siteOrSpaceInstallation . integration ,
95+ siteOrSpaceInstallation . installation ,
96+ siteOrSpaceInstallation . space ,
97+ {
98+ configuration : {
99+ ...configurationBody ,
100+ } ,
101+ }
102+ ) ;
103+ }
83104 return element ;
84105 }
85106 } ,
86107 render : async ( element , context ) => {
87- const VACallbackURL = `${ context . environment . spaceInstallation ?. urls ?. publicEndpoint } /visitor-auth/response` ;
108+ const siteOrSpaceInstallation =
109+ context . environment . siteInstallation ?? context . environment . spaceInstallation ;
110+ const VACallbackURL = `${ siteOrSpaceInstallation ?. urls ?. publicEndpoint } /visitor-auth/response` ;
88111 return (
89112 < block >
90113 < input
@@ -167,21 +190,39 @@ const configBlock = createComponent<
167190 } ,
168191} ) ;
169192
193+ /**
194+ * Get the published content (site or space) related urls.
195+ */
196+ async function getPublishedContentUrls ( context : CognitoRuntimeContext ) {
197+ const organizationId = context . environment . installation ?. target ?. organization ;
198+ const siteOrSpaceInstallation =
199+ context . environment . siteInstallation ?? context . environment . spaceInstallation ;
200+ const publishedContentData =
201+ 'site' in siteOrSpaceInstallation
202+ ? await context . api . orgs . getSiteById ( organizationId , siteOrSpaceInstallation . site )
203+ : await context . api . spaces . getSpaceById ( siteOrSpaceInstallation . space ) ;
204+
205+ return publishedContentData . data . urls ;
206+ }
207+
170208const handleFetchEvent : FetchEventCallback < CognitoRuntimeContext > = async ( request , context ) => {
171209 const { environment } = context ;
172- const installationURL = environment . spaceInstallation ?. urls ?. publicEndpoint ;
210+ const siteOrSpaceInstallation = environment . siteInstallation ?? environment . spaceInstallation ;
211+ const installationURL = siteOrSpaceInstallation ?. urls ?. publicEndpoint ;
173212 if ( installationURL ) {
174213 const router = Router ( {
175214 base : new URL ( installationURL ) . pathname ,
176215 } ) ;
177216
178217 router . get ( '/visitor-auth/response' , async ( request ) => {
179- if ( context . environment . spaceInstallation ?. space ) {
180- const space = await context . api . spaces . getSpaceById (
181- context . environment . spaceInstallation ?. space
182- ) ;
183- const spaceData = space . data ;
184- const privateKey = context . environment . signingSecrets . spaceInstallation ;
218+ if (
219+ ( 'site' in siteOrSpaceInstallation && siteOrSpaceInstallation . site ) ||
220+ ( 'space' in siteOrSpaceInstallation && siteOrSpaceInstallation . space )
221+ ) {
222+ const publishedContentUrls = await getPublishedContentUrls ( context ) ;
223+ const privateKey =
224+ context . environment . signingSecrets . siteInstallation ??
225+ context . environment . signingSecrets . spaceInstallation ;
185226 let token ;
186227 try {
187228 token = await sign (
@@ -194,9 +235,9 @@ const handleFetchEvent: FetchEventCallback<CognitoRuntimeContext> = async (reque
194235 } ) ;
195236 }
196237
197- const cognitoDomain = environment . spaceInstallation ? .configuration . cognito_domain ;
198- const clientId = environment . spaceInstallation ? .configuration . client_id ;
199- const clientSecret = environment . spaceInstallation ? .configuration . client_secret ;
238+ const cognitoDomain = siteOrSpaceInstallation . configuration . cognito_domain ;
239+ const clientId = siteOrSpaceInstallation . configuration . client_id ;
240+ const clientSecret = siteOrSpaceInstallation . configuration . client_secret ;
200241 if ( clientId && clientSecret ) {
201242 const searchParams = new URLSearchParams ( {
202243 grant_type : 'authorization_code' ,
@@ -224,13 +265,15 @@ const handleFetchEvent: FetchEventCallback<CognitoRuntimeContext> = async (reque
224265 if ( 'access_token' in resp ) {
225266 let url ;
226267 if ( request . query . state ) {
227- url = new URL ( `${ spaceData . urls ?. published } ${ request . query . state } ` ) ;
268+ url = new URL (
269+ `${ publishedContentUrls ?. published } ${ request . query . state } `
270+ ) ;
228271 url . searchParams . append ( 'jwt_token' , token ) ;
229272 } else {
230- url = new URL ( spaceData . urls ?. published ) ;
273+ url = new URL ( publishedContentUrls ?. published ) ;
231274 url . searchParams . append ( 'jwt_token' , token ) ;
232275 }
233- if ( spaceData . urls ?. published && token ) {
276+ if ( publishedContentUrls ?. published && token ) {
234277 return Response . redirect ( url . toString ( ) ) ;
235278 } else {
236279 return new Response (
@@ -287,9 +330,14 @@ export default createIntegration({
287330 components : [ configBlock ] ,
288331 fetch_visitor_authentication : async ( event , context ) => {
289332 const { environment } = context ;
290- const installationURL = environment . spaceInstallation ?. urls ?. publicEndpoint ;
291- const cognitoDomain = environment . spaceInstallation ?. configuration . cognito_domain ;
292- const clientId = environment . spaceInstallation ?. configuration . client_id ;
333+ const siteOrSpaceInstallation =
334+ environment . siteInstallation ?? environment . spaceInstallation ;
335+
336+ const installationURL = siteOrSpaceInstallation ?. urls ?. publicEndpoint ;
337+
338+ const cognitoDomain = siteOrSpaceInstallation ?. configuration . cognito_domain ;
339+ const clientId = siteOrSpaceInstallation ?. configuration . client_id ;
340+
293341 const location = event . location ? event . location : '' ;
294342
295343 const url = new URL ( `${ cognitoDomain } /oauth2/authorize` ) ;
0 commit comments