diff --git a/api.http b/api.http index 7d67368..f1eaaf9 100644 --- a/api.http +++ b/api.http @@ -1,266 +1,72 @@ -// InsertRegions +GET http://192.168.1.96:3006 -POST http://localhost:3000/oracle/InsertRegions +### + +GET http://localhost:3006 + +### + +POST http://localhost:3006/sendmail/b@gmail.com + +### + +GET http://localhost:3006/GetUserDetails/d@gmail.com + +### + +GET http://localhost:3006/GetUserDetail/d@gmail.com + +### + +GET http://localhost:3006/GetSP + +### + +GET http://localhost:3006/checkroles + +### + +POST http://localhost:3006/login-client Content-Type: application/json { - "p_region":"p_region", - "p_name":"p_name" + "P_EMAILADDR":"a@gmail.com", + "P_PASSWORD":"A1!bcdef" } ### -//UpdateRegion +POST http://localhost:3006/logout-client -PATCH http://localhost:3000/oracle/UpdateRegion +### + +POST http://localhost:3006/register Content-Type: application/json +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImJAZ21haWwuY29tIiwiaWF0IjoxNzUwMTM3NTc4LCJleHAiOjE3NTAxNDExNzh9.ooNhATybtRY_HORWBH0vBLzPODxOWZMi3Q_cvS7kAho { - "p_regionID": , - "p_name":"p_name" + "email":"b@gmail.com", + "password":"B1!bcdef" } ### -// GetRegions - -GET http://localhost:3000/oracle/GetRegions +GET http://localhost:3006/getrole/ca ### +PUT http://localhost:3006/forgot-password -//InsertNewServiceProvider -POST http://localhost:3000/oracle/InsertNewServiceProvider -Content-Type: application/json - -{ - "p_name": "Service Provider Name", - "p_lookupcode": "SP001", - "p_address1": "123 Main St", - "p_address2": "Suite 100", - "p_city": "Anytown", - "p_state": "CA", - "p_zip": "90210", - "p_country": "USA", - "p_issuingregion": "Region A", - "p_replacementregion": "Region B", - "p_bondsurety": "Surety Co.", - "p_cargopolicyno": "CP123456", - "p_cargosurety": "Cargo Surety Co.", - "p_user_id": "user123" -} ### - -//UpdateServiceProvider - -PUT http://localhost:3000/oracle/UpdateServiceProvider -Content-Type: application/json - -{ - "p_spid": 1, - "p_name": "Service Provider Name", - "p_lookupcode": "SP001", - "p_address1": "123 Main St", - "p_address2": "Suite 100", - "p_city": "Anytown", - "p_state": "CA", - "p_zip": "90210", - "p_country": "USA", - "p_issuingregion": "Region A", - "p_replacementregion": "Region B", - "p_bondsurety": "Surety Co.", - "p_cargopolicyno": "CP123456", - "p_cargosurety": "Cargo Surety Co.", - "p_user_id": "user123" -} +POST http://localhost:3006/logout-user ### -// GetAllServiceproviders - -GET http://localhost:3000/oracle/GetAllServiceproviders +GET http://localhost:3006/ref-tok ### -//GetSelectedServiceprovider - -GET http://localhost:3000/oracle/GetSelectedServiceprovider/12 - -### - -//InsertSPContacts - -POST http://localhost:3000/oracle/InsertSPContacts -Content-Type: application/json - -{ - "p_spid": 12345, - "p_defcontactflag":"N", - "p_firstname": "John", - "p_lastname": "Doe", - "p_title": "Mr.", - "p_phoneno": "123-456-7890", - "p_mobileno": "098-765-4321", - "p_faxno": "123-456-7891", - "p_emailaddress": "john.doe@example.com", - "p_user_id": "jdoe" -} - -### - -//SetSPDefaultcontact - -POST http://localhost:3000/oracle/SetSPDefaultcontact/12 - -### - -//UpdateSPContacts - -PUT http://localhost:3000/oracle/UpdateSPContacts -Content-Type: application/json - -{ - "p_spcontactid": 12345, - "p_firstname": "Jane", - "p_lastname": "Doe", - "p_title": "Ms.", - "p_phoneno": "123-456-7890", - "p_mobileno": "098-765-4321", - "p_faxno": "123-456-7891", - "p_emailaddress": "jane.doe@example.com", - "p_user_id": "jdoe" -} - -### - - -//InactivateSPContact - -POST http://localhost:3000/oracle/InactivateSPContact/12 - -### - -//GetSPDefaultcontact - -GET http://localhost:3000/oracle/GetSPDefaultcontact/12 - -### - -//GetAllSPcontacts - -//GET http://localhost:3000/oracle/GetAllSPcontacts - -### - -//CreateCarnetSequence - -POST http://localhost:3000/oracle/CreateCarnetSequence -Content-Type: application/json - -{ - "p_spid": 2, - "p_regionid": 81, - "p_startnumber": 2000, - "p_endnumber": 3000, - "p_carnettype": "original" -} - -### - -//GetCarnetSequence - -GET http://localhost:3000/oracle/GetCarnetSequence/12 - -### - -//GetParamValues - -GET http://localhost:3000/oracle/GetParamValues?id=500&type=original - -### - - -//CreateTableRecord - -POST http://localhost:3000/oracle/CreateTableRecord -Content-Type: application/json - -{ - "P_USERID": "6", - "P_TABLEFULLDESC": "TypeA desc" -} - -### - -//CreateParamRecord - -POST http://localhost:3000/oracle/CreateParamRecord -Content-Type: application/json - -{ - # "P_SPID": 4, - "P_PARAMTYPE": "TypeA", - "P_PARAMDESC": "Description of the parameter", - "P_PARAMVALUE": "Value of the parameter", - # "P_ADDLPARAMVALUE1": "Additional Value 1", - # "P_ADDLPARAMVALUE2": "Additional Value 2", - # "P_ADDLPARAMVALUE3": null, - # "P_ADDLPARAMVALUE4": "Additional Value 4", - # "P_ADDLPARAMVALUE5": null, - "P_SORTSEQ": 1, - "P_USERID": "12" -} - -### - -// UpdateParamRecord - -PATCH http://localhost:3000/oracle/UpdateParamRecord -Content-Type: application/json - -{ - "P_SPID": 2, - "P_PARAMID": 1, - "P_PARAMDESC": "abc", - "P_ADDLPARAMVALUE1": "asd", - "P_ADDLPARAMVALUE2": "ad", - "P_ADDLPARAMVALUE3": null, - "P_ADDLPARAMVALUE4": "asd", - "P_ADDLPARAMVALUE5": null, - "P_SORTSEQ": 1, - "P_USERID": "12" -} - -### - -// InActivateParamRecord - -PATCH http://localhost:3000/oracle/InActivateParamRecord?pid=1&uid=2 -Content-Type: application/json - -{ - "P_PARAMID":2, - "P_USERID":"12" -} - -### - -// ReActivateParamRecord - -PATCH http://localhost:3000/oracle/ReActivateParamRecord?pid=1&uid=2 -Content-Type: application/json - -{ - "P_PARAMID":2, - "P_USERID":"12" -} - -### - - - - - +GET http://localhost:3006/ref diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 7c5d0df..0b46033 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -1,14 +1,16 @@ -import { Body, Controller, Get, HttpCode, Post, Put, Req, Res } from '@nestjs/common'; +import { Body, Controller, Get, HttpCode, Post, Put, Req, Res, UseInterceptors } from '@nestjs/common'; import { AuthService } from './auth.service'; import { ApiTags } from '@nestjs/swagger'; import { AuthLoginDTO } from './auth.dto'; import { Request, Response } from 'express'; +import { LogoutInterceptor } from 'src/interceptors/logout.interceptor'; +@ApiTags('Auth') @Controller() export class AuthController { constructor(private readonly authService: AuthService) { } - @ApiTags('Auth') + @Post('/login') login(@Body() body: AuthLoginDTO) { return this.authService.login(body); @@ -46,7 +48,7 @@ export class AuthController { return this.authService.registerUser(body, req); } - // @UseInterceptors(LogoutInterceptor) + @UseInterceptors(LogoutInterceptor) @HttpCode(200) @Post('logout-client') async logout(@Req() req: any, @Res() res: Response) { diff --git a/src/guards/roles.guard.ts b/src/guards/roles.guard.ts new file mode 100644 index 0000000..602ad1c --- /dev/null +++ b/src/guards/roles.guard.ts @@ -0,0 +1,46 @@ +// roles.guard.ts +import { + CanActivate, + ExecutionContext, + Injectable, + ForbiddenException, +} from '@nestjs/common'; +import { Reflector } from '@nestjs/core'; +import { ROLES_KEY } from '../decorators/roles.decorator'; +import * as jwt from 'jsonwebtoken'; +import { UnauthorizedException } from 'src/exceptions/unauthorized.exception'; + +@Injectable() +export class RolesGuard implements CanActivate { + constructor(private reflector: Reflector) { } + + canActivate(context: ExecutionContext): boolean { + const requiredRoles = this.reflector.getAllAndOverride(ROLES_KEY, [ + context.getHandler(), + context.getClass(), + ]); + if (!requiredRoles || requiredRoles.length === 0) return true; + + const request = context.switchToHttp().getRequest(); + const token = request.cookies?.access_token; // Token is stored as HttpOnly cookie + + if (!token) { + throw new UnauthorizedException('Authentication Failed'); + } + + try { + const decoded: any = jwt.decode(token); + + const roles: string[] = decoded?.resource_access?.['app-be']?.roles || []; + const hasRole = requiredRoles.some(role => roles.includes(role)); + + if (!hasRole) { + throw new ForbiddenException('Unauthorized : Please Request Access for this Resource'); + } + + return true; + } catch (err) { + throw new UnauthorizedException('Authentication Failed'); + } + } +} diff --git a/src/oracle/uscib-managed-sp/region/region.controller.ts b/src/oracle/uscib-managed-sp/region/region.controller.ts index 7f1a37e..930c765 100644 --- a/src/oracle/uscib-managed-sp/region/region.controller.ts +++ b/src/oracle/uscib-managed-sp/region/region.controller.ts @@ -1,13 +1,16 @@ import { RegionService } from './region.service'; -import { Get, Post, Body, Controller, Patch } from '@nestjs/common'; +import { Get, Post, Body, Controller, Patch, UseGuards } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import { Roles } from 'src/decorators/roles.decorator'; import { InsertRegionsDto, UpdateRegionDto } from 'src/dto/property.dto'; +import { JwtAuthGuard } from 'src/guards/jwt-auth.guard'; +import { RolesGuard } from 'src/guards/roles.guard'; @ApiTags('Regions - Oracle') +@UseGuards(JwtAuthGuard, RolesGuard) @Roles('sa', 'ua') @Controller('oracle') export class RegionController {