Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Change 2fa to totp
Browse files Browse the repository at this point in the history
AnandChowdhary committed Oct 29, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent cee3a55 commit 750eb88
Showing 3 changed files with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ import {
import { users } from '@prisma/client';
import { Expose } from 'src/modules/prisma/prisma.interface';
import { Scopes } from '../auth/scope.decorator';
import { EnableTwoFactorAuthenticationDto } from './multi-factor-authentication.dto';
import { EnableTotpMfaDto } from './multi-factor-authentication.dto';
import { MultiFactorAuthenticationService } from './multi-factor-authentication.service';

@Controller('users/:userId/multi-factor-authentication')
@@ -18,29 +18,25 @@ export class MultiFactorAuthenticationController {
private multiFactorAuthenticationService: MultiFactorAuthenticationService,
) {}

@Post('2fa')
@Scopes('user-{userId}:write-2fa')
@Post('totp')
@Scopes('user-{userId}:write-totp')
async enable2FA(
@Param('userId', ParseIntPipe) userId: number,
@Body() body: EnableTwoFactorAuthenticationDto,
@Body() body: EnableTotpMfaDto,
): Promise<Expose<users> | string> {
if (body.token)
return this.multiFactorAuthenticationService.enableTwoFactorAuthentication(
return this.multiFactorAuthenticationService.enableTotpMfa(
userId,
body.token,
);
return this.multiFactorAuthenticationService.requestTwoFactorAuthentication(
userId,
);
return this.multiFactorAuthenticationService.requestTotpMfa(userId);
}

@Delete('2fa')
@Scopes('user-{userId}:delete-2fa')
@Delete('totp')
@Scopes('user-{userId}:delete-totp')
async disable2FA(
@Param('userId', ParseIntPipe) userId: number,
): Promise<Expose<users>> {
return this.multiFactorAuthenticationService.disableTwoFactorAuthentication(
userId,
);
return this.multiFactorAuthenticationService.disableTotpMfa(userId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IsOptional, IsString } from 'class-validator';

export class EnableTwoFactorAuthenticationDto {
export class EnableTotpMfaDto {
@IsString()
@IsOptional()
token?: string;
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import { PrismaService } from '../prisma/prisma.service';
export class MultiFactorAuthenticationService {
constructor(private prisma: PrismaService, private auth: AuthService) {}

async requestTwoFactorAuthentication(userId: number): Promise<string> {
async requestTotpMfa(userId: number): Promise<string> {
const enabled = await this.prisma.users.findOne({
where: { id: userId },
select: { twoFactorEnabled: true },
@@ -20,14 +20,11 @@ export class MultiFactorAuthenticationService {
return this.auth.getTotpQrCode(userId);
}

async enableTwoFactorAuthentication(
userId: number,
token: string,
): Promise<Expose<users>> {
async enableTotpMfa(userId: number, token: string): Promise<Expose<users>> {
return this.auth.enableTotp(userId, token);
}

async disableTwoFactorAuthentication(userId: number): Promise<Expose<users>> {
async disableTotpMfa(userId: number): Promise<Expose<users>> {
const enabled = await this.prisma.users.findOne({
where: { id: userId },
select: { twoFactorEnabled: true },

0 comments on commit 750eb88

Please sign in to comment.