Письма подрядчикам: шаблон Артура, код письма, отправка через Gmail API от имени менеджера (делегирование домена)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 22:38:58 +03:00
parent 841d769fb2
commit 7237edadbe
4 changed files with 211 additions and 2 deletions

View File

@@ -20,7 +20,7 @@ export function parseServiceAccountKey(raw: unknown): ServiceAccountKey {
const b64 = (x: string) => Buffer.from(x).toString("base64url");
/** Провайдер access-токена по ключу сервисного аккаунта (JWT -> токен, с кэшем до истечения). */
export function createTokenProvider(key: ServiceAccountKey, opts: { scope?: string; fetchFn?: typeof fetch; now?: () => number } = {}): () => Promise<string> {
export function createTokenProvider(key: ServiceAccountKey, opts: { scope?: string; subject?: string; fetchFn?: typeof fetch; now?: () => number } = {}): () => Promise<string> {
const fetchFn = opts.fetchFn ?? fetch;
const now = opts.now ?? (() => Date.now());
let cached: { token: string; expiresAt: number } | null = null;
@@ -30,7 +30,7 @@ export function createTokenProvider(key: ServiceAccountKey, opts: { scope?: stri
const iat = Math.floor(now() / 1000);
const uri = key.token_uri ?? "https://oauth2.googleapis.com/token";
const head = b64(JSON.stringify({ alg: "RS256", typ: "JWT" }));
const claim = b64(JSON.stringify({ iss: key.client_email, scope: opts.scope ?? DRIVE_SCOPE, aud: uri, iat, exp: iat + 3000 }));
const claim = b64(JSON.stringify({ iss: key.client_email, scope: opts.scope ?? DRIVE_SCOPE, ...(opts.subject ? { sub: opts.subject } : {}), aud: uri, iat, exp: iat + 3000 }));
const sig = createSign("RSA-SHA256").update(`${head}.${claim}`).sign(key.private_key, "base64url");
const res = await fetchFn(uri, {
method: "POST",