diff --git a/src/auth/auth.service.spec.ts b/src/auth/auth.service.spec.ts index 695c36578..2676328e3 100644 --- a/src/auth/auth.service.spec.ts +++ b/src/auth/auth.service.spec.ts @@ -17,6 +17,7 @@ import { NotInDBError, TokenNotValidError } from '../errors/errors'; import { Repository } from 'typeorm'; import { ConfigModule } from '@nestjs/config'; import appConfigMock from '../config/mock/app.config.mock'; +import { randomBytes } from 'crypto'; describe('AuthService', () => { let service: AuthService; @@ -79,7 +80,7 @@ describe('AuthService', () => { .then((result) => expect(result).toBeTruthy()); }); it('fails, if secret is too short', async () => { - const secret = service.bufferToBase64Url(service.randomString(54)); + const secret = service.bufferToBase64Url(randomBytes(54)); const hash = await service.hashPassword(secret); await service .checkPassword(secret, hash) @@ -328,10 +329,4 @@ describe('AuthService', () => { ); }); }); - describe('randomString', () => { - it('throws on invalid lenght parameter', () => { - expect(() => service.randomString(0)).toThrow(); - expect(() => service.randomString(-1)).toThrow(); - }); - }); }); diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 1c4d50ae8..cb7a16535 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -16,12 +16,12 @@ import { TokenNotValidError, TooManyTokensError, } from '../errors/errors'; -import { randomBytes } from 'crypto'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { ConsoleLoggerService } from '../logger/console-logger.service'; import { TimestampMillis } from '../utils/timestamp'; import { Cron, Timeout } from '@nestjs/schedule'; +import { randomBytes } from 'crypto'; @Injectable() export class AuthService { @@ -62,13 +62,6 @@ export class AuthService { return await compare(cleartext, password); } - randomString(length: number): Buffer { - if (length <= 0) { - throw new Error('randomString cannot have a length < 1'); - } - return randomBytes(length); - } - bufferToBase64Url(text: Buffer): string { // This is necessary as the is no base64url encoding in the toString method // but as can be seen on https://tools.ietf.org/html/rfc4648#page-7 @@ -93,8 +86,8 @@ export class AuthService { `User '${user.userName}' has already 200 tokens and can't have anymore`, ); } - const secret = this.bufferToBase64Url(this.randomString(54)); - const keyId = this.bufferToBase64Url(this.randomString(8)); + const secret = this.bufferToBase64Url(randomBytes(54)); + const keyId = this.bufferToBase64Url(randomBytes(8)); const accessToken = await this.hashPassword(secret); let token; // Tokens can only be valid for a maximum of 2 years