Skip to content

Commit

Permalink
refactor(api): remove $or query for hash on env (#6969)
Browse files Browse the repository at this point in the history
  • Loading branch information
scopsy authored Nov 13, 2024
1 parent 63bb3e6 commit 9fedf50
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .source
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export class GenerateUniqueApiKey {
const hashedApiKey = createHash('sha256').update(apiKey).digest('hex');

const environment = await this.environmentRepository.findByApiKey({
key: apiKey,
hash: hashedApiKey,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,6 @@ export class CommunityAuthService implements IAuthService {
);
}

@CachedEntity({
builder: ({ apiKey }: { apiKey: string }) =>
buildAuthServiceKey({
apiKey,
}),
})
private async getApiKeyUser({ apiKey }: { apiKey: string }): Promise<{
environment?: EnvironmentEntity;
user?: UserEntity;
Expand All @@ -364,7 +358,6 @@ export class CommunityAuthService implements IAuthService {
const hashedApiKey = createHash('sha256').update(apiKey).digest('hex');

const environment = await this.environmentRepository.findByApiKey({
key: apiKey,
hash: hashedApiKey,
});

Expand All @@ -373,16 +366,7 @@ export class CommunityAuthService implements IAuthService {
return { error: 'API Key not found' };
}

let key = environment.apiKeys.find((i) => i.hash === hashedApiKey);

if (!key) {
/*
* backward compatibility - delete after encrypt-api-keys-migration execution
* find by decrypted key if key not found, because of backward compatibility
* use-case: findByApiKey found by decrypted key, so we need to validate by decrypted key
*/
key = environment.apiKeys.find((i) => i.key === apiKey);
}
const key = environment.apiKeys.find((i) => i.hash === hashedApiKey);

if (!key) {
return { error: 'API Key not found' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ export class EnvironmentRepository extends BaseRepository<EnvironmentDBModel, En
);
}

// backward compatibility - update the query to { 'apiKeys.hash': hash } once encrypt-api-keys-migration executed
async findByApiKey({ key, hash }: { key: string; hash: string }) {
return await this.findOne({ $or: [{ 'apiKeys.key': key }, { 'apiKeys.hash': hash }] });
async findByApiKey({ hash }: { hash: string }) {
return await this.findOne({ 'apiKeys.hash': hash }, undefined, { readPreference: 'secondaryPreferred' });
}

async getApiKeys(environmentId: string): Promise<IApiKey[]> {
Expand Down
4 changes: 4 additions & 0 deletions libs/dal/src/repositories/environment/environment.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ environmentSchema.index({
_organizationId: 1,
});

environmentSchema.index({
'apiKeys.hash': 1,
});

export const Environment =
(mongoose.models.Environment as mongoose.Model<EnvironmentDBModel>) ||
mongoose.model<EnvironmentDBModel>('Environment', environmentSchema);

0 comments on commit 9fedf50

Please sign in to comment.