/
nasya
/
SafeDrop
Обзор
Документация
Войти
/
nasya
/
SafeDrop
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
scripts/init-hydra-client.ts
55 строк
2 KB
nasya
Initial commit
24 май 2026, 16:12
24 май 2026, 16:12
16bc032
Код
Авторство
О чём код?
const hydraAdminUrl = process.env.HYDRA_ADMIN_URL ?? "http://hydra:4445"; const publicBaseUrl = process.env.PUBLIC_BASE_URL ?? "http://localhost"; const clientId = process.env.PUBLIC_HYDRA_CLIENT_ID ?? "safedrop-web"; const webPublicUrl = process.env.WEB_PUBLIC_URL ?? publicBaseUrl; const webDirectPublicUrl = process.env.WEB_DIRECT_PUBLIC_URL ?? "http://localhost:5173"; function unique(values: string[]): string[] { return [...new Set(values)]; } const clientPayload = { client_id: clientId, client_name: "SafeDrop Web", grant_types: ["authorization_code", "refresh_token"], post_logout_redirect_uris: unique([`${webPublicUrl}/`, `${webDirectPublicUrl}/`]), redirect_uris: unique([`${webPublicUrl}/callback`, `${webDirectPublicUrl}/callback`]), response_types: ["code"], scope: "openid offline_access", token_endpoint_auth_method: "none", }; async function hydra(path: string, init: RequestInit = {}) { return fetch(`${hydraAdminUrl}${path}`, { ...init, headers: { "content-type": "application/json", ...(init.headers ?? {}), }, }); } async function main() { const get = await hydra(`/admin/clients/${encodeURIComponent(clientId)}`); const exists = get.ok; const response = exists ? await hydra(`/admin/clients/${encodeURIComponent(clientId)}`, { body: JSON.stringify(clientPayload), method: "PUT", }) : await hydra("/admin/clients", { body: JSON.stringify(clientPayload), method: "POST", }); if (!response.ok) { const body = await response.text(); throw new Error(`Hydra client init failed: ${response.status} ${body}`); } const action = exists ? "updated" : "created"; console.info(`Hydra OAuth2 public client ${action}: ${clientId}`); } await main();