Added file/json logging.
This commit is contained in:
@ -1,13 +1,16 @@
|
|||||||
|
import pino from 'pino';
|
||||||
|
import * as path from 'path';
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { AppController } from './app.controller';
|
import { AppController } from './app.controller';
|
||||||
import { AppService } from './app.service';
|
import { AppService } from './app.service';
|
||||||
import { UsersService } from './users/users.service';
|
import { UsersService } from './users/users.service';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
import { DatabaseOptions } from './database-config/database.options';
|
import { DatabaseOptions } from './database-config/database.options';
|
||||||
import { UsersModule } from './users/users.module';
|
import { UsersModule } from './users/users.module';
|
||||||
import { UserEntity } from './users/users.entity';
|
import { UserEntity } from './users/users.entity';
|
||||||
import { AuthModule } from './auth/auth.module';
|
import { AuthModule } from './auth/auth.module';
|
||||||
|
import { LoggerModule } from 'nestjs-pino';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -19,6 +22,23 @@ import { AuthModule } from './auth/auth.module';
|
|||||||
TypeOrmModule.forFeature([UserEntity]),
|
TypeOrmModule.forFeature([UserEntity]),
|
||||||
UsersModule,
|
UsersModule,
|
||||||
AuthModule,
|
AuthModule,
|
||||||
|
LoggerModule.forRootAsync({
|
||||||
|
imports: [ConfigModule],
|
||||||
|
inject: [ConfigService],
|
||||||
|
useFactory: async (config: ConfigService) => {
|
||||||
|
return {
|
||||||
|
pinoHttp: {
|
||||||
|
level: config.get('LOG_LEVEL') ?? 'info',
|
||||||
|
autoLogging: true,
|
||||||
|
stream: pino.destination({
|
||||||
|
dest: path.join(config.get('LOG_DIRECTORY') ?? 'logs', 'backend.api.log'),
|
||||||
|
minLength: 512,
|
||||||
|
sync: false,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
],
|
],
|
||||||
controllers: [AppController],
|
controllers: [AppController],
|
||||||
providers: [AppService, UsersService],
|
providers: [AppService, UsersService],
|
||||||
|
@ -3,12 +3,14 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { JwtService } from '@nestjs/jwt';
|
import { JwtService } from '@nestjs/jwt';
|
||||||
import { UserEntity } from 'src/users/users.entity';
|
import { UserEntity } from 'src/users/users.entity';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import { PinoLogger } from 'nestjs-pino';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthAccessService {
|
export class AuthAccessService {
|
||||||
constructor(
|
constructor(
|
||||||
private jwts: JwtService,
|
private jwts: JwtService,
|
||||||
private config: ConfigService,
|
private config: ConfigService,
|
||||||
|
private logger: PinoLogger,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async generate(user: UserEntity) {
|
async generate(user: UserEntity) {
|
||||||
@ -29,6 +31,13 @@ export class AuthAccessService {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.logger.debug({
|
||||||
|
class: AuthAccessService.name,
|
||||||
|
method: this.generate.name,
|
||||||
|
user_login: user.userLogin,
|
||||||
|
msg: 'User generated an access token.',
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
access_token: token,
|
access_token: token,
|
||||||
exp: expiration.getTime(),
|
exp: expiration.getTime(),
|
||||||
|
@ -8,10 +8,16 @@ import { JwtRefreshGuard } from './guards/jwt-refresh.guard';
|
|||||||
import { OfflineGuard } from './guards/offline.guard';
|
import { OfflineGuard } from './guards/offline.guard';
|
||||||
import { UserEntity } from 'src/users/users.entity';
|
import { UserEntity } from 'src/users/users.entity';
|
||||||
import { QueryFailedError } from 'typeorm';
|
import { QueryFailedError } from 'typeorm';
|
||||||
|
import { PinoLogger } from 'nestjs-pino';
|
||||||
|
import { JwtAccessGuard } from './guards/jwt-access.guard';
|
||||||
|
|
||||||
@Controller('auth')
|
@Controller('auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
constructor(private auth: AuthService, private users: UsersService) { }
|
constructor(
|
||||||
|
private auth: AuthService,
|
||||||
|
private users: UsersService,
|
||||||
|
private logger: PinoLogger,
|
||||||
|
) { }
|
||||||
|
|
||||||
@UseGuards(LoginAuthGuard)
|
@UseGuards(LoginAuthGuard)
|
||||||
@Post('login')
|
@Post('login')
|
||||||
@ -19,57 +25,53 @@ export class AuthController {
|
|||||||
@Request() req,
|
@Request() req,
|
||||||
@Res({ passthrough: true }) response: Response,
|
@Res({ passthrough: true }) response: Response,
|
||||||
) {
|
) {
|
||||||
|
let data: AuthenticationDto | null;
|
||||||
try {
|
try {
|
||||||
let data: AuthenticationDto | null;
|
data = await this.auth.login(req.user);
|
||||||
try {
|
if (!data.access_token || !data.refresh_token || !data.refresh_exp) {
|
||||||
data = await this.auth.login(req.user);
|
|
||||||
if (!data.access_token || !data.refresh_token || !data.refresh_exp) {
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
error_message: 'Something went wrong with tokens while logging in.',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
if (err instanceof QueryFailedError) {
|
|
||||||
if (err.message.includes('duplicate key value violates unique constraint "users_user_login_key"')) {
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
error_message: 'Username already exist.',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
console.log('AuthController', typeof err, err);
|
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error_message: 'Something went wrong while logging in.',
|
error_message: 'Something went wrong with tokens while logging in.',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
response.cookie('Authentication', data.access_token, {
|
|
||||||
httpOnly: true,
|
|
||||||
secure: true,
|
|
||||||
expires: new Date(data.exp),
|
|
||||||
});
|
|
||||||
|
|
||||||
response.cookie('Refresh', data.refresh_token, {
|
|
||||||
httpOnly: true,
|
|
||||||
secure: true,
|
|
||||||
expires: new Date(data.refresh_exp),
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
success: true,
|
|
||||||
};
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
this.logger.error({
|
||||||
|
class: AuthController.name,
|
||||||
|
method: this.login.name,
|
||||||
|
msg: 'Failed to login.',
|
||||||
|
error: err,
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error_message: 'Something went wrong.',
|
error_message: 'Something went wrong while logging in.',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
response.cookie('Authentication', data.access_token, {
|
||||||
|
httpOnly: true,
|
||||||
|
secure: true,
|
||||||
|
expires: new Date(data.exp),
|
||||||
|
});
|
||||||
|
|
||||||
|
response.cookie('Refresh', data.refresh_token, {
|
||||||
|
httpOnly: true,
|
||||||
|
secure: true,
|
||||||
|
expires: new Date(data.refresh_exp),
|
||||||
|
});
|
||||||
|
|
||||||
|
this.logger.info({
|
||||||
|
class: AuthController.name,
|
||||||
|
method: this.login.name,
|
||||||
|
user_login: req.user.userLogin,
|
||||||
|
msg: 'User logged in.',
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(LoginAuthGuard)
|
@UseGuards(JwtAccessGuard)
|
||||||
@Post('logout')
|
@Post('logout')
|
||||||
async logout(
|
async logout(
|
||||||
@Request() req,
|
@Request() req,
|
||||||
@ -81,7 +83,14 @@ export class AuthController {
|
|||||||
|
|
||||||
response.clearCookie('Refresh');
|
response.clearCookie('Refresh');
|
||||||
response.clearCookie('Authentication');
|
response.clearCookie('Authentication');
|
||||||
|
|
||||||
|
this.logger.info({
|
||||||
|
class: AuthController.name,
|
||||||
|
method: this.logout.name,
|
||||||
|
user_login: req.user.userLogin,
|
||||||
|
msg: 'User logged off',
|
||||||
|
});
|
||||||
|
|
||||||
return req.logout();
|
return req.logout();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,6 +109,11 @@ export class AuthController {
|
|||||||
secure: true,
|
secure: true,
|
||||||
expires: new Date(data.exp),
|
expires: new Date(data.exp),
|
||||||
});
|
});
|
||||||
|
this.logger.debug({
|
||||||
|
class: AuthController.name,
|
||||||
|
method: this.refresh.name,
|
||||||
|
msg: 'Updated Authentication cookie for access token.',
|
||||||
|
});
|
||||||
|
|
||||||
if (data.refresh_token != refresh_token) {
|
if (data.refresh_token != refresh_token) {
|
||||||
response.cookie('Refresh', data.refresh_token, {
|
response.cookie('Refresh', data.refresh_token, {
|
||||||
@ -107,11 +121,21 @@ export class AuthController {
|
|||||||
secure: true,
|
secure: true,
|
||||||
expires: new Date(data.refresh_exp),
|
expires: new Date(data.refresh_exp),
|
||||||
});
|
});
|
||||||
|
this.logger.debug({
|
||||||
|
class: AuthController.name,
|
||||||
|
method: this.refresh.name,
|
||||||
|
msg: 'Updated Refresh cookie for refresh token.',
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
this.logger.error({
|
||||||
|
class: AuthController.name,
|
||||||
|
method: this.refresh.name,
|
||||||
|
msg: 'Failed to refresh tokens.',
|
||||||
|
error: err,
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error_message: 'Something went wrong.',
|
error_message: 'Something went wrong.',
|
||||||
@ -131,16 +155,32 @@ export class AuthController {
|
|||||||
try {
|
try {
|
||||||
const { user_login, user_name, password } = body;
|
const { user_login, user_name, password } = body;
|
||||||
user = await this.users.register(user_login.toLowerCase(), user_name, password, true);
|
user = await this.users.register(user_login.toLowerCase(), user_name, password, true);
|
||||||
|
this.logger.info({
|
||||||
|
class: AuthController.name,
|
||||||
|
method: this.register.name,
|
||||||
|
user_login: user.userLogin,
|
||||||
|
msg: 'User registered',
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof QueryFailedError) {
|
if (err instanceof QueryFailedError) {
|
||||||
if (err.message.includes('duplicate key value violates unique constraint "users_user_login_key"')) {
|
if (err.message.includes('duplicate key value violates unique constraint "users_user_login_key"')) {
|
||||||
|
this.logger.warn({
|
||||||
|
class: AuthController.name,
|
||||||
|
method: this.register.name,
|
||||||
|
msg: 'Failed to register due to duplicate userLogin.',
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error_message: 'Username already exist.',
|
error_message: 'Username already exist.',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log('AuthController', err);
|
this.logger.error({
|
||||||
|
class: AuthController.name,
|
||||||
|
method: this.register.name,
|
||||||
|
msg: 'Failed to register.',
|
||||||
|
error: err,
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error_message: 'Something went wrong when creating user.',
|
error_message: 'Something went wrong when creating user.',
|
||||||
@ -156,7 +196,12 @@ export class AuthController {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log('AuthController', err);
|
this.logger.error({
|
||||||
|
class: AuthController.name,
|
||||||
|
method: this.register.name,
|
||||||
|
msg: 'Failed to login after registering.',
|
||||||
|
error: err,
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error_message: 'Something went wrong while logging in.',
|
error_message: 'Something went wrong while logging in.',
|
||||||
@ -177,6 +222,6 @@ export class AuthController {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,6 +8,7 @@ import { UUID } from 'crypto';
|
|||||||
import { UserEntity } from 'src/users/users.entity';
|
import { UserEntity } from 'src/users/users.entity';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { AuthRefreshTokenEntity } from './entities/auth.refresh-token.entity';
|
import { AuthRefreshTokenEntity } from './entities/auth.refresh-token.entity';
|
||||||
|
import { PinoLogger } from 'nestjs-pino';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthRefreshService {
|
export class AuthRefreshService {
|
||||||
@ -15,7 +16,8 @@ export class AuthRefreshService {
|
|||||||
private jwts: JwtService,
|
private jwts: JwtService,
|
||||||
private config: ConfigService,
|
private config: ConfigService,
|
||||||
@InjectRepository(AuthRefreshTokenEntity)
|
@InjectRepository(AuthRefreshTokenEntity)
|
||||||
private authRefreshTokenRepository: Repository<AuthRefreshTokenEntity>
|
private authRefreshTokenRepository: Repository<AuthRefreshTokenEntity>,
|
||||||
|
private logger: PinoLogger,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
|
|
||||||
@ -23,7 +25,20 @@ export class AuthRefreshService {
|
|||||||
let expiration: Date | null = null;
|
let expiration: Date | null = null;
|
||||||
if (refreshToken) {
|
if (refreshToken) {
|
||||||
const token = await this.get(refreshToken, user.userId);
|
const token = await this.get(refreshToken, user.userId);
|
||||||
|
if (!token) {
|
||||||
|
this.logger.warn({
|
||||||
|
class: AuthRefreshService.name,
|
||||||
|
method: this.generate.name,
|
||||||
|
msg: 'Refresh token given is invalid.',
|
||||||
|
});
|
||||||
|
throw new UnauthorizedException('Invalid refresh token.');
|
||||||
|
}
|
||||||
if (token.exp.getTime() > new Date().getTime()) {
|
if (token.exp.getTime() > new Date().getTime()) {
|
||||||
|
this.logger.warn({
|
||||||
|
class: AuthRefreshService.name,
|
||||||
|
method: this.generate.name,
|
||||||
|
msg: 'Refresh token given has expired.',
|
||||||
|
});
|
||||||
throw new UnauthorizedException('Invalid refresh token.');
|
throw new UnauthorizedException('Invalid refresh token.');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +51,20 @@ export class AuthRefreshService {
|
|||||||
// - token has expired.
|
// - token has expired.
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const threshhold = parseInt(this.config.getOrThrow<string>('AUTH_JWT_REFRESH_TOKEN_EXPIRATION_THRESHHOLD_MS'));
|
const threshhold = parseInt(this.config.getOrThrow<string>('AUTH_JWT_REFRESH_TOKEN_EXPIRATION_THRESHHOLD_MS'));
|
||||||
if (!refreshToken || !expiration || now.getTime() - expiration.getTime() > threshhold) {
|
if (!refreshToken || now.getTime() - expiration.getTime() > threshhold) {
|
||||||
|
if (refreshToken) {
|
||||||
|
this.authRefreshTokenRepository.delete({
|
||||||
|
tokenHash: refreshToken,
|
||||||
|
userId: user.userId,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.logger.debug({
|
||||||
|
class: AuthRefreshService.name,
|
||||||
|
method: this.generate.name,
|
||||||
|
msg: 'Deleted previous refresh token.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const limit = parseInt(this.config.getOrThrow<string>('AUTH_JWT_REFRESH_TOKEN_EXPIRATION_MS'));
|
const limit = parseInt(this.config.getOrThrow<string>('AUTH_JWT_REFRESH_TOKEN_EXPIRATION_MS'));
|
||||||
expiration = moment(now).add(limit, 'ms').toDate();
|
expiration = moment(now).add(limit, 'ms').toDate();
|
||||||
refreshToken = await this.jwts.signAsync(
|
refreshToken = await this.jwts.signAsync(
|
||||||
@ -52,11 +80,23 @@ export class AuthRefreshService {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.logger.debug({
|
||||||
|
class: AuthRefreshService.name,
|
||||||
|
method: this.generate.name,
|
||||||
|
msg: 'Generated a new refresh token.',
|
||||||
|
});
|
||||||
|
|
||||||
this.authRefreshTokenRepository.insert({
|
this.authRefreshTokenRepository.insert({
|
||||||
tokenHash: refreshToken,
|
tokenHash: refreshToken,
|
||||||
userId: user.userId,
|
userId: user.userId,
|
||||||
exp: expiration
|
exp: expiration
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.logger.debug({
|
||||||
|
class: AuthRefreshService.name,
|
||||||
|
method: this.generate.name,
|
||||||
|
msg: 'Inserted the new refresh token.',
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -19,7 +19,6 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async validate(req: Request, payload: any) {
|
async validate(req: Request, payload: any) {
|
||||||
console.log('jwt payload', payload);
|
|
||||||
const user = await this.users.findById(payload.sub);
|
const user = await this.users.findById(payload.sub);
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new UnauthorizedException();
|
throw new UnauthorizedException();
|
||||||
|
@ -2,15 +2,18 @@ import * as cookieParser from 'cookie-parser';
|
|||||||
import { NestFactory } from '@nestjs/core';
|
import { NestFactory } from '@nestjs/core';
|
||||||
import { AppModule } from './app.module';
|
import { AppModule } from './app.module';
|
||||||
import { ValidationPipe } from '@nestjs/common';
|
import { ValidationPipe } from '@nestjs/common';
|
||||||
|
import { Logger, LoggerErrorInterceptor } from 'nestjs-pino';
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule, { bufferLogs: true });
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
app.useGlobalPipes(new ValidationPipe({
|
app.useGlobalPipes(new ValidationPipe({
|
||||||
stopAtFirstError: true,
|
stopAtFirstError: true,
|
||||||
whitelist: true,
|
whitelist: true,
|
||||||
transform: true,
|
transform: true,
|
||||||
}));
|
}));
|
||||||
|
app.useLogger(app.get(Logger));
|
||||||
|
app.useGlobalInterceptors(new LoggerErrorInterceptor());
|
||||||
await app.listen(process.env.PORT ?? 3001);
|
await app.listen(process.env.PORT ?? 3001);
|
||||||
}
|
}
|
||||||
bootstrap();
|
bootstrap();
|
||||||
|
Reference in New Issue
Block a user