From 30be46bbd3da2798c6068105f10908a2a85116dd Mon Sep 17 00:00:00 2001 From: Rafael Lopes Date: Mon, 12 Jan 2026 17:53:58 -0300 Subject: [PATCH] =?UTF-8?q?WIP:=20Criado=20conex=C3=A3o=20com=20API=20do?= =?UTF-8?q?=20GLPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/infra/api/glpi.auth.js | 54 +++++++++++++++++++++++++++++++++ src/infra/api/glpi.client.js | 21 +++++++++++++ src/infra/api/glpi.config.js | 17 +++++++++++ src/infra/api/hubsoft.config.js | 14 --------- 4 files changed, 92 insertions(+), 14 deletions(-) create mode 100644 src/infra/api/glpi.auth.js create mode 100644 src/infra/api/glpi.client.js create mode 100644 src/infra/api/glpi.config.js diff --git a/src/infra/api/glpi.auth.js b/src/infra/api/glpi.auth.js new file mode 100644 index 0000000..3589680 --- /dev/null +++ b/src/infra/api/glpi.auth.js @@ -0,0 +1,54 @@ +// src/infra/api/glpi.auth.js + +const axios = require('axios') +const qs = require('qs') +const { glpi } = require('./glpi.config') +const { logError } = require('../../shared/utils/logger') + +let cachedToken = null +let tokenExpiresAt = null + +const getAuthToken = async () => { + try { + // reaproveita token válido + if (cachedToken && tokenExpiresAt && Date.now() < tokenExpiresAt) { + return cachedToken + } + + const payload = { + grant_type: 'password', + client_id: glpi.clientId, + client_secret: glpi.clientSecret, + username: glpi.username, + password: glpi.password, + scope: 'api' + } + + const response = await axios.post( + glpi.authUrl, + qs.stringify(payload), + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } + ) + + cachedToken = response.data.access_token + + const expiresIn = response.data.expires_in || 3600 + tokenExpiresAt = Date.now() + (expiresIn - 60) * 1000 + + return cachedToken + } catch (error) { + logError('Erro ao obter token de autenticação GLPI', { + message: error.message, + response: error.response?.data + }) + throw error + } +} + +module.exports = { + getAuthToken +} diff --git a/src/infra/api/glpi.client.js b/src/infra/api/glpi.client.js new file mode 100644 index 0000000..3571206 --- /dev/null +++ b/src/infra/api/glpi.client.js @@ -0,0 +1,21 @@ +// src/shared/infra/api/glpi.client.js + +const { getAuthToken } = require('./glpi.auth') + +async function glpiRequest({ method, url, data, params }) { + const token = await getAuthToken() + + return axios({ + method, + url: `${glpi.baseUrl}${url}`, + headers: { + Authorization: `Bearer ${token}`, + Accept: 'application/json' + }, + data, + params + }) +} +module.exports = { + glpiRequest +} \ No newline at end of file diff --git a/src/infra/api/glpi.config.js b/src/infra/api/glpi.config.js new file mode 100644 index 0000000..8ea2ed4 --- /dev/null +++ b/src/infra/api/glpi.config.js @@ -0,0 +1,17 @@ +// src/shared/infra/api/glpi.config.js +module.exports = { + glpi: { + baseUrl: process.env.GLPI_BASE_URL, + + authUrl: process.env.GLPI_AUTH_URL, + + authPayload: { + grant_type: 'password', + client_id: process.env.GLPI_CLIENT_ID, + client_secret: process.env.GLPI_CLIENT_SECRET, + username: process.env.GLPI_USERNAME, + password: process.env.GLPI_PASSWORD, + scope: 'api' + } + } +}; \ No newline at end of file diff --git a/src/infra/api/hubsoft.config.js b/src/infra/api/hubsoft.config.js index ff919c7..da02d1e 100644 --- a/src/infra/api/hubsoft.config.js +++ b/src/infra/api/hubsoft.config.js @@ -1,18 +1,4 @@ // src/shared/infra/api/hubsoft.config.js - -var hubsoft = { - baseUrl: process.env.HUBSOFT_BASE_URL, - authUrl: process.env.HUBSOFT_AUTH_URL, - authPayload: { - grant_type: 'password', - client_id: process.env.HUBSOFT_CLIENT_ID, - client_secret: process.env.HUBSOFT_CLIENT_SECRET, - username: process.env.HUBSOFT_USERNAME, - password: process.env.HUBSOFT_PASSWORD - }, - - -}; module.exports = { hubsoft: { baseUrl: process.env.HUBSOFT_BASE_URL,