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

Commit

Permalink
♻️ Use TokensService for UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 1, 2020
1 parent b147b68 commit b5d94ce
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/modules/access-tokens/access-tokens.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';
import { PrismaModule } from '../prisma/prisma.module';
import { TokensModule } from '../tokens/tokens.module';
import { AccessTokenController } from './access-tokens.controller';
import { AccessTokensService } from './access-tokens.service';

@Module({
imports: [PrismaModule],
imports: [PrismaModule, TokensModule],
controllers: [AccessTokenController],
providers: [AccessTokensService],
})
Expand Down
10 changes: 7 additions & 3 deletions src/modules/access-tokens/access-tokens.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import {
accessTokens,
accessTokensCreateInput,
Expand All @@ -15,15 +14,20 @@ import {
} from '@prisma/client';
import { Expose } from '../../modules/prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
import { TokensService } from '../tokens/tokens.service';

@Injectable()
export class AccessTokensService {
constructor(private prisma: PrismaService) {}
constructor(
private prisma: PrismaService,
private tokensService: TokensService,
) {}

async createAccessToken(
userId: number,
data: Omit<Omit<accessTokensCreateInput, 'accessToken'>, 'user'>,
): Promise<accessTokens> {
const accessToken = randomStringGenerator();
const accessToken = this.tokensService.generateUuid();
return this.prisma.accessTokens.create({
data: { ...data, accessToken, user: { connect: { id: userId } } },
});
Expand Down
3 changes: 2 additions & 1 deletion src/modules/api-keys/api-keys.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';
import { PrismaModule } from '../prisma/prisma.module';
import { TokensService } from '../tokens/tokens.service';
import { ApiKeyController } from './api-keys.controller';
import { ApiKeysService } from './api-keys.service';

@Module({
imports: [PrismaModule],
imports: [PrismaModule, TokensService],
controllers: [ApiKeyController],
providers: [ApiKeysService],
})
Expand Down
9 changes: 6 additions & 3 deletions src/modules/api-keys/api-keys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import {
apiKeys,
apiKeysCreateInput,
Expand All @@ -15,16 +14,20 @@ import {
} from '@prisma/client';
import { Expose } from '../../modules/prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
import { TokensService } from '../tokens/tokens.service';

@Injectable()
export class ApiKeysService {
constructor(private prisma: PrismaService) {}
constructor(
private prisma: PrismaService,
private tokensService: TokensService,
) {}

async createApiKey(
groupId: number,
data: Omit<Omit<apiKeysCreateInput, 'apiKey'>, 'group'>,
): Promise<apiKeys> {
const apiKey = randomStringGenerator();
const apiKey = this.tokensService.generateUuid();
return this.prisma.apiKeys.create({
data: { ...data, apiKey, group: { connect: { id: groupId } } },
});
Expand Down
7 changes: 3 additions & 4 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
UnauthorizedException,
UnprocessableEntityException,
} from '@nestjs/common';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { Authenticator } from '@otplib/core';
Expand Down Expand Up @@ -263,7 +262,7 @@ export class AuthService {
* @returns Data URI string with QR code image
*/
async getTotpQrCode(userId: number): Promise<string> {
const secret = randomStringGenerator() as string;
const secret = this.tokensService.generateUuid();
await this.prisma.users.update({
where: { id: userId },
data: { twoFactorSecret: secret },
Expand Down Expand Up @@ -292,7 +291,7 @@ export class AuthService {
'Two-factor authentication is already enabled',
);
if (!user.twoFactorSecret)
user.twoFactorSecret = randomStringGenerator() as string;
user.twoFactorSecret = this.tokensService.generateUuid();
if (!this.authenticator.check(code, user.twoFactorSecret))
throw new UnauthorizedException(
'Two-factor authentication code is invalid',
Expand Down Expand Up @@ -479,7 +478,7 @@ export class AuthService {
userAgent: string,
id: number,
): Promise<TokenResponse> {
const token = randomStringGenerator();
const token = this.tokensService.generateUuid();
await this.prisma.sessions.create({
data: { token, ipAddress, userAgent, user: { connect: { id } } },
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import { ConfigModule } from '@nestjs/config';
import { AuthModule } from '../auth/auth.module';
import { EmailModule } from '../email/email.module';
import { PrismaModule } from '../prisma/prisma.module';
import { TokensModule } from '../tokens/tokens.module';
import { TwilioModule } from '../twilio/twilio.module';
import { MultiFactorAuthenticationController } from './multi-factor-authentication.controller';
import { MultiFactorAuthenticationService } from './multi-factor-authentication.service';

@Module({
imports: [PrismaModule, AuthModule, TwilioModule, EmailModule, ConfigModule],
imports: [
PrismaModule,
AuthModule,
TwilioModule,
EmailModule,
ConfigModule,
TokensModule,
],
controllers: [MultiFactorAuthenticationController],
providers: [MultiFactorAuthenticationService],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import {
Injectable,
NotFoundException,
} from '@nestjs/common';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import { ConfigService } from '@nestjs/config';
import { MfaMethod, users } from '@prisma/client';
import { hash } from 'bcrypt';
import { AuthService } from '../auth/auth.service';
import { EmailService } from '../email/email.service';
import { Expose } from '../prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
import { TokensService } from '../tokens/tokens.service';
import { TwilioService } from '../twilio/twilio.service';

@Injectable()
Expand All @@ -21,6 +21,7 @@ export class MultiFactorAuthenticationService {
private configService: ConfigService,
private twilioService: TwilioService,
private emailService: EmailService,
private tokensService: TokensService,
) {}

async requestTotpMfa(userId: number): Promise<string> {
Expand All @@ -46,7 +47,7 @@ export class MultiFactorAuthenticationService {
throw new BadRequestException(
'Two-factor authentication is already enabled',
);
const secret = randomStringGenerator() as string;
const secret = this.tokensService.generateUuid();
await this.prisma.users.update({
where: { id: userId },
data: { twoFactorSecret: secret, twoFactorPhone: phone },
Expand Down Expand Up @@ -74,7 +75,7 @@ export class MultiFactorAuthenticationService {
throw new BadRequestException(
'Two-factor authentication is already enabled',
);
const secret = randomStringGenerator() as string;
const secret = this.tokensService.generateUuid();
await this.prisma.users.update({
where: { id: userId },
data: { twoFactorSecret: secret },
Expand Down Expand Up @@ -119,7 +120,7 @@ export class MultiFactorAuthenticationService {
await this.prisma.backupCodes.deleteMany({ where: { user: { id } } });
const codes: string[] = [];
for await (const _ of [...Array(10)]) {
const unsafeCode = randomStringGenerator();
const unsafeCode = this.tokensService.generateUuid();
codes.push(unsafeCode);
const code = await hash(
unsafeCode,
Expand Down

0 comments on commit b5d94ce

Please sign in to comment.