Compare commits

...

7 Commits

19 changed files with 156 additions and 27 deletions

View File

@ -9,7 +9,7 @@ services:
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
ports:
- "${DB_PORT}:5432"
- "${DB_PORT}:${DB_PORT}"
pgadmin:
container_name: pgadmin4_container
image: dpage/pgadmin4

View File

@ -0,0 +1,15 @@
{
"npmName": "Ticket-API-Services",
"npmVersion": "0.0.0",
"providedIn": "root",
"withInterfaces": true,
"enumNameSuffix": "Enum",
"supportsES6": true,
"ngVersion": "17.0.0",
"modelSuffix": "Model",
"stringEnums": true,
"enumPropertyNaming": "PascalCase",
"modelPropertyNaming": "original",
"fileNaming": "camelCase",
"paramNaming": "camelCase"
}

48
backend/openapitools.json Normal file
View File

@ -0,0 +1,48 @@
{
"$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "7.6.0",
"generators": {
"typescript-angular": {
"generatorName": "typescript-angular",
"output": "web-api",
"inputSpec": "http://localhost:3000/api-json",
"additionalProperties": {
"npmName": "Ticket-API-Services",
"npmVersion": "0.0.0",
"ngVersion": "17.0.0",
"npmRepository": null,
"configurationPrefix": null,
"apiModulePrefix" : "TicketApi",
"providedIn": "any",
"fileNaming": "camelCase",
"paramNaming": "camelCase",
"enumPropertyNamingReplaceSpecialChar": "false",
"enumUnknownDefaultCase": "false",
"enumNameSuffix": "ApiEnum",
"enumPropertyNaming": "PascalCase",
"modelPropertyNaming": "original",
"modelSuffix": "ApiModel",
"modelFileSuffix": ".api.model",
"serviceSuffix": "ApiService",
"serviceFileSuffix": ".api.service",
"withInterfaces": true,
"supportsES6": true,
"stringEnums": true,
"sortParamsByRequiredFlag": true,
"sortModelPropertiesByRequiredFlag": true,
"useSingleRequestParameter": false,
"taggedUnions": false,
"snapshot": false,
"prependFormOrBodyParameters": false,
"nullSafeAdditionalProps": false,
"legacyDiscriminatorBehavior": true,
"ensureUniqueParams": true,
"disallowAdditionalPropertiesIfNotPresent": true,
"allowUnicodeIdentifiers": false
}
}
}
}
}

View File

@ -17,7 +17,8 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:e2e": "jest --config ./test/jest-e2e.json",
"build:api": "pnpm openapi-generator-cli generate"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
@ -28,6 +29,7 @@
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.3.1",
"@nestjs/typeorm": "^10.0.2",
"argon2": "^0.40.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"install": "^0.13.0",

View File

@ -29,6 +29,9 @@ dependencies:
'@nestjs/typeorm':
specifier: ^10.0.2
version: 10.0.2(@nestjs/common@10.3.7)(@nestjs/core@10.3.7)(reflect-metadata@0.2.2)(rxjs@7.8.1)(typeorm@0.3.20)
argon2:
specifier: ^0.40.1
version: 0.40.1
class-transformer:
specifier: ^0.5.1
version: 0.5.1
@ -1182,7 +1185,6 @@ packages:
/@phc/format@1.0.0:
resolution: {integrity: sha512-m7X9U6BG2+J+R1lSOdCiITLLrxm+cWlNI3HUFA92oLO77ObGNzaKdh8pMLqdZcshtkKuV84olNNXDfMc4FezBQ==}
engines: {node: '>=10'}
dev: true
/@pkgjs/parseargs@0.11.0:
resolution: {integrity: sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==}
@ -1823,7 +1825,6 @@ packages:
'@phc/format': 1.0.0
node-addon-api: 7.1.0
node-gyp-build: 4.8.1
dev: true
/argparse@1.0.10:
resolution: {integrity: sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==}
@ -4230,7 +4231,6 @@ packages:
/node-addon-api@7.1.0:
resolution: {integrity: sha512-mNcltoe1R8o7STTegSOHdnJNN7s5EUvhoS7ShnTHDyOSd+8H+UdWODq6qSv67PjC8Zc5JRT8+oLAMCr0SIXw7g==}
engines: {node: ^16 || ^18 || >= 20}
dev: true
/node-emoji@1.11.0:
resolution: {integrity: sha512-wo2DpQkQp7Sjm2A0cq+sN7EHKO6Sl0ctXeBdFZrL9T9+UywORbufTcTZxom8YqpLQt/FqNMUkOpkZrJVYSKD3A==}
@ -4252,7 +4252,6 @@ packages:
/node-gyp-build@4.8.1:
resolution: {integrity: sha512-OSs33Z9yWr148JZcbZd5WiAXhh/n9z8TxQcdMhIOlpN9AhWpLfvVFO73+m77bBABQMaY9XSvIa+qk0jlI7Gcaw==}
hasBin: true
dev: true
/node-int64@0.4.0:
resolution: {integrity: sha512-O5lz91xSOeoXP6DulyHfllpq+Eg00MWitZIbtPfoSEvqIHdl5gfcY6hYzDWnj0qD5tz52PI08u9qUvSVeUBeHw==}

