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

Commit

Permalink
✨ Add SMS-based MFA method
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 8, 2020
1 parent 1a05c45 commit 727b611
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/errors/errors.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const MFA_ENABLED_CONFLICT =
export const MFA_NOT_ENABLED = 'MFA_NOT_ENABLED';
export const MFA_BACKUP_CODE_USED = 'MFA_BACKUP_CODE_USED';
export const MFA_PHONE_OR_TOKEN_REQUIRED = 'MFA_BACKUP_CODE_USED';
export const MFA_PHONE_NOT_FOUND = 'MFA_BACKUP_CODE_USED';
export const UNVERIFIED_LOCATION = 'UNVERIFIED_LOCATION';

export const CURRENT_PASSWORD_REQUIRED = 'Current password is required';
Expand Down
2 changes: 2 additions & 0 deletions src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { GeolocationModule } from '../geolocation/geolocation.module';
import { PrismaModule } from '../prisma/prisma.module';
import { PwnedModule } from '../pwned/pwned.module';
import { TokensModule } from '../tokens/tokens.module';
import { TwilioModule } from '../twilio/twilio.module';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { StaartStrategy } from './staart.strategy';
Expand All @@ -23,6 +24,7 @@ import { StaartStrategy } from './staart.strategy';
ConfigModule,
PwnedModule,
ApiKeysModule,
TwilioModule,
GeolocationModule,
ApprovedSubnetsModule,
JwtModule.register({
Expand Down
28 changes: 20 additions & 8 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ import { authenticator } from 'otplib';
import qrcode from 'qrcode';
import randomColor from 'randomcolor';
import {
COMPROMISED_PASSWORD,
EMAIL_USER_CONFLICT,
EMAIL_VERIFIED_CONFLICT,
INVALID_CREDENTIALS,
INVALID_MFA_CODE,
MFA_BACKUP_CODE_USED,
MFA_ENABLED_CONFLICT,
MFA_NOT_ENABLED,
MFA_PHONE_NOT_FOUND,
NO_EMAILS,
UNVERIFIED_EMAIL,
USER_NOT_FOUND,
EMAIL_VERIFIED_CONFLICT,
NO_TOKEN_PROVIDED,
SESSION_NOT_FOUND,
MFA_ENABLED_CONFLICT,
INVALID_MFA_CODE,
MFA_NOT_ENABLED,
MFA_BACKUP_CODE_USED,
UNVERIFIED_EMAIL,
UNVERIFIED_LOCATION,
COMPROMISED_PASSWORD,
USER_NOT_FOUND,
} from 'src/errors/errors.constants';
import { safeEmail } from '../../helpers/safe-email';
import { ApprovedSubnetsService } from '../approved-subnets/approved-subnets.service';
Expand All @@ -49,6 +50,7 @@ import {
PASSWORD_RESET_TOKEN,
} from '../tokens/tokens.constants';
import { TokensService } from '../tokens/tokens.service';
import { TwilioService } from '../twilio/twilio.service';
import { RegisterDto } from './auth.dto';
import {
AccessTokenClaims,
Expand All @@ -70,6 +72,7 @@ export class AuthService {
private tokensService: TokensService,
private geolocationService: GeolocationService,
private approvedSubnetsService: ApprovedSubnetsService,
private twilioService: TwilioService,
) {
this.authenticator = authenticator.create({
window: [
Expand Down Expand Up @@ -513,6 +516,15 @@ export class AuthService {
)}`,
},
});
} else if (user.twoFactorMethod === 'SMS' || forceMethod === 'SMS') {
if (!user.twoFactorPhone)
throw new BadRequestException(MFA_PHONE_NOT_FOUND);
this.twilioService.send({
to: user.twoFactorPhone,
body: `${this.getOneTimePassword(user.twoFactorSecret)} is your ${
this.configService.get<string>('sms.smsServiceName') ?? ''
} verification code.`,
});
}
return { totpToken, type: user.twoFactorMethod, multiFactorRequired: true };
}
Expand Down
2 changes: 2 additions & 0 deletions src/modules/emails/emails.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GeolocationService } from '../geolocation/geolocation.service';
import { PrismaModule } from '../prisma/prisma.module';
import { PwnedModule } from '../pwned/pwned.module';
import { TokensModule } from '../tokens/tokens.module';
import { TwilioModule } from '../twilio/twilio.module';
import { UsersService } from '../users/users.service';
import { EmailController } from './emails.controller';
import { EmailsService } from './emails.service';
Expand All @@ -17,6 +18,7 @@ import { EmailsService } from './emails.service';
PrismaModule,
EmailModule,
ConfigModule,
TwilioModule,
PwnedModule,
TokensModule,
JwtModule.register({
Expand Down

0 comments on commit 727b611

Please sign in to comment.