todo_service: запись назначенному в любом проекте и без проекта, агентские токены, тест не зависит от реальной даты

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-30 10:22:31 +03:00
parent 2fc2a534ff
commit 678cde985c
6 changed files with 83 additions and 21 deletions

View File

@@ -17,18 +17,33 @@ export class Auth {
private jwks: ReturnType<typeof createRemoteJWKSet> | null = null;
constructor(private readonly pool: Pool, private readonly s: TodoServiceSettings) {}
/** REST: только внутренний токен; person передаётся заголовком X-Caller-Person. */
async internal(authorization: string | undefined, callerPerson: string | undefined): Promise<Caller> {
const t = bearer(authorization);
if (!t || !safeEqual(t, this.s.http.token)) throw new AccessError("unauthorized", 401);
return callerPerson ? resolveCaller(this.pool, callerPerson) : ADMIN;
/** Токен -> права: административный (без заголовка - администратор) или агентский (всегда конкретный person). null - токен не наш. */
private async byServiceToken(t: string, callerPerson: string | undefined): Promise<Caller | null> {
if (safeEqual(t, this.s.http.token)) return callerPerson ? resolveCaller(this.pool, callerPerson) : ADMIN;
const at = this.s.http.agentTokens.find((x) => safeEqual(t, x.token));
if (!at) return null;
if (at.personId) {
if (callerPerson && callerPerson.toLowerCase() !== at.personId.toLowerCase()) throw new AccessError("X-Caller-Person не совпадает с person токена", 403);
return resolveCaller(this.pool, at.personId);
}
if (!callerPerson) throw new AccessError("агентскому токену нужен заголовок X-Caller-Person", 403);
return resolveCaller(this.pool, callerPerson);
}
/** MCP: внутренний токен, статический токен или JWT Keycloak; JWT без известного person отклоняется. */
/** REST: административный или агентский токен; person передаётся заголовком X-Caller-Person. */
async internal(authorization: string | undefined, callerPerson: string | undefined): Promise<Caller> {
const t = bearer(authorization);
const c = t ? await this.byServiceToken(t, callerPerson) : null;
if (!c) throw new AccessError("unauthorized", 401);
return c;
}
/** MCP: токен сервиса, статический токен или JWT Keycloak; JWT без известного person отклоняется. */
async mcp(authorization: string | undefined, callerPerson: string | undefined): Promise<Caller> {
const t = bearer(authorization);
if (!t) throw new AccessError("unauthorized", 401);
if (safeEqual(t, this.s.http.token)) return callerPerson ? resolveCaller(this.pool, callerPerson) : ADMIN;
const sc = await this.byServiceToken(t, callerPerson);
if (sc) return sc;
const st = this.s.mcp.staticTokens.find((x) => safeEqual(t, x.token));
if (st) return st.personId ? resolveCaller(this.pool, st.personId) : { ...ADMIN, name: st.name ?? "mcp-token" };
if (!this.s.mcp.issuer) throw new AccessError("unauthorized", 401);