From 2f902755c47af829ae3eb6447a5cabe3c0017cf4 Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Sat, 5 Dec 2020 12:55:38 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20starting=20work=20on=20RefreshAu?= =?UTF-8?q?th?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ref #12 --- src/models/creation/RefreshAuth.ts | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/models/creation/RefreshAuth.ts diff --git a/src/models/creation/RefreshAuth.ts b/src/models/creation/RefreshAuth.ts new file mode 100644 index 0000000..16233c4 --- /dev/null +++ b/src/models/creation/RefreshAuth.ts @@ -0,0 +1,36 @@ +import { IsString } from 'class-validator'; +import * as jsonwebtoken from 'jsonwebtoken'; +import { getConnectionManager } from 'typeorm'; +import { IllegalJWTError, JwtNotProvidedError, UserNotFoundError } from '../../errors/AuthError'; +import { Auth } from '../entities/Auth'; +import { User } from '../entities/User'; + +export class RefreshAuth { + @IsString() + token: string; + + public async toAuth(): Promise { + let newAuth: Auth = new Auth(); + if (!this.token || this.token === undefined) { + throw new JwtNotProvidedError() + } + let decoded + try { + decoded = jsonwebtoken.verify(this.token, 'securekey') + } catch (error) { + throw new IllegalJWTError() + } + const found_users = await getConnectionManager().get().getRepository(User).findOne({ id: decoded["userid"], refreshTokenCount: decoded["refreshtokencount"] }); + if (!found_users) { + throw new UserNotFoundError() + } else { + const found_user = found_users[0] + delete found_user.password; + newAuth.access_token = "ja" + newAuth.access_token_expires_at = 5555555 + newAuth.refresh_token = "ja" + newAuth.refresh_token_expires_at = 555555 + } + return newAuth; + } +} \ No newline at end of file