Showing
9 changed files
with
54 additions
and
21 deletions
| 1 | 1 | version: '3.9' |
| 2 | 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 | 18 | restart: always |
| 19 | + container_name: base-pg-database | |
| 7 | 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 | 25 | ports: |
| 13 | - - '3306:3306' | |
| 14 | - env_file: | |
| 15 | - - .env | |
| 26 | + - '5432:5432' | ... | ... |
| ... | ... | @@ -4,7 +4,8 @@ |
| 4 | 4 | # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. |
| 5 | 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 | 10 | #TESTING DATABASE . CHANGE TO YOUR OWN DATA |
| 10 | 11 | DB_HOST=base-database |
| ... | ... | @@ -15,7 +16,8 @@ DB_USER_PASSWORD=basePassw |
| 15 | 16 | DB_PORT=3306 |
| 16 | 17 | DB_DRIVER=mysql |
| 17 | 18 | PRIVATE_API_URL=http://localhost:3000 |
| 18 | -API_KEY=80dc792592de35b9ac634a8a1f299cf7352e7230d1764b0a42e61b2f3422991d | |
| 19 | +CLIENT_API_KEY=45aad74a22b04b0bedeb467aa28e2c6a90a595c68618b2b557081adec152c1f2 | |
| 20 | +PRIVATE_API_KEY=80dc792592de35b9ac634a8a1f299cf7352e7230d1764b0a42e61b2f3422991d | |
| 19 | 21 | TOKEN_EXPIRATION="30d" |
| 20 | 22 | TOKEN_SECRET=85712985fdd06085b3032b6d9dba16436f742e4dc650cd66e50bb20373c22f5c |
| 21 | 23 | CLIENT_SECRET=3d0f23390356bbd818b037863376bad3c7b80da765bb20d9db80d1275f2afd88 | ... | ... |
| ... | ... | @@ -6,12 +6,14 @@ import { PrismaService } from 'src/config/prisma.service'; |
| 6 | 6 | import { AuthModule } from '../auth/auth.module'; |
| 7 | 7 | import { DescryptMiddleware } from './middlewares/decrypt.middleware'; |
| 8 | 8 | import { PublicModule } from '@app/public'; |
| 9 | +import { PrivateModule } from '@app/private'; | |
| 9 | 10 | import { ConfigModule } from '@nestjs/config'; |
| 10 | 11 | import { ThrottlerModule } from '@nestjs/throttler'; |
| 11 | 12 | |
| 12 | 13 | @Module({ |
| 13 | 14 | imports: [ |
| 14 | 15 | PublicModule, |
| 16 | + PrivateModule, | |
| 15 | 17 | UsersModule, |
| 16 | 18 | AuthModule, |
| 17 | 19 | ConfigModule.forRoot({ | ... | ... |
| ... | ... | @@ -2,7 +2,7 @@ import { Controller, Request, Post, UseGuards } from '@nestjs/common'; |
| 2 | 2 | import { AuthService } from './auth.service'; |
| 3 | 3 | import { AuthGuard } from '@nestjs/passport'; |
| 4 | 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 | 7 | @Controller('auth') |
| 8 | 8 | export class AuthController { | ... | ... |
| 1 | 1 | import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common'; |
| 2 | 2 | |
| 3 | 3 | @Injectable() |
| 4 | -export class ApiKeyGuard implements CanActivate { | |
| 4 | +export class PrivateApiKeyGuard implements CanActivate { | |
| 5 | 5 | async canActivate(context: ExecutionContext): Promise<boolean> { |
| 6 | 6 | const request = context.switchToHttp().getRequest(); |
| 7 | 7 | const apiKey = request.headers['x-api-key']; |
| ... | ... | @@ -10,7 +10,7 @@ export class ApiKeyGuard implements CanActivate { |
| 10 | 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 | 14 | throw new UnauthorizedException('Invalid API key'); |
| 15 | 15 | } |
| 16 | 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 | 2 | import { UsersService } from './users.service'; |
| 3 | 3 | import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; |
| 4 | 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 | 6 | import { MixAuthGuard } from '../auth/guards/auth.guard'; |
| 7 | 7 | |
| 8 | 8 | @Controller('users') |
| ... | ... | @@ -15,13 +15,13 @@ export class UsersController { |
| 15 | 15 | // return this.usersService.create({ data: createUserDto }); |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | - @UseGuards(ApiKeyGuard, new MixAuthGuard('private')) | |
| 18 | + @UseGuards(PrivateApiKeyGuard, new MixAuthGuard('private')) | |
| 19 | 19 | @Get(':id') |
| 20 | 20 | async findOne(@Param('id') id: string) { |
| 21 | 21 | return privateEncryptBody(await this.usersService.findOne(id)); |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - @UseGuards(ApiKeyGuard, JwtAuthGuard) | |
| 24 | + @UseGuards(PrivateApiKeyGuard, JwtAuthGuard) | |
| 25 | 25 | @Get() |
| 26 | 26 | async findAll() { |
| 27 | 27 | return privateEncryptBody(await this.usersService.findAll()); | ... | ... |
| ... | ... | @@ -20,8 +20,8 @@ |
| 20 | 20 | "paths": { |
| 21 | 21 | "@app/public": ["libs/public-api/src"], |
| 22 | 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