@@ -13,31 +13,37 @@ import {
13
13
14
14
const logger = Logger ( 'cognito.visitor-auth' ) ;
15
15
16
- type CognitoRuntimeEnvironment = RuntimeEnvironment < { } , CognitoSpaceInstallationConfiguration > ;
16
+ type CognitoRuntimeEnvironment = RuntimeEnvironment <
17
+ { } ,
18
+ CognitoSiteOrSpaceInstallationConfiguration
19
+ > ;
17
20
18
21
type CognitoRuntimeContext = RuntimeContext < CognitoRuntimeEnvironment > ;
19
22
20
- type CognitoSpaceInstallationConfiguration = {
23
+ type CognitoSiteOrSpaceInstallationConfiguration = {
21
24
client_id ?: string ;
22
25
cognito_domain ?: string ;
23
26
client_secret ?: string ;
24
27
} ;
25
28
26
- type CognitoState = CognitoSpaceInstallationConfiguration ;
29
+ type CognitoState = CognitoSiteOrSpaceInstallationConfiguration ;
27
30
28
31
type CognitoProps = {
29
32
installation : {
30
33
configuration ?: IntegrationInstallationConfiguration ;
31
34
} ;
32
35
spaceInstallation : {
33
- configuration ?: CognitoSpaceInstallationConfiguration ;
36
+ configuration ?: CognitoSiteOrSpaceInstallationConfiguration ;
37
+ } ;
38
+ siteInstallation : {
39
+ configuration ?: CognitoSiteOrSpaceInstallationConfiguration ;
34
40
} ;
35
41
} ;
36
42
37
43
export type CognitoAction = { action : 'save.config' } ;
38
44
39
45
const getDomainWithHttps = ( url : string ) : string => {
40
- if ( url . startsWith ( 'https://' ) ) {
46
+ if ( url . startsWith ( 'https://' ) || url . startsWith ( 'http://' ) ) {
41
47
return url ;
42
48
} else {
43
49
return `https://${ url } ` ;
@@ -52,39 +58,56 @@ const configBlock = createComponent<
52
58
> ( {
53
59
componentId : 'config' ,
54
60
initialState : ( props ) => {
61
+ const siteOrSpaceInstallation = props . siteInstallation ?? props . spaceInstallation ;
55
62
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 ( ) || '' ,
59
66
} ;
60
67
} ,
61
68
action : async ( element , action , context ) => {
62
69
switch ( action . action ) {
63
70
case 'save.config' :
64
71
const { api, environment } = context ;
65
- const spaceInstallation = environment . spaceInstallation ;
72
+ const siteOrSpaceInstallation =
73
+ environment . siteInstallation ?? environment . spaceInstallation ;
66
74
67
75
const configurationBody = {
68
- ...spaceInstallation . configuration ,
76
+ ...siteOrSpaceInstallation . configuration ,
69
77
client_id : element . state . client_id ,
70
78
client_secret : element . state . client_secret ,
71
79
cognito_domain : getDomainWithHttps ( element . state . cognito_domain ) ,
72
80
} ;
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
+ }
83
104
return element ;
84
105
}
85
106
} ,
86
107
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` ;
88
111
return (
89
112
< block >
90
113
< input
@@ -167,21 +190,39 @@ const configBlock = createComponent<
167
190
} ,
168
191
} ) ;
169
192
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
+
170
208
const handleFetchEvent : FetchEventCallback < CognitoRuntimeContext > = async ( request , context ) => {
171
209
const { environment } = context ;
172
- const installationURL = environment . spaceInstallation ?. urls ?. publicEndpoint ;
210
+ const siteOrSpaceInstallation = environment . siteInstallation ?? environment . spaceInstallation ;
211
+ const installationURL = siteOrSpaceInstallation ?. urls ?. publicEndpoint ;
173
212
if ( installationURL ) {
174
213
const router = Router ( {
175
214
base : new URL ( installationURL ) . pathname ,
176
215
} ) ;
177
216
178
217
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 ;
185
226
let token ;
186
227
try {
187
228
token = await sign (
@@ -194,9 +235,9 @@ const handleFetchEvent: FetchEventCallback<CognitoRuntimeContext> = async (reque
194
235
} ) ;
195
236
}
196
237
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 ;
200
241
if ( clientId && clientSecret ) {
201
242
const searchParams = new URLSearchParams ( {
202
243
grant_type : 'authorization_code' ,
@@ -224,13 +265,15 @@ const handleFetchEvent: FetchEventCallback<CognitoRuntimeContext> = async (reque
224
265
if ( 'access_token' in resp ) {
225
266
let url ;
226
267
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
+ ) ;
228
271
url . searchParams . append ( 'jwt_token' , token ) ;
229
272
} else {
230
- url = new URL ( spaceData . urls ?. published ) ;
273
+ url = new URL ( publishedContentUrls ?. published ) ;
231
274
url . searchParams . append ( 'jwt_token' , token ) ;
232
275
}
233
- if ( spaceData . urls ?. published && token ) {
276
+ if ( publishedContentUrls ?. published && token ) {
234
277
return Response . redirect ( url . toString ( ) ) ;
235
278
} else {
236
279
return new Response (
@@ -287,9 +330,14 @@ export default createIntegration({
287
330
components : [ configBlock ] ,
288
331
fetch_visitor_authentication : async ( event , context ) => {
289
332
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
+
293
341
const location = event . location ? event . location : '' ;
294
342
295
343
const url = new URL ( `${ cognitoDomain } /oauth2/authorize` ) ;
0 commit comments