fix(auth): route session lifecycle through Square

This commit is contained in:
2026-08-19 16:44:06 +08:00
parent 1907924203
commit dc776ff489
6 changed files with 80 additions and 63 deletions

View File

@@ -1,16 +0,0 @@
import {
NIANCODE_AUTH_CLIENT_ID,
NIANCODE_AUTH_GATEWAY_URL,
NIANCODE_AUTH_SCOPE,
} from '../../shared/auth-public';
export const NIANCODE_AUTH_CLIENT_SECRET = 'app';
export const NIANCODE_AUTH_PASSWORD_ENCODE_KEY = 'thanks,pig4cloud';
export const NIANCODE_AUTH_CONFIG = {
gatewayAuthUrl: NIANCODE_AUTH_GATEWAY_URL,
clientId: NIANCODE_AUTH_CLIENT_ID,
clientSecret: NIANCODE_AUTH_CLIENT_SECRET,
passwordEncodeKey: NIANCODE_AUTH_PASSWORD_ENCODE_KEY,
scope: NIANCODE_AUTH_SCOPE,
} as const;

View File

@@ -2,7 +2,6 @@ import type { IncomingMessage, ServerResponse } from 'http';
import type { HostApiContext } from '../context';
import { parseJsonBody, sendJson } from '../route-utils';
import { proxyAwareFetch } from '../../utils/proxy-fetch';
import { NIANCODE_AUTH_CONFIG } from '../auth-config';
import { WORKS_SQUARE_CONFIG } from '../works-config';
import {
clearWorksSquareSession,
@@ -92,14 +91,6 @@ function withoutRefreshToken(payload: unknown): unknown {
return publicPayload;
}
function normalizeAuthBase(): string {
const authBase = NIANCODE_AUTH_CONFIG.gatewayAuthUrl.replace(/\/+$/, '');
if (!/^https?:\/\//i.test(authBase)) {
throw new Error('authBase must start with http:// or https://');
}
return authBase;
}
function normalizeWorksBase(value = WORKS_SQUARE_CONFIG.apiBaseUrl): string {
const apiBase = value.replace(/\/+$/, '');
if (!/^https?:\/\//i.test(apiBase)) {
@@ -652,7 +643,6 @@ async function handleLogout(
await parseJsonBody<LogoutInput>(req),
['accessToken'],
);
const authBase = normalizeAuthBase();
const rendererAccessToken = readOptionalTrimmedString(body.accessToken);
const accessToken = getWorksSquareSessionSnapshot()?.accessToken
?? readRequiredString(rendererAccessToken, 'accessToken');
@@ -674,8 +664,8 @@ async function handleLogout(
let response: Response;
try {
response = await proxyAwareFetch(`${authBase}/token/logout`, {
method: 'DELETE',
response = await proxyAwareFetch(createWorksUrl('/api/auth/logout').toString(), {
method: 'POST',
headers: {
Authorization: `Bearer ${accessToken}`,
},