From 9372605535e985053e902e1ad87e7773ac85e3aa Mon Sep 17 00:00:00 2001 From: vigdorov Date: Tue, 4 Aug 2020 21:11:53 +0300 Subject: [PATCH] deletee host --- src/auth/auth.controller.ts | 10 ++++------ src/users/users.schema.ts | 3 --- src/users/users.service.ts | 23 ++++++++++------------- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index ba5c87c..78e9ad6 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -42,9 +42,8 @@ export class AuthController { description: 'Объект с логином и паролем пользователя для авторизации' }) async authUser(@Req() request: Request): Promise { - const host = request.headers.host; const agent = request.headers['user-agent']; - return await this.userService.authUser(request.body.login, request.body.password, host, agent); + return await this.userService.authUser(request.body.login, request.body.password, agent); } @Post('refresh') @@ -56,10 +55,9 @@ export class AuthController { description: 'Токен для сброса токенов' }) async refreshAuth(@Req() request: Request): Promise { - const host = request.headers.host; const agent = request.headers['user-agent']; const refresh_token = request.body.refresh_token; - return await this.userService.refreshAuth(refresh_token, host, agent); + return await this.userService.refreshAuth(refresh_token, agent); } @Post('check') @@ -69,8 +67,8 @@ export class AuthController { description: 'Токен для проверки', }) async checkAccessToken(@Req() request: Request): Promise { - const {access_token, host, agent} = request.body; - return this.userService.checkAccessToken(access_token, host, agent); + const {access_token, agent} = request.body; + return this.userService.checkAccessToken(access_token, agent); } @Options([ diff --git a/src/users/users.schema.ts b/src/users/users.schema.ts index 0174286..63d4365 100644 --- a/src/users/users.schema.ts +++ b/src/users/users.schema.ts @@ -58,9 +58,6 @@ export class CheckAuthTokenRequest { @ApiProperty() access_token: string; - @ApiProperty() - host: string; - @ApiProperty() agent: string; } diff --git a/src/users/users.service.ts b/src/users/users.service.ts index d754db5..e391c11 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -8,7 +8,6 @@ import * as jwt from 'jsonwebtoken'; interface Token { login: string; - host: string; agent: string; iat: number; exp: number; @@ -143,17 +142,15 @@ export class UserService { return bcrypt.compare(password, hash); } - generateTokens(login: string, host: string, agent: string): TokenResponse { + generateTokens(login: string, agent: string): TokenResponse { const access_token = jwt.sign({ login, - host, agent, iat: Math.floor(Date.now() / 1000), exp: Math.floor(Date.now() / 1000) + (20), // секунды * минуты // выставить 60 * 2 }, SECRET_JWT_ACCESS_KEY); const refresh_token = jwt.sign({ login, - host, agent, iat: Math.floor(Date.now() / 1000), exp: Math.floor(Date.now() / 1000) + (60 * 5), // секунды * минуты * часы * дни // потом выставить 60 * 60 * 24 * 1 @@ -164,10 +161,10 @@ export class UserService { }; } - async authUser(login: string, password: string, host: string, agent: string): Promise { + async authUser(login: string, password: string, agent: string): Promise { const searchUser = await this.findUser(login); if (searchUser && await this.checkPassword(password, searchUser.password)) { - return this.generateTokens(login, host, agent); + return this.generateTokens(login, agent); } throw new BadRequestException('Не верный логин или пароль'); @@ -181,21 +178,21 @@ export class UserService { } } - checkToken(token: Token, host: string, agent: string): boolean { - return token.host === host && token.agent === agent; + checkToken(token: Token, agent: string): boolean { + return token.agent === agent; } - async refreshAuth(refresh_token: string, host: string, agent: string): Promise { + async refreshAuth(refresh_token: string, agent: string): Promise { this.verifyToken(refresh_token, SECRET_JWT_REFRESH_KEY); const token = jwt.decode(refresh_token) as Token; const searchUser = await this.findUser(token.login); - if (searchUser && this.checkToken(token, host, agent)) { - return this.generateTokens(token.login, host, agent); + if (searchUser && this.checkToken(token, agent)) { + return this.generateTokens(token.login, agent); } throw new BadRequestException('Unauthorized request'); } - async checkAccessToken(access_token: string, host: string, agent: string): Promise { + async checkAccessToken(access_token: string, agent: string): Promise { try { this.verifyToken(access_token, SECRET_JWT_ACCESS_KEY); } catch (e) { @@ -204,6 +201,6 @@ export class UserService { const token = jwt.decode(access_token) as Token; const searchUser = await this.findUser(token.login); - return searchUser && this.checkToken(token, host, agent); + return searchUser && this.checkToken(token, agent); } } \ No newline at end of file