@@ -14,13 +14,26 @@ import GoogleSSO from '../sso/GoogleSSO'
1414import { decrypt } from '../utils/encryption.util'
1515
1616export class LoginMethodController {
17+ constructor ( ) {
18+ this . create = this . create . bind ( this )
19+ this . read = this . read . bind ( this )
20+ this . update = this . update . bind ( this )
21+ this . defaultMethods = this . defaultMethods . bind ( this )
22+ this . testConfig = this . testConfig . bind ( this )
23+ }
24+
1725 private assertEnterprisePlatform ( ) : void {
1826 const platformType = getRunningExpressApp ( ) . identityManager . getPlatformType ( )
1927 if ( platformType === Platform . CLOUD || platformType === Platform . OPEN_SOURCE ) {
2028 throw new InternalFlowiseError ( StatusCodes . FORBIDDEN , GeneralErrorMessage . FORBIDDEN )
2129 }
2230 }
2331
32+ private async getSafeConfig ( encryptedConfig : string ) : Promise < Record < string , unknown > > {
33+ const { clientSecret : _ , ...safe } = JSON . parse ( await decrypt ( encryptedConfig ) ) as Record < string , unknown >
34+ return safe
35+ }
36+
2437 public async create ( req : Request , res : Response , next : NextFunction ) {
2538 try {
2639 this . assertEnterprisePlatform ( )
@@ -73,6 +86,10 @@ export class LoginMethodController {
7386 let queryRunner
7487 try {
7588 this . assertEnterprisePlatform ( )
89+ const user = ( req as any ) . user
90+ if ( ! user ?. activeOrganizationId ) {
91+ throw new InternalFlowiseError ( StatusCodes . FORBIDDEN , GeneralErrorMessage . FORBIDDEN )
92+ }
7693 queryRunner = getRunningExpressApp ( ) . AppDataSource . createQueryRunner ( )
7794 await queryRunner . connect ( )
7895 const query = req . query as Partial < LoginMethod >
@@ -91,12 +108,18 @@ export class LoginMethodController {
91108 if ( query . id ) {
92109 loginMethod = await loginMethodService . readLoginMethodById ( query . id , queryRunner )
93110 if ( ! loginMethod ) throw new InternalFlowiseError ( StatusCodes . NOT_FOUND , LoginMethodErrorMessage . LOGIN_METHOD_NOT_FOUND )
94- loginMethod . config = JSON . parse ( await decrypt ( loginMethod . config ) )
111+ if ( loginMethod . organizationId !== user . activeOrganizationId ) {
112+ throw new InternalFlowiseError ( StatusCodes . FORBIDDEN , GeneralErrorMessage . FORBIDDEN )
113+ }
114+ loginMethod . config = await this . getSafeConfig ( loginMethod . config )
95115 } else if ( query . organizationId ) {
116+ if ( query . organizationId !== user . activeOrganizationId ) {
117+ throw new InternalFlowiseError ( StatusCodes . FORBIDDEN , GeneralErrorMessage . FORBIDDEN )
118+ }
96119 loginMethod = await loginMethodService . readLoginMethodByOrganizationId ( query . organizationId , queryRunner )
97120
98121 for ( let method of loginMethod ) {
99- method . config = JSON . parse ( await decrypt ( method . config ) )
122+ method . config = await this . getSafeConfig ( method . config )
100123 }
101124 loginMethodConfig . providers = loginMethod
102125 } else {
@@ -131,25 +154,39 @@ export class LoginMethodController {
131154 }
132155 }
133156 public async testConfig ( req : Request , res : Response , next : NextFunction ) {
157+ let queryRunner
134158 try {
135- const providers = req . body . providers
136- if ( req . body . providerName === 'azure' ) {
137- const response = await AzureSSO . testSetup ( providers [ 0 ] . config )
159+ const providers = req . body . providers as { config : Record < string , unknown > } [ ]
160+ const providerName = req . body . providerName as string
161+ const organizationId = req . body . organizationId as string | undefined
162+ let config = providers [ 0 ] ?. config ?? { }
163+
164+ if ( organizationId ) {
165+ queryRunner = getRunningExpressApp ( ) . AppDataSource . createQueryRunner ( )
166+ await queryRunner . connect ( )
167+ const loginMethodService = new LoginMethodService ( )
168+ config = await loginMethodService . getConfigWithSecrets ( organizationId , providerName , config , queryRunner )
169+ }
170+
171+ if ( providerName === 'azure' ) {
172+ const response = await AzureSSO . testSetup ( config )
138173 return res . json ( response )
139- } else if ( req . body . providerName === 'google' ) {
140- const response = await GoogleSSO . testSetup ( providers [ 0 ] . config )
174+ } else if ( providerName === 'google' ) {
175+ const response = await GoogleSSO . testSetup ( config )
141176 return res . json ( response )
142- } else if ( req . body . providerName === 'auth0' ) {
143- const response = await Auth0SSO . testSetup ( providers [ 0 ] . config )
177+ } else if ( providerName === 'auth0' ) {
178+ const response = await Auth0SSO . testSetup ( config )
144179 return res . json ( response )
145- } else if ( req . body . providerName === 'github' ) {
146- const response = await GithubSSO . testSetup ( providers [ 0 ] . config )
180+ } else if ( providerName === 'github' ) {
181+ const response = await GithubSSO . testSetup ( config )
147182 return res . json ( response )
148183 } else {
149184 return res . json ( { error : 'Provider not supported' } )
150185 }
151186 } catch ( error ) {
152187 next ( error )
188+ } finally {
189+ if ( queryRunner ) await queryRunner . release ( )
153190 }
154191 }
155192}
0 commit comments