apps/api: move /auth method to root

This commit is contained in:
vchikalkin 2023-11-01 00:06:12 +03:00
parent a42aa89aec
commit d372007e0e
6 changed files with 62 additions and 40 deletions

View File

@ -0,0 +1,27 @@
import { AppService } from './app.service';
import { env } from './config/env';
import { Controller, Get, HttpStatus, Req, Res } from '@nestjs/common';
import { FastifyReply, FastifyRequest } from 'fastify';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('auth')
public async auth(@Req() req: FastifyRequest, @Res() reply: FastifyReply) {
const token = req.cookies[env.COOKIE_TOKEN_NAME];
try {
this.appService.checkToken(token);
return reply.send();
} catch {
// if (error.name === 'TokenExpiredError') {
// const newToken = this.appService.refreshToken(token);
// return reply.setCookie(env.COOKIE_TOKEN_NAME, newToken, cookieOptions).send();
// }
return reply.status(HttpStatus.UNAUTHORIZED).send();
}
}
}

View File

@ -1,3 +1,5 @@
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { env } from './config/env';
import { LdapModule } from './ldap/ldap.module';
import { UsersModule } from './users/users.module';
@ -8,7 +10,7 @@ import { MongooseModule } from '@nestjs/mongoose';
@Global()
@Module({
controllers: [],
controllers: [AppController],
exports: [JwtModule],
imports: [
ConfigModule.forRoot({
@ -24,6 +26,7 @@ import { MongooseModule } from '@nestjs/mongoose';
UsersModule,
MongooseModule.forRoot(`mongodb://${env.MONGO_HOST}`),
],
providers: [AppService],
})
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class AppModule {}

View File

@ -0,0 +1,19 @@
import type { DecodedToken } from './ldap/types/jwt';
import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
@Injectable()
export class AppService {
constructor(private readonly jwtService: JwtService) {}
public checkToken(token: string) {
this.jwtService.verify(token);
}
public refreshToken(token: string) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { exp, iat, ...payload } = this.jwtService.decode(token) as DecodedToken;
return this.jwtService.sign(payload);
}
}

View File

@ -0,0 +1,9 @@
import type { CookieSerializeOptions } from '@fastify/cookie';
import { env } from 'src/config/env';
export const cookieOptions: CookieSerializeOptions = {
httpOnly: true,
maxAge: env.API_TOKEN_TTL,
path: '/',
secure: true,
};

View File

@ -6,19 +6,13 @@ import { LdapService } from './ldap.service';
import type { CookieSerializeOptions } from '@fastify/cookie';
import { Body, Controller, Get, HttpException, HttpStatus, Post, Req, Res } from '@nestjs/common';
import { FastifyReply, FastifyRequest } from 'fastify';
import { cookieOptions } from 'src/config/cookie';
import { env } from 'src/config/env';
@Controller('ldap')
export class LdapController {
cookieOptions: CookieSerializeOptions;
constructor(private readonly ldapService: LdapService) {
this.cookieOptions = {
httpOnly: true,
maxAge: env.API_TOKEN_TTL,
path: '/',
secure: true,
};
}
constructor(private readonly ldapService: LdapService) {}
private clearCookies(req, reply) {
if (req.cookies) {
@ -37,7 +31,7 @@ export class LdapController {
try {
const token = await this.ldapService.login(login, password);
return reply.setCookie(env.COOKIE_TOKEN_NAME, token, this.cookieOptions).status(200).send();
return reply.setCookie(env.COOKIE_TOKEN_NAME, token, cookieOptions).status(200).send();
} catch {
throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED);
}
@ -53,25 +47,6 @@ export class LdapController {
return reply.status(302).redirect('/login');
}
@Get('auth')
async auth(@Req() req: FastifyRequest, @Res() reply: FastifyReply) {
const token = req.cookies[env.COOKIE_TOKEN_NAME];
try {
this.ldapService.checkToken(token);
return reply.send();
} catch (error) {
if (error.name === 'TokenExpiredError') {
const newToken = this.ldapService.refreshToken(token);
return reply.setCookie(env.COOKIE_TOKEN_NAME, newToken, this.cookieOptions).send();
}
return reply.status(HttpStatus.UNAUTHORIZED).send();
}
}
@Get('/get-user')
async getUser(@Req() req: FastifyRequest, @Res() reply: FastifyReply) {
const token = req.cookies[env.COOKIE_TOKEN_NAME];

View File

@ -35,17 +35,6 @@ export class LdapService {
}
}
public checkToken(token: string) {
this.jwtService.verify(token);
}
public refreshToken(token: string) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { exp, iat, ...payload } = this.jwtService.decode(token) as DecodedToken;
return this.jwtService.sign(payload);
}
public async getUser(token: string) {
const { username } = this.jwtService.decode(token) as DecodedToken;