From 0a1c3426c04c78069ca0de19d6cddd0c89b16592 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Sun, 17 Jan 2021 19:52:08 +0100 Subject: [PATCH] private: fixed token generation bugs Signed-off-by: Philip Molares --- src/users/users.service.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 9de3730c0..db2ee724e 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -39,19 +39,24 @@ export class UsersService { ): Promise { const user = await this.getUserByUsername(userName); let accessToken = ''; + let randomString = ''; for (let i = 0; i < 100; i++) { try { - const randomString = crypt.randomBytes(64).toString(); + randomString = crypt.randomBytes(64).toString("base64"); accessToken = await this.hashPassword(randomString); await this.getUserByAuthToken(accessToken); } catch (NotInDBError) { const token = AuthToken.create(user, identifier, accessToken); - return this.authTokenRepository.save(token); + const createdToken = this.authTokenRepository.save(token); + return { + accessToken: randomString, + ...createdToken + } } } // This should never happen throw new RandomnessError( - 'You machine is not able to generate not-in-use tokens. This should never happen.', + 'Your machine is not able to generate not-in-use tokens. This should never happen.', ); }