View File

@ -24,7 +24,7 @@ import { CorsMiddleware } from './middleware/cors-middleware/cors.middlware';
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer
// TODO: Redirect via Reverse Proxy all HTTP requests to HTTPS
// TODO Redirect via Reverse Proxy all HTTP requests to HTTPS
.apply(
CspMiddleware,
SecurityHeadersMiddleware,

View File

@ -0,0 +1 @@
export * from './user-credentials.entity';

View File

@ -4,7 +4,11 @@ import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';
async function setupSwagger(app) {
const config = new DocumentBuilder().build();
const config = new DocumentBuilder()
.setTitle('Tickets API')
.setDescription('Description of the API')
.setVersion('0.0.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
}

View File

@ -7,39 +7,56 @@ import {
UseGuards,
} from '@nestjs/common';
import { AuthService } from '../services/auth.service';
import { UserCredentialsDto } from '../models/dto';
import { Tokens } from '../models/types';
import { TokensDto, UserCredentialsDto } from '../models/dto';
import { RefreshTokenGuard } from '../common/guards';
import { GetCurrentUser, GetCurrentUserId, Public } from '../common/decorators';
import { ApiCreatedResponse, ApiTags } from '@nestjs/swagger';
@ApiTags('Authentication')
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@ApiCreatedResponse({
description: 'User signed up successfully',
type: TokensDto,
})
@Public()
@Post('signup')
@HttpCode(HttpStatus.CREATED)
public async signup(
@Body() userCredentials: UserCredentialsDto
): Promise<Tokens> {
): Promise<TokensDto> {
return this.authService.signup(userCredentials);
}
@ApiCreatedResponse({
description: 'User signin successfully',
type: TokensDto,
})
@Public()
@Post('signin')
@HttpCode(HttpStatus.OK)
public async signin(
@Body() userCredentials: UserCredentialsDto
): Promise<Tokens> {
): Promise<TokensDto> {
return this.authService.signin(userCredentials);
}
@ApiCreatedResponse({
description: 'User signed out successfully',
type: Boolean,
})
@Post('logout')
@HttpCode(HttpStatus.OK)
public async logout(@GetCurrentUserId() userId: number): Promise<boolean> {
return this.authService.logout(userId);
}
@ApiCreatedResponse({
description: 'User tokens refreshed successfully',
type: TokensDto,
})
@Public()
@UseGuards(RefreshTokenGuard)
@Post('refresh')
@ -47,7 +64,7 @@ export class AuthController {
public async refresh(
@GetCurrentUserId() userId: number,
@GetCurrentUser('refresh_token') refresh_token: string
): Promise<Tokens> {
): Promise<TokensDto> {
return this.authService.refresh(userId, refresh_token);
}
}

View File

@ -1 +1,2 @@
export * from './user-credentials.dto';
export * from './tokens.dto';

View File

@ -0,0 +1,22 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
export class TokensDto {
@ApiProperty({
title: 'Access token',
description: 'Access token',
example: 'eyJhbGci',
})
@IsNotEmpty()
@IsString()
public access_token: string;
@ApiProperty({
title: 'Refresh token',
description: 'Refresh token',
example: 'eyJhbGci',
})
@IsNotEmpty()
@IsString()
public refresh_token: string;
}

View File

@ -1,10 +1,22 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator';
export class UserCredentialsDto {
@ApiProperty({
title: 'E-Mail',
description: 'User email',
example: 'foo@bar.com',
})
@IsNotEmpty()
@IsEmail()
public email: string;
@ApiProperty({
title: 'Password',
description: 'User password',
example: '$tr0ngP@$$w0rd',
minLength: 8,
})
@IsNotEmpty()
@IsString()
@MinLength(8)

View File

@ -1,3 +1,2 @@
export * from './tokens.type';
export * from './jwt-payload.type';
export * from './jwt-payload-with-refresh-token.type';

View File

@ -1,4 +0,0 @@
export type Tokens = {
access_token: string;
refresh_token: string;
};

View File

@ -1,6 +1,5 @@
import { ForbiddenException, Injectable } from '@nestjs/common';
import { UserCredentialsDto } from '../models/dto';
import { Tokens } from '../models/types';
import { TokensDto, UserCredentialsDto } from '../models/dto';
import { EncryptionService } from './encryption.service';
import { UserRepository } from '../repositories/user.repository';
import { TokenManagementService } from './token-management.service';
@ -13,7 +12,7 @@ export class AuthService {
private readonly encryptionService: EncryptionService
) {}
public async signup(userCredentials: UserCredentialsDto): Promise<Tokens> {
public async signup(userCredentials: UserCredentialsDto): Promise<TokensDto> {
const passwordHashed = await this.encryptionService.hashData(
userCredentials.password
);
@ -24,7 +23,7 @@ export class AuthService {
return this.generateAndPersistTokens(user.id, user.email);
}
public async signin(userCredentials: UserCredentialsDto): Promise<Tokens> {
public async signin(userCredentials: UserCredentialsDto): Promise<TokensDto> {
const user = await this.userRepository.findUserByEmail(
userCredentials.email
);
@ -43,7 +42,10 @@ export class AuthService {
return this.generateAndPersistTokens(user.id, user.email);
}
public async refresh(userId: number, refreshToken: string): Promise<Tokens> {
public async refresh(
userId: number,
refreshToken: string
): Promise<TokensDto> {
const user = await this.userRepository.findUserById(userId);
if (!user || !user.hashedRt) {
throw new ForbiddenException('Access Denied');
@ -71,7 +73,7 @@ export class AuthService {
private async generateAndPersistTokens(
userId: number,
email: string
): Promise<Tokens> {
): Promise<TokensDto> {
const tokens = await this.tokenManagementService.generateTokens(
userId,
email

View File

@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Tokens } from '../models/types';
import { JwtService } from '@nestjs/jwt';
import { TokensDto } from '../models/dto';
@Injectable()
export class TokenManagementService {
@ -24,7 +24,10 @@ export class TokenManagementService {
this.JWT_SECRET_RT = this.configService.get<string>('JWT_SECRET_RT');
}
public async generateTokens(userId: number, email: string): Promise<Tokens> {
public async generateTokens(
userId: number,
email: string
): Promise<TokensDto> {
const access_token: string = await this.createAccessToken(userId, email);
const refresh_token: string = await this.createRefreshToken(userId, email);
return { access_token, refresh_token };

View File

@ -1,6 +1,6 @@
import { ConfigService } from '@nestjs/config';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { UserCredentials } from '../../entities/user-credentials.entity';
import { UserCredentials } from 'src/entities';
export const databaseConfigFactory = (
configService: ConfigService

View File

@ -31,6 +31,7 @@
"@types/dompurify": "^3.0.5",
"crypto-js": "^4.2.0",
"dompurify": "^3.1.3",
"primeicons": "^7.0.0",
"primeng": "^17.11.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",

View File

@ -38,6 +38,9 @@ dependencies:
dompurify:
specifier: ^3.1.3
version: 3.1.3
primeicons:
specifier: ^7.0.0
version: 7.0.0
primeng:
specifier: ^17.11.0
version: 17.11.0(@angular/common@17.3.0)(@angular/core@17.3.0)(@angular/forms@17.3.0)(rxjs@7.8.1)(zone.js@0.14.4)
@ -8564,6 +8567,10 @@ packages:
react-is: 18.2.0
dev: true
/primeicons@7.0.0:
resolution: {integrity: sha512-jK3Et9UzwzTsd6tzl2RmwrVY/b8raJ3QZLzoDACj+oTJ0oX7L9Hy+XnVwgo4QVKlKpnP/Ur13SXV/pVh4LzaDw==}
dev: false
/primeng@17.11.0(@angular/common@17.3.0)(@angular/core@17.3.0)(@angular/forms@17.3.0)(rxjs@7.8.1)(zone.js@0.14.4):
resolution: {integrity: sha512-EtkXnE7I6gcrqjNDvUi4qvwTvwL/m3hwPJImoO/s088HDOBerwjN1c2Pq3bwGRG8Eg4Ga9nXdE/IrYj2mbAZLw==}
peerDependencies: