diff --git a/package.json b/package.json index d2c7ed4..32a66bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ipwhitelist", - "version": "0.5.147", + "version": "0.5.148", "description": "IP WhiteList microservice for cloud provider", "main": "server.js", "scripts": { diff --git a/v2/server.js b/v2/server.js index 6d25198..48e38af 100644 --- a/v2/server.js +++ b/v2/server.js @@ -90,31 +90,6 @@ function createV2Router() { } }); - // ── GET /v2/impersonation-check — IAM impersonation: все GET эндпоинты ─ - router.get('/impersonation-check', async (req, res) => { - const token = req.session && req.session.token; - if (!token) return res.send('

Нет токена

Войти

'); - - const endpoints = [ - { name: 'status', url: 'https://auth-api.ngcloud.ru/api/v1/impersonation/status' }, - { name: 'history', url: 'https://auth-api.ngcloud.ru/api/v1/impersonation/history' }, - { name: 'history/all', url: 'https://auth-api.ngcloud.ru/api/v1/impersonation/history/all' }, - ]; - - const results = {}; - for (const ep of endpoints) { - try { - const data = await iamFetch(token, ep.url); - results[ep.name] = { ok: true, data }; - } catch (e) { - results[ep.name] = { ok: false, error: e.message }; - } - } - - res.set('Content-Type', 'text/html; charset=utf-8'); - res.send(iamStatPage(results)); - }); - // ── /v2/app — пользовательский CRUD (только с сессией) ────────────── router.use('/app', enhanceImpersonation, resolveContext, createUserRouter()); @@ -187,63 +162,4 @@ function debugPage(u, version) { `; } -function iamStatPage(results) { - function fmt(data, depth) { - if (data === null || data === undefined) return 'null'; - if (typeof data === 'string') return '"' + data.replace(/'; - if (typeof data === 'number' || typeof data === 'boolean') return '' + data + ''; - if (Array.isArray(data)) { - if (data.length === 0) return '[]'; - let html = '[
'; - for (const item of data) html += '
' + fmt(item, depth + 1) + ',
'; - html += '
]'; - return html; - } - if (typeof data === 'object') { - let html = '{
'; - for (const [k, v] of Object.entries(data)) { - html += '
"' + k + '": ' + fmt(v, depth + 1) + '
'; - } - html += '
}'; - return html; - } - return String(data).replace(/'; - if (r.ok) { - html += '
'; - html += fmt(r.data, 0); - html += '
'; - } else { - html += '
❌ ' + r.error.replace(/'; - } - html += '
'; - } - - html += ''; - return html; -} - module.exports = { createV2Router }; diff --git a/v2/src/user/index.js b/v2/src/user/index.js index 1c6e345..9ae3390 100644 --- a/v2/src/user/index.js +++ b/v2/src/user/index.js @@ -15,6 +15,7 @@ // ═══════════════════════════════════════════════════════════════════════════════ const express = require('express'); +const https = require('https'); const crud = require('../crud'); const q = require('../db/queries'); const config = require('../config'); @@ -22,8 +23,28 @@ const config = require('../config'); function createUserRouter() { const router = express.Router(); - // ── GET / — список записей + форма ────────────────────────────────── + // ── GET /?impersonation-check — IAM impersonation: все GET эндпоинты ── router.get('/', async (req, res) => { + if (req.query['impersonation-check'] !== undefined) { + const token = req.session && req.session.token; + if (!token) return res.send('

Нет токена

Войти

'); + const endpoints = [ + { name: 'status', url: 'https://auth-api.ngcloud.ru/api/v1/impersonation/status' }, + { name: 'history', url: 'https://auth-api.ngcloud.ru/api/v1/impersonation/history' }, + { name: 'history/all', url: 'https://auth-api.ngcloud.ru/api/v1/impersonation/history/all' }, + ]; + const results = {}; + for (const ep of endpoints) { + try { + results[ep.name] = { ok: true, data: await iamFetch(token, ep.url) }; + } catch (e) { + results[ep.name] = { ok: false, error: e.message }; + } + } + res.set('Content-Type', 'text/html; charset=utf-8'); + return res.send(iamStatPage(results)); + } + try { const isAdmin = req.canAdmin; let clId = req.clientId; @@ -137,4 +158,81 @@ function createUserRouter() { return router; } +// ── Хелпер: HTTP-запрос к IAM ──────────────────────────────────────────────── +async function iamFetch(token, url) { + const u = new URL(url); + return new Promise((resolve, reject) => { + https.get({ + hostname: u.hostname, port: 443, path: u.pathname, + headers: { Authorization: 'Bearer ' + token, Accept: 'application/json' }, + }, res => { + let data = ''; + res.on('data', c => data += c); + res.on('end', () => { + if (res.statusCode !== 200) return reject(new Error(res.statusCode + ': ' + data.slice(0, 200))); + try { resolve(JSON.parse(data)); } catch (e) { reject(e); } + }); + }).on('error', reject); + }); +} + +function iamStatPage(results) { + function fmt(data, depth) { + if (data === null || data === undefined) return 'null'; + if (typeof data === 'string') return '"' + data.replace(/'; + if (typeof data === 'number' || typeof data === 'boolean') return '' + data + ''; + if (Array.isArray(data)) { + if (data.length === 0) return '[]'; + let html = '[
'; + for (const item of data) html += '
' + fmt(item, depth + 1) + ',
'; + html += '
]'; + return html; + } + if (typeof data === 'object') { + let html = '{
'; + for (const [k, v] of Object.entries(data)) { + html += '
"' + k + '": ' + fmt(v, depth + 1) + '
'; + } + html += '
}'; + return html; + } + return String(data).replace(/'; + if (r.ok) { + html += '
'; + html += fmt(r.data, 0); + html += '
'; + } else { + html += '
❌ ' + r.error.replace(/'; + } + html += '
'; + } + + html += ''; + return html; +} + module.exports = { createUserRouter };