@@ -5,6 +5,10 @@ import { Encryptor } from '../../../helpers/encryption/encryptor.js';
55import { isConnectionTypeAgent } from '../../../helpers/index.js' ;
66import { UserEntity } from '../../user/user.entity.js' ;
77import { ConnectionEntity } from '../connection.entity.js' ;
8+ import {
9+ decryptConnectionCredentialsAsync ,
10+ decryptConnectionsCredentialsAsync ,
11+ } from '../utils/decrypt-connection-credentials-async.js' ;
812import { isTestConnectionUtil } from '../utils/is-test-connection-util.js' ;
913import { IConnectionRepository } from './connection.repository.interface.js' ;
1014
@@ -26,6 +30,7 @@ export const customConnectionRepositoryExtension: IConnectionRepository &
2630 savedConnection . cert = this . decryptConnectionField ( savedConnection . cert ) ;
2731 }
2832 }
33+ savedConnection . credentialsDecrypted = true ;
2934 return savedConnection ;
3035 } ,
3136
@@ -38,7 +43,9 @@ export const customConnectionRepositoryExtension: IConnectionRepository &
3843 if ( ! includeTestConnections ) {
3944 connectionQb . andWhere ( 'connection.isTestConnection = :isTest' , { isTest : false } ) ;
4045 }
41- return await connectionQb . getMany ( ) ;
46+ const connections = await connectionQb . getMany ( ) ;
47+ await decryptConnectionsCredentialsAsync ( connections ) ;
48+ return connections ;
4249 } ,
4350
4451 async findAllUserTestConnections ( userId : string ) : Promise < Array < ConnectionEntity > > {
@@ -48,7 +55,9 @@ export const customConnectionRepositoryExtension: IConnectionRepository &
4855 . leftJoinAndSelect ( 'connection.connection_properties' , 'connection_properties' )
4956 . andWhere ( 'user.id = :userId' , { userId : userId } )
5057 . andWhere ( 'connection.isTestConnection = :isTest' , { isTest : true } ) ;
51- return await connectionQb . getMany ( ) ;
58+ const connections = await connectionQb . getMany ( ) ;
59+ await decryptConnectionsCredentialsAsync ( connections ) ;
60+ return connections ;
5261 } ,
5362
5463 async findAllUserNonTestsConnections ( userId : string ) : Promise < Array < ConnectionEntity > > {
@@ -81,6 +90,7 @@ export const customConnectionRepositoryExtension: IConnectionRepository &
8190 connection . signing_key = Encryptor . generateRandomString ( 40 ) ;
8291 await this . save ( connection ) ;
8392 }
93+ await decryptConnectionCredentialsAsync ( connection ) ;
8494 return connection ;
8595 } ,
8696
@@ -96,6 +106,7 @@ export const customConnectionRepositoryExtension: IConnectionRepository &
96106 connection . signing_key = Encryptor . generateRandomString ( 40 ) ;
97107 await this . save ( connection ) ;
98108 }
109+ await decryptConnectionCredentialsAsync ( connection ) ;
99110
100111 if ( connection . masterEncryption && ! masterPwd ) {
101112 throw new Error ( Messages . MASTER_PASSWORD_MISSING ) ;
@@ -121,19 +132,24 @@ export const customConnectionRepositoryExtension: IConnectionRepository &
121132 const qb = this . createQueryBuilder ( 'connection' )
122133 . leftJoinAndSelect ( 'connection.groups' , 'group' )
123134 . andWhere ( 'connection.id = :connectionId' , { connectionId : connectionId } ) ;
124- return await qb . getOne ( ) ;
135+ const connection = await qb . getOne ( ) ;
136+ if ( connection ) {
137+ await decryptConnectionCredentialsAsync ( connection ) ;
138+ }
139+ return connection ;
125140 } ,
126141
127142 async getWorkedConnectionsInTwoWeeks ( ) : Promise < Array < ConnectionEntity > > {
128- const freshNonTestConnectionsWithLogs = await this . createQueryBuilder ( 'connection' )
143+ const connections = await this . createQueryBuilder ( 'connection' )
129144 . leftJoinAndSelect ( 'connection.author' , 'author' )
130145 . leftJoin ( 'connection.logs' , 'logs' )
131146 . where ( 'connection.createdAt > :date' , { date : Constants . TWO_WEEKS_AGO ( ) } )
132147 . andWhere ( 'author.gclid IS NOT NULL' )
133148 . andWhere ( 'connection.isTestConnection = :isTest' , { isTest : false } )
134149 . andWhere ( 'logs.id IS NOT NULL' )
135150 . getMany ( ) ;
136- return freshNonTestConnectionsWithLogs ;
151+ await decryptConnectionsCredentialsAsync ( connections ) ;
152+ return connections ;
137153 } ,
138154
139155 async getConnectionByGroupIdWithCompanyAndUsersInCompany ( groupId : string ) : Promise < ConnectionEntity | null > {
@@ -142,17 +158,29 @@ export const customConnectionRepositoryExtension: IConnectionRepository &
142158 . leftJoinAndSelect ( 'connection.company' , 'company' )
143159 . leftJoinAndSelect ( 'company.users' , 'user' ) ;
144160 qb . andWhere ( 'group.id = :groupId' , { groupId : groupId } ) ;
145- return await qb . getOne ( ) ;
161+ const connection = await qb . getOne ( ) ;
162+ if ( connection ) {
163+ await decryptConnectionCredentialsAsync ( connection ) ;
164+ }
165+ return connection ;
146166 } ,
147167
148168 async findOneById ( connectionId : string ) : Promise < ConnectionEntity | null > {
149- return await this . findOne ( { where : { id : connectionId } } ) ;
169+ const connection = await this . findOne ( { where : { id : connectionId } } ) ;
170+ if ( connection ) {
171+ await decryptConnectionCredentialsAsync ( connection ) ;
172+ }
173+ return connection ;
150174 } ,
151175
152176 async findOneAgentConnectionByToken ( connectionToken : string ) : Promise < ConnectionEntity | null > {
153177 const qb = this . createQueryBuilder ( 'connection' ) . leftJoinAndSelect ( 'connection.agent' , 'agent' ) ;
154178 qb . andWhere ( 'agent.token = :agentToken' , { agentToken : connectionToken } ) ;
155- return await qb . getOne ( ) ;
179+ const connection = await qb . getOne ( ) ;
180+ if ( connection ) {
181+ await decryptConnectionCredentialsAsync ( connection ) ;
182+ }
183+ return connection ;
156184 } ,
157185
158186 async isTestConnectionById ( connectionId : string ) : Promise < boolean > {
@@ -179,13 +207,12 @@ export const customConnectionRepositoryExtension: IConnectionRepository &
179207
180208 async findAllCompanyUsersNonTestsConnections ( companyId : string ) : Promise < Array < ConnectionEntity > > {
181209 const connectionQb = this . createQueryBuilder ( 'connection' )
182- . leftJoin ( 'connection.groups' , 'group' )
183- . leftJoin ( 'group.users' , 'user' )
184- . leftJoin ( 'user.company' , 'company' )
185210 . leftJoinAndSelect ( 'connection.connection_properties' , 'connection_properties' )
186211 . where ( 'connection.isTestConnection = :isTest' , { isTest : false } )
187- . andWhere ( 'company.id = :companyId' , { companyId : companyId } ) ;
188- return await connectionQb . getMany ( ) ;
212+ . andWhere ( 'connection.companyId = :companyId' , { companyId : companyId } ) ;
213+ const connections = await connectionQb . getMany ( ) ;
214+ await decryptConnectionsCredentialsAsync ( connections ) ;
215+ return connections ;
189216 } ,
190217
191218 async freezeConnections ( connectionsIds : Array < string > ) : Promise < void > {
@@ -210,7 +237,9 @@ export const customConnectionRepositoryExtension: IConnectionRepository &
210237 . where ( 'user.id = :userId' , { userId : userId } )
211238 . andWhere ( 'connection.isTestConnection = :isTest' , { isTest : true } )
212239 . andWhere ( 'connection.company IS NULL' ) ;
213- return await qb . getMany ( ) ;
240+ const connections = await qb . getMany ( ) ;
241+ await decryptConnectionsCredentialsAsync ( connections ) ;
242+ return connections ;
214243 } ,
215244
216245 decryptConnectionField ( field : string ) : string {
0 commit comments