From 4246c7495efd69e2f1119ad1143313b726992754 Mon Sep 17 00:00:00 2001 From: Rafael Date: Tue, 20 Jan 2026 17:52:34 -0300 Subject: [PATCH] UPDATE: updating /me to get api key --- docsfera.json | 516 ++++++++++++++-------------- src/modules/auth/auth.controller.ts | 25 +- src/modules/auth/auth.module.ts | 3 +- 3 files changed, 275 insertions(+), 269 deletions(-) diff --git a/docsfera.json b/docsfera.json index 8942a26..7d13f03 100644 --- a/docsfera.json +++ b/docsfera.json @@ -715,6 +715,158 @@ ] } }, + "/api-key": { + "post": { + "operationId": "ApiKeyController_create", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateApiKeyDto" + } + } + } + }, + "responses": { + "201": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateApiKeyResponseDto" + } + } + } + }, + "default": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateApiKeyResponseDto" + } + } + } + } + }, + "tags": [ + "ApiKey" + ], + "security": [ + { + "access-token": [] + } + ] + }, + "get": { + "operationId": "ApiKeyController_findAll", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ApiKeyBaseResponseDto" + } + } + } + } + }, + "default": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ApiKeyBaseResponseDto" + } + } + } + } + } + }, + "tags": [ + "ApiKey" + ], + "security": [ + { + "access-token": [] + } + ] + } + }, + "/api-key/{id}": { + "delete": { + "operationId": "ApiKeyController_remove", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + }, + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "ApiKey" + ], + "security": [ + { + "access-token": [] + } + ] + } + }, "/connections": { "post": { "operationId": "ConnectionController_createConnection", @@ -6584,158 +6736,6 @@ ] } }, - "/api-key": { - "post": { - "operationId": "ApiKeyController_create", - "parameters": [ - { - "name": "dadosfera-lang", - "in": "header", - "required": false, - "schema": { - "enum": [ - "pt-br", - "en-us" - ], - "type": "string" - } - } - ], - "requestBody": { - "required": true, - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/CreateApiKeyDto" - } - } - } - }, - "responses": { - "201": { - "description": "", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/CreateApiKeyResponseDto" - } - } - } - }, - "default": { - "description": "", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/CreateApiKeyResponseDto" - } - } - } - } - }, - "tags": [ - "ApiKey" - ], - "security": [ - { - "access-token": [] - } - ] - }, - "get": { - "operationId": "ApiKeyController_findAll", - "parameters": [ - { - "name": "dadosfera-lang", - "in": "header", - "required": false, - "schema": { - "enum": [ - "pt-br", - "en-us" - ], - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "", - "content": { - "application/json": { - "schema": { - "type": "array", - "items": { - "$ref": "#/components/schemas/ApiKeyBaseResponseDto" - } - } - } - } - }, - "default": { - "description": "", - "content": { - "application/json": { - "schema": { - "type": "array", - "items": { - "$ref": "#/components/schemas/ApiKeyBaseResponseDto" - } - } - } - } - } - }, - "tags": [ - "ApiKey" - ], - "security": [ - { - "access-token": [] - } - ] - } - }, - "/api-key/{id}": { - "delete": { - "operationId": "ApiKeyController_remove", - "parameters": [ - { - "name": "dadosfera-lang", - "in": "header", - "required": false, - "schema": { - "enum": [ - "pt-br", - "en-us" - ], - "type": "string" - } - }, - { - "name": "id", - "required": true, - "in": "path", - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "" - } - }, - "tags": [ - "ApiKey" - ], - "security": [ - { - "access-token": [] - } - ] - } - }, "/identity-providers": { "post": { "operationId": "IdentityProviderController_addIdentityProvider", @@ -7851,6 +7851,104 @@ "accessToken" ] }, + "CreateApiKeyDto": { + "type": "object", + "properties": { + "permissions": { + "description": "Array of permission IDs", + "type": "array", + "items": { + "type": "number" + } + } + }, + "required": [ + "permissions" + ] + }, + "PermissionDto": { + "type": "object", + "properties": { + "id": { + "type": "number" + }, + "name": { + "type": "string" + } + }, + "required": [ + "id", + "name" + ] + }, + "CreateApiKeyResponseDto": { + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uuid" + }, + "key_mask": { + "type": "string" + }, + "permissions": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PermissionDto" + } + }, + "created_at": { + "type": "string", + "format": "date-time" + }, + "created_by": { + "type": "string" + }, + "key": { + "type": "string" + } + }, + "required": [ + "id", + "key_mask", + "permissions", + "created_at", + "created_by", + "key" + ] + }, + "ApiKeyBaseResponseDto": { + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uuid" + }, + "key_mask": { + "type": "string" + }, + "permissions": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PermissionDto" + } + }, + "created_at": { + "type": "string", + "format": "date-time" + }, + "created_by": { + "type": "string" + } + }, + "required": [ + "id", + "key_mask", + "permissions", + "created_at", + "created_by" + ] + }, "CreateConnectionDto": { "type": "object", "properties": { @@ -8074,37 +8172,6 @@ "message" ] }, - "PermissionDto": { - "type": "object", - "properties": { - "id": { - "type": "number" - }, - "seqid": { - "type": "number" - }, - "name": { - "type": "string" - }, - "claim": { - "type": "string" - }, - "createdAt": { - "type": "string" - }, - "updatedAt": { - "type": "string" - } - }, - "required": [ - "id", - "seqid", - "name", - "claim", - "createdAt", - "updatedAt" - ] - }, "Customer": { "type": "object", "properties": { @@ -10588,89 +10655,6 @@ "publicKey" ] }, - "CreateApiKeyDto": { - "type": "object", - "properties": { - "permissions": { - "description": "Array of permission IDs", - "type": "array", - "items": { - "type": "number" - } - } - }, - "required": [ - "permissions" - ] - }, - "CreateApiKeyResponseDto": { - "type": "object", - "properties": { - "id": { - "type": "string", - "format": "uuid" - }, - "key_mask": { - "type": "string" - }, - "permissions": { - "type": "array", - "items": { - "$ref": "#/components/schemas/PermissionDto" - } - }, - "created_at": { - "type": "string", - "format": "date-time" - }, - "created_by": { - "type": "string" - }, - "key": { - "type": "string" - } - }, - "required": [ - "id", - "key_mask", - "permissions", - "created_at", - "created_by", - "key" - ] - }, - "ApiKeyBaseResponseDto": { - "type": "object", - "properties": { - "id": { - "type": "string", - "format": "uuid" - }, - "key_mask": { - "type": "string" - }, - "permissions": { - "type": "array", - "items": { - "$ref": "#/components/schemas/PermissionDto" - } - }, - "created_at": { - "type": "string", - "format": "date-time" - }, - "created_by": { - "type": "string" - } - }, - "required": [ - "id", - "key_mask", - "permissions", - "created_at", - "created_by" - ] - }, "CreateIdentityProvider": { "type": "object", "properties": { diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 34dfd06..7c3e073 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -55,6 +55,7 @@ import jwt, { JwtPayload } from 'jsonwebtoken'; import { LanguageEnum } from 'src/utils/languages.enum'; import { Language } from 'src/decorators/language.decorator'; import { ApiInternalOnlyEndpoint } from 'src/decorators/swagger.decorator'; +import { ApiKeyService } from 'src/modules/api-key/api-key.service'; type CookiesValues = { accessToken?: string; @@ -74,6 +75,7 @@ export class AuthController { @Inject(DadosferaLogger) dadosferaLogger: DadosferaLogger, private authClient: AuthClientService, + private apiKeyService: ApiKeyService, ) { this.logger = dadosferaLogger.logger; @@ -477,10 +479,29 @@ export class AuthController { async getMe(@Req() req: Request, @Res() res: Response) { this.logger.info('GET /auth/me ') + // Check for API key header first + const apiKey = req.get('X-Api-key'); + if (apiKey) { + this.logger.info('Authenticating via X-Api-key header'); + const { api_key } = await this.apiKeyService.get(apiKey); + + const userDto = { + id: api_key.user_id, + name: api_key.username, + customer: { + id: api_key.customer_id, + name: api_key.customer_name, + tier: api_key.customer_tier, + } + }; + + return res.status(200).json(userDto); + } + // Get token and headers const accessToken = req.cookies['ddf-auth']; - const refreshToken = req.cookies['ddf-refresh-auth']; - const userId = req.cookies['ddf-user-id']; + const refreshToken = req.cookies['ddf-refresh-auth']; + const userId = req.cookies['ddf-user-id']; const resourceHost = req.headers["host"] const hasUserSession = Boolean(accessToken) && Boolean(userId); diff --git a/src/modules/auth/auth.module.ts b/src/modules/auth/auth.module.ts index 24e7623..47b59cc 100644 --- a/src/modules/auth/auth.module.ts +++ b/src/modules/auth/auth.module.ts @@ -8,10 +8,11 @@ import { AuthClientService } from './auth.service'; import { DucClient } from '../duc/client.config'; import { GoogleLoginStrategy } from './passport-strategies/google-strategy'; import { getOauthSecrets } from 'src/utils/OauthSecrets'; +import { ApiKeyModule } from '../api-key/api-key.module'; const client = new DucClient(); @Module({ - imports: [ClientsModule.register([client.providerOptions])], + imports: [ClientsModule.register([client.providerOptions]), ApiKeyModule], controllers: [AuthController], providers: [ AuthClientService,