changing auth middleware approach

This commit is contained in:
rodrigo.zamboni
2022-01-31 16:08:17 -03:00
parent 63c9c8195b
commit 24e8c732b8
9 changed files with 79 additions and 241 deletions
+9 -71
View File
@@ -1,89 +1,27 @@
import jwt from 'jsonwebtoken';
// import {decode,verify} from 'jsonwebtoken'
import jwkToPem from 'jwk-to-pem';
import { Request, Response, NextFunction } from 'express';
import axios from 'axios';
import { NestMiddleware } from '@nestjs/common';
// let pems: { [key: string]: {} }[];
export class LoggerMiddleware implements NestMiddleware{
setUp = async (region: string, id: string):Promise<{ [key: string]: {} }[]> => {
const URL = `https://cognito-idp.${region}.amazonaws.com/${id}/.well-known/jwks.json`;
try {
const response = await axios.get(URL);
if (response.status !== 200) {
throw new Error('Deu Ruim');
}
const data = await response.data;
const { keys } = data;
return keys.map((key: any) => {
const modulus = key.n;
const exponent = key.e;
const keyType = key.kty;
const jwk = { kty: keyType, n: modulus, e: exponent };
const pem = jwkToPem(jwk);
const keyId = key.kid;
return { [keyId]: pem };
});
// console.log('got PEMS');
} catch (error) {
// console.log(error);
// console.log('Error! Unable to download JWKs');
}
};
use = async (
request: Request,
response: Response,
next: NextFunction
) => {
const awsRegion = process.env.AWS_REGION
? process.env.AWS_REGION
: 'undefined';
const awsPoolId = process.env.AWS_IDENTITY_POOL_ID
? process.env.AWS_IDENTITY_POOL_ID
: 'undefined';
try {
const pems = await this.setUp(awsRegion, awsPoolId);
const userToken = request.headers.authorization?.split(' ')[1];
if (userToken) {
const user: any = jwt.decode(userToken, { complete: true });
if (user === null) {
response.status(401).end();
return;
}
const { kid } = user.header;
const pem = pems.filter((item: any) => item[kid]);
const pemValue: any = pem[0][kid];
if (!pem) {
response.status(401).end();
return;
}
jwt.verify(userToken, pemValue, (err: any) => {
if (err) {
response.status(401).json(err);
return;
}
next();
});
const verify_response = await axios.post(
`${process.env.DEV_URL}/user/verify`,
{ "token":request.get('Authorization')}
).then(response =>{
if(response.data.message == "Success"){
next()
}
})
} catch (error) {
response.status(401).send({ message: 'Authentication Failed!' });
}
@@ -1,8 +1,7 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, Put } from '@nestjs/common';
import { Controller, Get, Post, Body, Patch, Param, Delete, Put, Headers} from '@nestjs/common';
import { RestEnrichmentsService } from './rest-enrichments.service';
import { CreateRestEnrichmentDto } from './dto/create-rest-enrichment.dto';
import { UpdateRestEnrichmentDto } from './dto/update-rest-enrichment.dto';
import { AuthenticateController } from 'src/utils/Authenticate';
import axios from 'axios';
import { ApiTags } from '@nestjs/swagger';
@@ -12,17 +11,14 @@ export class RestEnrichmentsController {
constructor(private readonly restEnrichmentsService: RestEnrichmentsService) {}
@Post()
async create(@Body() body: CreateRestEnrichmentDto) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async create(@Body() body: CreateRestEnrichmentDto,@Headers() headers) {
const connectionResponse = await axios.post(
`${process.env.DEV_URL}/enrichment`,
body,
{
headers:{
"Authorization":`Token ${accessToken}`,
"Authorization":headers.authorization,
"Content-Type":"application/json"
}
}
@@ -32,15 +28,13 @@ export class RestEnrichmentsController {
}
@Get()
async list() {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async list(@Headers() headers) {
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/enrichment`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -49,16 +43,13 @@ export class RestEnrichmentsController {
}
@Get(':id')
async show(@Param('id') id: string) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async show(@Param('id') id: string,@Headers() headers) {
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/enrichment/${id}`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -67,17 +58,14 @@ export class RestEnrichmentsController {
}
@Put(':id')
async update(@Param('id') id: string, @Body() body: UpdateRestEnrichmentDto) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async update(@Param('id') id: string,@Headers() headers, @Body() body: UpdateRestEnrichmentDto) {
const connectionResponse = await axios.put(
`${process.env.DEV_URL}/enrichment/${id}`,
body,
{
headers:{
"Authorization":`Token ${accessToken}`,
"Authorization":headers.authorization,
"Content-Type":"application/json"
}
}
@@ -87,16 +75,13 @@ export class RestEnrichmentsController {
}
@Delete(':id')
async remove(@Param('id') id: string) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async remove(@Param('id') id: string,@Headers() headers) {
const connectionResponse = await axios.delete(
`${process.env.DEV_URL}/enrichment/${id}`,
{
headers:{
"Authorization":`Token ${accessToken}`,
"Authorization":headers.authorization,
"Content-Type":"application/json"
}
}
@@ -1,8 +1,5 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { Controller, Get, Param, Headers } from '@nestjs/common';
import { RestFactoryService } from './rest-factory.service';
import { CreateRestFactoryDto } from './dto/create-rest-factory.dto';
import { UpdateRestFactoryDto } from './dto/update-rest-factory.dto';
import { AuthenticateController } from 'src/utils/Authenticate';
import axios from 'axios';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
@@ -14,16 +11,13 @@ export class RestFactoryController {
@Get('/start')
async startDagFactory() {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async startDagFactory(@Headers() headers) {
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/factory/start`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -33,16 +27,13 @@ export class RestFactoryController {
@Get('/pipeline_params')
async sendJSONSettingsToDagFactory() {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async sendJSONSettingsToDagFactory(@Headers() headers) {
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/factory/pipeline_params`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -51,16 +42,13 @@ export class RestFactoryController {
}
@Get('/start/:pipelineId')
async startDag(@Param('pipelineId') pipelineId:string) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async startDag(@Param('pipelineId') pipelineId:string,@Headers() headers) {
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/factory/start/${pipelineId}`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
+12 -32
View File
@@ -2,7 +2,6 @@ import axios from 'axios';
require("dotenv").config();
import { RestInputsService } from './rest-inputs.service';
import { Controller, Get, Post, Body, Patch, Param, Delete, Put, Headers } from '@nestjs/common';
import { AuthenticateController } from 'src/utils/Authenticate';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { CreateRestInputDto } from './dto/create-rest-input.dto';
import { UpdateRestInputDto } from './dto/update-rest-input.dto';
@@ -15,24 +14,16 @@ import { testConnectionDTO } from './dto/testConnection.dto';
export class RestInputsController {
constructor(private readonly restInputsService: RestInputsService) {}
@Get('/test-auth/')
async testauth(){
return "Auth Success"
}
@Post('/test-connection/')
async testConnection(@Body() body:testConnectionDTO){
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async testConnection(@Body() body:testConnectionDTO,@Headers() headers){
const connectionResponse = await axios.post(
`${process.env.DEV_URL}/input/test-connection`,
body,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -42,17 +33,14 @@ export class RestInputsController {
}
@Post()
async create(@Body() body: CreateRestInputDto){
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async create(@Body() body: CreateRestInputDto,@Headers() headers){
const connectionResponse = await axios.post(
`${process.env.DEV_URL}/input`,
body,
{
headers:{
"Authorization":`Token ${accessToken}`,
"Authorization":headers.authorization,
"Content-Type":"application/json"
}
}
@@ -63,16 +51,13 @@ export class RestInputsController {
}
@Get()
async list(){
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async list(@Headers() headers){
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/input`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -82,15 +67,13 @@ export class RestInputsController {
}
@Get('/:id')
async show(@Param() id: string){
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async show(@Param() id: string,@Headers() headers){
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/input/:${id}`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -103,16 +86,15 @@ export class RestInputsController {
async update(
@Body() body: UpdateRestInputDto,
@Param() id: string,
@Headers() headers
){
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
const connectionResponse = await axios.put(
`${process.env.DEV_URL}/input/:${id}`,
body,
{
headers:{
"Authorization":`Token ${accessToken}`,
"Authorization":headers.authorization,
"Content-Type":"application/json"
}
}
@@ -123,15 +105,13 @@ export class RestInputsController {
}
@Delete(':id')
async delete(@Param() id: string){
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async delete(@Param() id: string,@Headers() headers){
const connectionResponse = await axios.delete(
`${process.env.DEV_URL}/input/:${id}`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -1,8 +1,7 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, Put } from '@nestjs/common';
import { Controller, Get, Post, Body, Headers, Param, Delete, Put } from '@nestjs/common';
import { RestOutputsService } from './rest-outputs.service';
import { CreateRestOutputDto } from './dto/create-rest-output.dto';
import { UpdateRestOutputDto } from './dto/update-rest-output.dto';
import { AuthenticateController } from 'src/utils/Authenticate';
import axios from 'axios';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
@@ -13,17 +12,14 @@ export class RestOutputsController {
constructor(private readonly restOutputsService: RestOutputsService) {}
@Post()
async create(@Body() body: CreateRestOutputDto) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async create(@Body() body: CreateRestOutputDto,@Headers() headers) {
const connectionResponse = await axios.post(
`${process.env.DEV_URL}/output`,
body,
{
headers:{
"Authorization":`Token ${accessToken}`,
"Authorization":headers.authorization,
"Content-Type":"application/json"
}
}
@@ -33,15 +29,13 @@ export class RestOutputsController {
}
@Get()
async list() {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async list(@Headers() headers) {
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/output`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -50,16 +44,13 @@ export class RestOutputsController {
}
@Get(':id')
async show(@Param('id') id: string) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async show(@Param('id') id: string,@Headers() headers) {
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/output/${id}`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -68,17 +59,14 @@ export class RestOutputsController {
}
@Put(':id')
async update(@Param('id') id: string, @Body() body: UpdateRestOutputDto) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async update(@Param('id') id: string, @Body() body: UpdateRestOutputDto,@Headers() headers) {
const connectionResponse = await axios.put(
`${process.env.DEV_URL}/output/${id}`,
body,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -87,16 +75,13 @@ export class RestOutputsController {
}
@Delete(':id')
async remove(@Param('id') id: string) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async remove(@Param('id') id: string,@Headers() headers) {
const connectionResponse = await axios.delete(
`${process.env.DEV_URL}/output/${id}`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -1,8 +1,7 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, Put } from '@nestjs/common';
import { Controller, Get, Post, Body, Headers, Param, Delete, Put } from '@nestjs/common';
import { RestPipelinesService } from './rest-pipelines.service';
import { CreateRestPipelineDto } from './dto/create-rest-pipeline.dto';
import { UpdateRestPipelineDto } from './dto/update-rest-pipeline.dto';
import { AuthenticateController } from 'src/utils/Authenticate';
import axios from 'axios';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
@@ -13,17 +12,14 @@ export class RestPipelinesController {
constructor(private readonly restPipelinesService: RestPipelinesService) {}
@Post()
async create(@Body() body: CreateRestPipelineDto) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async create(@Body() body: CreateRestPipelineDto,@Headers() headers) {
const connectionResponse = await axios.post(
`${process.env.DEV_URL}/pipeline`,
body,
{
headers:{
"Authorization":`Token ${accessToken}`,
"Authorization":headers.authorization,
"Content-Type":"application/json"
}
}
@@ -33,15 +29,13 @@ export class RestPipelinesController {
}
@Get()
async list() {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async list(@Headers() headers) {
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/pipeline`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -50,16 +44,13 @@ export class RestPipelinesController {
}
@Get(':id/logs/details')
async showLogs(@Param('id') id: string) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async showLogs(@Param('id') id: string,@Headers() headers) {
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/pipeline/${id}/logs/details`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -68,16 +59,13 @@ export class RestPipelinesController {
}
@Get(':id/status')
async showStatus(@Param('id') id: string) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async showStatus(@Param('id') id: string,@Headers() headers) {
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/pipeline/${id}/status`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -86,16 +74,13 @@ export class RestPipelinesController {
}
@Get(':id')
async show(@Param('id') id: string) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async show(@Param('id') id: string,@Headers() headers) {
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/pipeline/${id}`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -104,17 +89,14 @@ export class RestPipelinesController {
}
@Put(':id')
async update(@Param('id') id: string, @Body() body: UpdateRestPipelineDto) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async update(@Param('id') id: string, @Headers() headers, @Body() body: UpdateRestPipelineDto) {
const connectionResponse = await axios.put(
`${process.env.DEV_URL}/pipeline/${id}`,
body,
{
headers:{
"Authorization":`Token ${accessToken}`,
"Authorization":headers.authorization,
"Content-Type":"application/json"
}
}
@@ -124,16 +106,13 @@ export class RestPipelinesController {
}
@Delete(':id')
async remove(@Param('id') id: string) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async remove(@Param('id') id: string,@Headers() headers) {
const connectionResponse = await axios.delete(
`${process.env.DEV_URL}/pipeline/${id}`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -1,8 +1,7 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, Put } from '@nestjs/common';
import { Controller, Get, Post, Body, Headers, Param, Delete, Put } from '@nestjs/common';
import { RestTransformationsService } from './rest-transformations.service';
import { CreateRestTransformationDto } from './dto/create-rest-transformation.dto';
import { UpdateRestTransformationDto } from './dto/update-rest-transformation.dto';
import { AuthenticateController } from 'src/utils/Authenticate';
import axios from 'axios';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
@@ -13,17 +12,14 @@ export class RestTransformationsController {
constructor(private readonly restTransformationsService: RestTransformationsService) {}
@Post()
async create(@Body() body: CreateRestTransformationDto) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async create(@Body() body: CreateRestTransformationDto,@Headers() headers) {
const connectionResponse = await axios.post(
`${process.env.DEV_URL}/transformation`,
body,
{
headers:{
"Authorization":`Token ${accessToken}`,
"Authorization":headers.authorization,
"Content-Type":"application/json"
}
}
@@ -34,16 +30,13 @@ export class RestTransformationsController {
}
@Get()
async list() {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async list(@Headers() headers) {
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/transformation`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -53,16 +46,13 @@ export class RestTransformationsController {
}
@Get(':id')
async show(@Param('id') id: string) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async show(@Param('id') id: string,@Headers() headers) {
const connectionResponse = await axios.get(
`${process.env.DEV_URL}/transformation/${id}`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -72,17 +62,14 @@ export class RestTransformationsController {
}
@Put(':id')
async update(@Param('id') id: string, @Body() body: UpdateRestTransformationDto) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async update(@Param('id') id: string, @Headers() headers, @Body() body: UpdateRestTransformationDto) {
const connectionResponse = await axios.put(
`${process.env.DEV_URL}/transformation/${id}`,
body,
{
headers:{
"Authorization":`Token ${accessToken}`,
"Authorization":headers.authorization,
"Content-Type":"application/json"
}
}
@@ -93,16 +80,13 @@ export class RestTransformationsController {
}
@Delete(':id')
async remove(@Param('id') id: string) {
const authController = new AuthenticateController()
const accessToken = await authController.authenticate()
async remove(@Param('id') id: string,@Headers() headers) {
const connectionResponse = await axios.delete(
`${process.env.DEV_URL}/transformation/${id}`,
{
headers:{
"Authorization":`Token ${accessToken}`
"Authorization":headers.authorization
}
}
)
@@ -2,7 +2,6 @@ import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/commo
import { RestUserService } from './rest-user.service';
import { CreateRestUserDto } from './dto/create-rest-user.dto';
import { UpdateRestUserDto } from './dto/update-rest-user.dto';
import { AuthenticateController } from 'src/utils/Authenticate';
import axios from 'axios';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { UserAuthDTO } from './dto/userAuth.dto';
+1 -1
View File
File diff suppressed because one or more lines are too long