@@ -2,50 +2,29 @@ import {
22 FastifyPluginAsyncTypebox ,
33 Type ,
44} from '@fastify/type-provider-typebox' ;
5- import { IdentityClient } from '@frontegg/client' ;
65import { AppSystemProp , logger , system } from '@openops/server-shared' ;
76import { ALL_PRINCIPAL_TYPES , OpenOpsId } from '@openops/shared' ;
8- import { getCloudToken , getCloudUser } from '../user-info/cloud-auth' ;
7+ import { allowAllOriginsHookHandler } from '../helper/allow-all-origins-hook-handler' ;
8+ import { getVerifiedUser } from '../user-info/cloud-auth' ;
99import { flowTemplateService } from './flow-template.service' ;
1010
1111export const cloudTemplateController : FastifyPluginAsyncTypebox = async (
1212 app ,
1313) => {
14- const fronteggClientId = system . get ( AppSystemProp . FRONTEGG_CLIENT_ID ) ;
15- const fronteggApiKey = system . get ( AppSystemProp . FRONTEGG_API_KEY ) ;
14+ const publicKey = system . get ( AppSystemProp . FRONTEGG_PUBLIC_KEY ) ;
15+ const connectionPageEnabled = system . getBoolean (
16+ AppSystemProp . CLOUD_CONNECTION_PAGE_ENABLED ,
17+ ) ;
1618
17- if ( ! fronteggClientId || ! fronteggApiKey ) {
19+ if ( ! publicKey || ! connectionPageEnabled ) {
1820 logger . info (
1921 'Missing Frontegg configuration, disabling cloud templates API' ,
2022 ) ;
2123 return ;
2224 }
2325
24- const identityClient = new IdentityClient ( {
25- FRONTEGG_CLIENT_ID : fronteggClientId ,
26- FRONTEGG_API_KEY : fronteggApiKey ,
27- } ) ;
28-
2926 // cloud templates are available on any origin
30- app . addHook ( 'onSend' , ( request , reply , payload , done ) => {
31- void reply . header (
32- 'Access-Control-Allow-Origin' ,
33- request . headers . origin || request . headers [ 'Ops-Origin' ] || '*' ,
34- ) ;
35- void reply . header ( 'Access-Control-Allow-Methods' , 'GET,OPTIONS' ) ;
36- void reply . header (
37- 'Access-Control-Allow-Headers' ,
38- 'Content-Type,Ops-Origin,Authorization' ,
39- ) ;
40- void reply . header ( 'Access-Control-Allow-Credentials' , 'true' ) ;
41-
42- if ( request . method === 'OPTIONS' ) {
43- return reply . status ( 204 ) . send ( ) ;
44- }
45-
46- done ( null , payload ) ;
47- return ;
48- } ) ;
27+ app . addHook ( 'onSend' , allowAllOriginsHookHandler ) ;
4928
5029 app . get (
5130 '/' ,
@@ -70,23 +49,7 @@ export const cloudTemplateController: FastifyPluginAsyncTypebox = async (
7049 } ,
7150 } ,
7251 async ( request ) => {
73- const token = getCloudToken ( request ) ;
74-
75- if ( ! ( await getCloudUser ( identityClient , token ) ) ) {
76- return flowTemplateService . getFlowTemplates ( {
77- search : request . query . search ,
78- tags : request . query . tags ,
79- services : request . query . services ,
80- domains : request . query . domains ,
81- blocks : request . query . blocks ,
82- projectId : request . principal . projectId ,
83- organizationId : request . principal . organization . id ,
84- cloudTemplates : true ,
85- isSample : true ,
86- version : request . query . version ,
87- categories : request . query . categories ,
88- } ) ;
89- }
52+ const user = getVerifiedUser ( request , publicKey ) ;
9053
9154 return flowTemplateService . getFlowTemplates ( {
9255 search : request . query . search ,
@@ -97,6 +60,7 @@ export const cloudTemplateController: FastifyPluginAsyncTypebox = async (
9760 projectId : request . principal . projectId ,
9861 organizationId : request . principal . organization . id ,
9962 cloudTemplates : true ,
63+ isSample : ! user ,
10064 version : request . query . version ,
10165 categories : request . query . categories ,
10266 } ) ;
@@ -120,13 +84,14 @@ export const cloudTemplateController: FastifyPluginAsyncTypebox = async (
12084 } ,
12185 } ,
12286 async ( request , reply ) => {
123- const token = getCloudToken ( request ) ;
124- if ( ! ( await getCloudUser ( identityClient , token ) ) ) {
87+ const user = getVerifiedUser ( request , publicKey ) ;
88+
89+ if ( ! user ) {
12590 const template = await flowTemplateService . getFlowTemplate (
12691 request . params . id ,
12792 ) ;
12893
129- return template ?. isSample ? template : reply . status ( 404 ) . send ( ) ;
94+ return template ?. isSample ? template : reply . status ( 403 ) . send ( ) ;
13095 }
13196
13297 return flowTemplateService . getFlowTemplate ( request . params . id ) ;
0 commit comments