Отдельный префикс /api/v1 со своей авторификацией по API-ключам,
чтобы внешние системы могли забирать и менять данные, не получая
доступа к админке.
Что добавлено:
- таблица api_keys (db/init.sql, db/migration.sql, ensureApiKeysTable)
- CRUD ключей: GET/POST /api/api-keys, PUT/DELETE /:id, POST /:id/rotate
- requireApiKey: X-Api-Key или Authorization: Bearer, только для /api/v1/*
- 21 эндпоинт /api/v1: branches, groups, students, modules, entries,
lesson-reports, stats, me; списки в формате {items,total,limit,offset}
- страница управления ключами public/apikeys.html + пункт в меню
Безопасность:
- в БД только sha256(ключ) и префикс, секрет отдаётся один раз
- скоупы read/write: без write мутации дают 403
- branch_ids ключа сужают права и понижают роль до tutor
- per-key rate limit на cache.rateLimitStore, подбор ключей -> бан IP
- аудит мутаций с меткой via_api_key
- ключи не входят в бэкап и удаляются при restore
Проверено: api-keys.selftest.js (45 проверок), api.smoketest.js без
регрессий, работа без Redis через in-memory fallback.
140 lines
6.7 KiB
HTML
140 lines
6.7 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="ru">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>API-ключи — Админ-панель</title>
|
|
<link rel="stylesheet" href="admin.css">
|
|
</head>
|
|
<body data-page="apikeys">
|
|
<div class="layout">
|
|
<div class="sidebar" id="sidebar"></div>
|
|
<div class="main">
|
|
<div class="page-head">
|
|
<h2>API-ключи</h2>
|
|
<p class="page-sub">Доступ к данным из внешних систем по HTTP API</p>
|
|
</div>
|
|
|
|
<div class="actions-row" style="margin-bottom:16px">
|
|
<button class="btn-primary" id="addKeyBtn" type="button">
|
|
<span style="font-size:1.2rem">+</span> Создать ключ
|
|
</button>
|
|
</div>
|
|
|
|
<div class="audit-wrap">
|
|
<table class="audit-table" id="keysTable">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Название</th>
|
|
<th>Ключ</th>
|
|
<th>Права</th>
|
|
<th>Филиалы</th>
|
|
<th>Создан</th>
|
|
<th>Использован</th>
|
|
<th>Статус</th>
|
|
<th></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody></tbody>
|
|
</table>
|
|
<div class="empty" id="keysEmpty" style="display:none">API-ключи не созданы</div>
|
|
</div>
|
|
|
|
<div class="settings-card" style="margin-top:16px">
|
|
<div class="card-head">
|
|
<i data-lucide="terminal"></i>
|
|
<h3>Как использовать</h3>
|
|
</div>
|
|
<p class="hint">Базовый адрес: <code>/api/v1</code>. Ключ передаётся в заголовке <code>X-Api-Key</code> или <code>Authorization: Bearer <ключ></code>.</p>
|
|
<pre class="code-block">curl -H "X-Api-Key: wsk_ВАШ_КЛЮЧ" https://ВАШ_ДОМЕН/api/v1/groups
|
|
|
|
curl -H "X-Api-Key: wsk_ВАШ_КЛЮЧ" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"student_name":"Иван","group_id":1,"description":"Сделал проект"}' \
|
|
https://ВАШ_ДОМЕН/api/v1/entries</pre>
|
|
<p class="hint">Секрет показывается один раз при создании и ротации — в базе хранится только SHA-256 хеш. Эндпоинты: <code>/me</code>, <code>/branches</code>, <code>/groups</code>, <code>/students</code>, <code>/modules</code>, <code>/entries</code>, <code>/lesson-reports</code>, <code>/stats</code>.</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="modal-overlay" id="keyModal">
|
|
<div class="edit-modal" style="max-width:520px">
|
|
<h3 id="keyModalTitle">Новый API-ключ</h3>
|
|
<div class="settings-stack" style="gap:12px;max-width:none">
|
|
<div class="settings-field">
|
|
<label>Название <span style="color:#ef4444">*</span></label>
|
|
<input type="text" id="kName" class="settings-input" placeholder="Интеграция с 1С" autocomplete="off">
|
|
</div>
|
|
<div class="settings-field">
|
|
<label>Права доступа</label>
|
|
<div style="display:flex;gap:16px;flex-wrap:wrap">
|
|
<label style="display:flex;align-items:center;gap:6px;font-weight:400">
|
|
<input type="checkbox" id="kRead" checked> Чтение
|
|
</label>
|
|
<label style="display:flex;align-items:center;gap:6px;font-weight:400">
|
|
<input type="checkbox" id="kWrite"> Запись
|
|
</label>
|
|
</div>
|
|
<span class="hint" style="font-size:.75rem">Без права «Запись» ключ сможет только читать данные.</span>
|
|
</div>
|
|
<div class="settings-field">
|
|
<label>Филиалы (мультивыбор)</label>
|
|
<select id="kBranches" class="settings-input" multiple size="4"></select>
|
|
<span class="hint" style="font-size:.75rem">Пусто — доступ ко всем вашим филиалам. Выбор: Ctrl/Cmd+клик.</span>
|
|
</div>
|
|
<div class="settings-row">
|
|
<div class="settings-field" style="flex:1">
|
|
<label>Лимит запросов в минуту</label>
|
|
<input type="number" id="kRate" class="settings-input" min="1" max="10000" placeholder="120">
|
|
</div>
|
|
<div class="settings-field" style="flex:1">
|
|
<label>Действует до</label>
|
|
<input type="date" id="kExpires" class="settings-input">
|
|
</div>
|
|
</div>
|
|
<div class="card-foot" style="justify-content:flex-end;border-top:none;padding-top:0">
|
|
<button type="button" class="btn-primary ghost" id="kCancel">Отмена</button>
|
|
<button type="button" class="btn-primary" id="kSave">Создать</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="modal-overlay" id="secretModal">
|
|
<div class="edit-modal" style="max-width:560px">
|
|
<h3>Ключ создан</h3>
|
|
<p class="hint">Скопируйте ключ сейчас — второй раз он не показывается.</p>
|
|
<div class="settings-field">
|
|
<label>Ключ</label>
|
|
<input type="text" id="secretValue" class="settings-input mono" readonly>
|
|
</div>
|
|
<div class="card-foot" style="justify-content:flex-end;border-top:none;padding-top:0">
|
|
<button type="button" class="btn-primary ghost" id="secretCopy">Скопировать</button>
|
|
<button type="button" class="btn-primary" id="secretClose">Готово</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="toast" id="toast"></div>
|
|
<script src="vendor/lucide.min.js"></script>
|
|
<script src="js/datetime.js"></script>
|
|
<script src="admin.js"></script>
|
|
<script src="js/apikeys.js"></script>
|
|
<style>
|
|
.status-ok{font-size:.72rem;font-weight:600;color:#16a34a;background:rgba(22,163,74,.12);border-radius:999px;padding:2px 10px;white-space:nowrap}
|
|
.status-off{font-size:.72rem;font-weight:600;color:#ef4444;background:rgba(239,68,68,.12);border-radius:999px;padding:2px 10px;white-space:nowrap}
|
|
.status-warn{font-size:.72rem;font-weight:600;color:#d97706;background:rgba(217,119,6,.12);border-radius:999px;padding:2px 10px;white-space:nowrap}
|
|
.row-btn{background:none;border:none;color:var(--muted);cursor:pointer;font-size:1.05rem;padding:4px 8px;border-radius:6px}
|
|
.row-btn:hover{background:var(--bg);color:var(--accent)}
|
|
.row-btn.del:hover{color:#ef4444;background:rgba(239,68,68,.1)}
|
|
.key-prefix{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:.78rem;color:var(--muted)}
|
|
.key-scopes{font-size:.72rem;color:var(--muted)}
|
|
.key-meta{font-size:.74rem;color:var(--muted);white-space:nowrap}
|
|
.code-block{background:var(--bg);border:1px solid var(--border);border-radius:8px;padding:12px;overflow-x:auto;font-size:.78rem;line-height:1.5;margin:8px 0}
|
|
.mono{font-family:ui-monospace,SFMono-Regular,Menlo,monospace}
|
|
</style>
|
|
</body>
|
|
</html>
|
|
</div>
|
|
</div> |