Showing
9 changed files
with
54 additions
and
21 deletions
| 1 | version: '3.9' | 1 | version: '3.9' |
| 2 | services: | 2 | services: |
| 3 | - mysql: | ||
| 4 | - image: mysql:8.0 | ||
| 5 | - container_name: base-database | 3 | + # mysql: |
| 4 | + # image: mysql:8.0 | ||
| 5 | + # container_name: base-database | ||
| 6 | + # restart: always | ||
| 7 | + # environment: | ||
| 8 | + # MYSQL_ROOT_PASSWORD: '${DB_ROOT_PASSWORD}' | ||
| 9 | + # MYSQL_DATABASE: '${DB_NAME}' | ||
| 10 | + # MYSQL_USER: '${DB_USER}' | ||
| 11 | + # MYSQL_PASSWORD: '${DB_USER_PASSWORD}' | ||
| 12 | + # ports: | ||
| 13 | + # - '3306:3306' | ||
| 14 | + # env_file: | ||
| 15 | + # - .env | ||
| 16 | + postgres: | ||
| 17 | + image: postgres | ||
| 6 | restart: always | 18 | restart: always |
| 19 | + container_name: base-pg-database | ||
| 7 | environment: | 20 | environment: |
| 8 | - MYSQL_ROOT_PASSWORD: '${DB_ROOT_PASSWORD}' | ||
| 9 | - MYSQL_DATABASE: '${DB_NAME}' | ||
| 10 | - MYSQL_USER: '${DB_USER}' | ||
| 11 | - MYSQL_PASSWORD: '${DB_USER_PASSWORD}' | 21 | + POSTGRES_USER: root |
| 22 | + POSTGRES_PASSWORD: toor | ||
| 23 | + volumes: | ||
| 24 | + - postgres:/var/lib/postgresql/data | ||
| 12 | ports: | 25 | ports: |
| 13 | - - '3306:3306' | ||
| 14 | - env_file: | ||
| 15 | - - .env | 26 | + - '5432:5432' |
| @@ -4,7 +4,8 @@ | @@ -4,7 +4,8 @@ | ||
| 4 | # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. | 4 | # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. |
| 5 | # See the documentation for all the connection string options: https://pris.ly/d/connection-strings | 5 | # See the documentation for all the connection string options: https://pris.ly/d/connection-strings |
| 6 | 6 | ||
| 7 | -DATABASE_URL="mysql://root:toor@localhost:3306/base-db" | 7 | +#DATABASE_URL="mysql://root:toor@localhost:3306/base-db" |
| 8 | +DATABASE_URL=postgresql://develop:_nRi.Xw7AGSh@postgrebd.ditopay.io:5432/ditopay?schema=public | ||
| 8 | 9 | ||
| 9 | #TESTING DATABASE . CHANGE TO YOUR OWN DATA | 10 | #TESTING DATABASE . CHANGE TO YOUR OWN DATA |
| 10 | DB_HOST=base-database | 11 | DB_HOST=base-database |
| @@ -15,7 +16,8 @@ DB_USER_PASSWORD=basePassw | @@ -15,7 +16,8 @@ DB_USER_PASSWORD=basePassw | ||
| 15 | DB_PORT=3306 | 16 | DB_PORT=3306 |
| 16 | DB_DRIVER=mysql | 17 | DB_DRIVER=mysql |
| 17 | PRIVATE_API_URL=http://localhost:3000 | 18 | PRIVATE_API_URL=http://localhost:3000 |
| 18 | -API_KEY=80dc792592de35b9ac634a8a1f299cf7352e7230d1764b0a42e61b2f3422991d | 19 | +CLIENT_API_KEY=45aad74a22b04b0bedeb467aa28e2c6a90a595c68618b2b557081adec152c1f2 |
| 20 | +PRIVATE_API_KEY=80dc792592de35b9ac634a8a1f299cf7352e7230d1764b0a42e61b2f3422991d | ||
| 19 | TOKEN_EXPIRATION="30d" | 21 | TOKEN_EXPIRATION="30d" |
| 20 | TOKEN_SECRET=85712985fdd06085b3032b6d9dba16436f742e4dc650cd66e50bb20373c22f5c | 22 | TOKEN_SECRET=85712985fdd06085b3032b6d9dba16436f742e4dc650cd66e50bb20373c22f5c |
| 21 | CLIENT_SECRET=3d0f23390356bbd818b037863376bad3c7b80da765bb20d9db80d1275f2afd88 | 23 | CLIENT_SECRET=3d0f23390356bbd818b037863376bad3c7b80da765bb20d9db80d1275f2afd88 |
| @@ -6,12 +6,14 @@ import { PrismaService } from 'src/config/prisma.service'; | @@ -6,12 +6,14 @@ import { PrismaService } from 'src/config/prisma.service'; | ||
| 6 | import { AuthModule } from '../auth/auth.module'; | 6 | import { AuthModule } from '../auth/auth.module'; |
| 7 | import { DescryptMiddleware } from './middlewares/decrypt.middleware'; | 7 | import { DescryptMiddleware } from './middlewares/decrypt.middleware'; |
| 8 | import { PublicModule } from '@app/public'; | 8 | import { PublicModule } from '@app/public'; |
| 9 | +import { PrivateModule } from '@app/private'; | ||
| 9 | import { ConfigModule } from '@nestjs/config'; | 10 | import { ConfigModule } from '@nestjs/config'; |
| 10 | import { ThrottlerModule } from '@nestjs/throttler'; | 11 | import { ThrottlerModule } from '@nestjs/throttler'; |
| 11 | 12 | ||
| 12 | @Module({ | 13 | @Module({ |
| 13 | imports: [ | 14 | imports: [ |
| 14 | PublicModule, | 15 | PublicModule, |
| 16 | + PrivateModule, | ||
| 15 | UsersModule, | 17 | UsersModule, |
| 16 | AuthModule, | 18 | AuthModule, |
| 17 | ConfigModule.forRoot({ | 19 | ConfigModule.forRoot({ |
| @@ -2,7 +2,7 @@ import { Controller, Request, Post, UseGuards } from '@nestjs/common'; | @@ -2,7 +2,7 @@ import { Controller, Request, Post, UseGuards } from '@nestjs/common'; | ||
| 2 | import { AuthService } from './auth.service'; | 2 | import { AuthService } from './auth.service'; |
| 3 | import { AuthGuard } from '@nestjs/passport'; | 3 | import { AuthGuard } from '@nestjs/passport'; |
| 4 | import { clientEncryptBody, privateEncryptBody } from 'src/helpers/encryptor.helper'; | 4 | import { clientEncryptBody, privateEncryptBody } from 'src/helpers/encryptor.helper'; |
| 5 | -import { ApiKeyGuard } from './guards/api-key.guard'; | 5 | +import { ApiKeyGuard } from './guards/public-api-key.guard'; |
| 6 | 6 | ||
| 7 | @Controller('auth') | 7 | @Controller('auth') |
| 8 | export class AuthController { | 8 | export class AuthController { |
| 1 | import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common'; | 1 | import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common'; |
| 2 | 2 | ||
| 3 | @Injectable() | 3 | @Injectable() |
| 4 | -export class ApiKeyGuard implements CanActivate { | 4 | +export class PrivateApiKeyGuard implements CanActivate { |
| 5 | async canActivate(context: ExecutionContext): Promise<boolean> { | 5 | async canActivate(context: ExecutionContext): Promise<boolean> { |
| 6 | const request = context.switchToHttp().getRequest(); | 6 | const request = context.switchToHttp().getRequest(); |
| 7 | const apiKey = request.headers['x-api-key']; | 7 | const apiKey = request.headers['x-api-key']; |
| @@ -10,7 +10,7 @@ export class ApiKeyGuard implements CanActivate { | @@ -10,7 +10,7 @@ export class ApiKeyGuard implements CanActivate { | ||
| 10 | throw new UnauthorizedException('No API key provided'); | 10 | throw new UnauthorizedException('No API key provided'); |
| 11 | } | 11 | } |
| 12 | 12 | ||
| 13 | - if (apiKey !== process.env.API_KEY) { | 13 | + if (apiKey !== process.env.PRIVATE_API_KEY) { |
| 14 | throw new UnauthorizedException('Invalid API key'); | 14 | throw new UnauthorizedException('Invalid API key'); |
| 15 | } | 15 | } |
| 16 | return true; | 16 | return true; |
| 1 | +import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common'; | ||
| 2 | + | ||
| 3 | +@Injectable() | ||
| 4 | +export class PublicApiKeyGuard implements CanActivate { | ||
| 5 | + async canActivate(context: ExecutionContext): Promise<boolean> { | ||
| 6 | + const request = context.switchToHttp().getRequest(); | ||
| 7 | + const apiKey = request.headers['x-api-key']; | ||
| 8 | + | ||
| 9 | + if (!apiKey) { | ||
| 10 | + throw new UnauthorizedException('No API key provided'); | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + if (apiKey !== process.env.CLIENT_API_KEY) { | ||
| 14 | + throw new UnauthorizedException('Invalid API key'); | ||
| 15 | + } | ||
| 16 | + return true; | ||
| 17 | + } | ||
| 18 | +} |
| @@ -2,7 +2,7 @@ import { Controller, Get, Post, Body, Param, UseGuards } from '@nestjs/common'; | @@ -2,7 +2,7 @@ import { Controller, Get, Post, Body, Param, UseGuards } from '@nestjs/common'; | ||
| 2 | import { UsersService } from './users.service'; | 2 | import { UsersService } from './users.service'; |
| 3 | import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; | 3 | import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; |
| 4 | import { privateEncryptBody } from 'src/helpers/encryptor.helper'; | 4 | import { privateEncryptBody } from 'src/helpers/encryptor.helper'; |
| 5 | -import { ApiKeyGuard } from '../auth/guards/api-key.guard'; | 5 | +import { PrivateApiKeyGuard } from '../auth/guards/private-api-key.guard'; |
| 6 | import { MixAuthGuard } from '../auth/guards/auth.guard'; | 6 | import { MixAuthGuard } from '../auth/guards/auth.guard'; |
| 7 | 7 | ||
| 8 | @Controller('users') | 8 | @Controller('users') |
| @@ -15,13 +15,13 @@ export class UsersController { | @@ -15,13 +15,13 @@ export class UsersController { | ||
| 15 | // return this.usersService.create({ data: createUserDto }); | 15 | // return this.usersService.create({ data: createUserDto }); |
| 16 | } | 16 | } |
| 17 | 17 | ||
| 18 | - @UseGuards(ApiKeyGuard, new MixAuthGuard('private')) | 18 | + @UseGuards(PrivateApiKeyGuard, new MixAuthGuard('private')) |
| 19 | @Get(':id') | 19 | @Get(':id') |
| 20 | async findOne(@Param('id') id: string) { | 20 | async findOne(@Param('id') id: string) { |
| 21 | return privateEncryptBody(await this.usersService.findOne(id)); | 21 | return privateEncryptBody(await this.usersService.findOne(id)); |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | - @UseGuards(ApiKeyGuard, JwtAuthGuard) | 24 | + @UseGuards(PrivateApiKeyGuard, JwtAuthGuard) |
| 25 | @Get() | 25 | @Get() |
| 26 | async findAll() { | 26 | async findAll() { |
| 27 | return privateEncryptBody(await this.usersService.findAll()); | 27 | return privateEncryptBody(await this.usersService.findAll()); |
| @@ -20,8 +20,8 @@ | @@ -20,8 +20,8 @@ | ||
| 20 | "paths": { | 20 | "paths": { |
| 21 | "@app/public": ["libs/public-api/src"], | 21 | "@app/public": ["libs/public-api/src"], |
| 22 | "@app/public/*": ["libs/public-api/src/*"], | 22 | "@app/public/*": ["libs/public-api/src/*"], |
| 23 | - "@app/private": ["libs/private/src"], | ||
| 24 | - "@app/private/*": ["libs/private/src/*"] | 23 | + "@app/private": ["libs/private-api/src"], |
| 24 | + "@app/private/*": ["libs/private-api/src/*"] | ||
| 25 | } | 25 | } |
| 26 | } | 26 | } |
| 27 | } | 27 | } |
Please
register
or
login
to post a comment