Skip to content

Commit

Permalink
fix: 🐞 remove deprecated propety and apply JWT to refresh secret
Browse files Browse the repository at this point in the history
remove deprecated propety and apply JWT to refresh secret
  • Loading branch information
tal-rofe committed Jul 29, 2022
1 parent 430c406 commit 0b882a8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
9 changes: 9 additions & 0 deletions apps/backend/src/modules/database/client-secret.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ export class DBClientSecretService {
},
});
}

public async getSecretExpiration(secretId: string) {
const secret = await this.prisma.clientSecret.findUniqueOrThrow({
where: { id: secretId },
select: { expiration: true },
});

return secret.expiration;
}
}
6 changes: 2 additions & 4 deletions apps/backend/src/modules/database/inline-policy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ export class DBInlinePolicyService {
}

public async addRule(inlinePolicyId: string, rule: Record<string, unknown>) {
const inlinePolicyDB = await this.prisma.inlinePolicy.findFirst({
const inlinePolicyDB = await this.prisma.inlinePolicy.findUniqueOrThrow({
where: { id: inlinePolicyId },
select: { rules: true },
rejectOnNotFound: true,
});

let newInlinePolicyRules: Prisma.JsonObject;
Expand All @@ -60,10 +59,9 @@ export class DBInlinePolicyService {
}

public async removeRule(inlinePolicyId: string, ruleName: string) {
const inlinePolicyDB = await this.prisma.inlinePolicy.findFirst({
const inlinePolicyDB = await this.prisma.inlinePolicy.findUniqueOrThrow({
where: { id: inlinePolicyId },
select: { rules: true },
rejectOnNotFound: true,
});

if (!inlinePolicyDB.rules) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export class RefreshSecretContract {
constructor(public readonly secretId: string) {}
constructor(
public readonly userId: string,
public readonly userEmail: string,
public readonly secretId: string,
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ export class RefreshSecretHandler implements IQueryHandler<RefreshSecretContract
) {}

async execute(contract: RefreshSecretContract) {
const secret = this.secretsService.generateSecret();
const expiration = await this.dbClientSecretService.getSecretExpiration(contract.secretId);

const secret = await this.secretsService.generateSecret(
contract.userId,
contract.userEmail,
expiration ? expiration.getTime() : null,
);

await this.dbClientSecretService.refreshSecret(contract.secretId, secret);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Controller, HttpCode, HttpStatus, Logger, Param, Patch, UseGuards } from '@nestjs/common';
import { QueryBus } from '@nestjs/cqrs';

import { CurrentUserId } from '@/decorators/current-user-id.decorator';
import { CurrentUserEmail } from '@/decorators/current-user-email.decorator';

import { BelongingSecretGuard } from './guards/belonging-secret.guard';
import type { IRefreshClientSecret } from './interfaces/responses';
import { RefreshSecretContract } from './queries/contracts/refresh-secret.contract';
Expand All @@ -15,11 +18,17 @@ export class RefreshSecretController {
@UseGuards(BelongingSecretGuard)
@Patch(Routes.REFRSH_SECRET)
@HttpCode(HttpStatus.OK)
public async refreshSecret(@Param('secret_id') secretId: string): Promise<IRefreshClientSecret> {
this.logger.log(`Will try to refresh a client secret with Id: "${secretId}"`);
public async refreshSecret(
@Param('secret_id') secretId: string,
@CurrentUserId() userId: string,
@CurrentUserEmail() userEmail: string,
): Promise<IRefreshClientSecret> {
this.logger.log(
`Will try to refresh a client secret with Id: "${secretId}" for a user with Id: "${userId}"`,
);

const secret = await this.queryBus.execute<RefreshSecretContract, string>(
new RefreshSecretContract(secretId),
new RefreshSecretContract(userId, userEmail, secretId),
);

this.logger.log('Successfully refreshed a client secret');
Expand Down

0 comments on commit 0b882a8

Please sign in to comment.