mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-18 05:25:40 +09:00
refactor(api): drop cookie support for sudo mode (#2466)
This commit is contained in:
1 parent
ab68b61653
commit
12718eabbc
8 files changed
+1
-84
No files matched your search
@@ -428,7 +428,6 @@ export function buildAPIConfigFromMaster(master: MasterConfig): APIConfig {
|
||||
},
|
||||
bluesky: master.auth.bluesky as BlueskyOAuthConfig,
|
||||
},
|
||||
cookie: master.cookie,
|
||||
klipy: {
|
||||
apiKey: master.integrations.klipy.api_key,
|
||||
},
|
||||
|
||||
@@ -11,7 +11,6 @@ import * as AuthPassword from '../../auth/AuthPassword';
|
||||
import {SUDO_MODE_HEADER} from '../../middleware/SudoModeMiddleware';
|
||||
import type {User} from '../../models/User';
|
||||
import type {HonoEnv} from '../../types/HonoEnv';
|
||||
import {setSudoCookie} from '../../utils/SudoCookieUtils';
|
||||
import {getSudoModeService} from './SudoModeService';
|
||||
|
||||
export interface SudoVerificationBody {
|
||||
@@ -127,10 +126,6 @@ function setSudoTokenHeader(
|
||||
const tokenToSet = result.sudoToken ?? ctx.req.header(SUDO_MODE_HEADER);
|
||||
if (tokenToSet) {
|
||||
ctx.header(SUDO_MODE_HEADER, tokenToSet);
|
||||
const user = ctx.get('user');
|
||||
if (user) {
|
||||
setSudoCookie(ctx, tokenToSet, user.id.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -281,10 +281,6 @@ export interface APIConfig {
|
||||
};
|
||||
bluesky: BlueskyOAuthConfig;
|
||||
};
|
||||
cookie: {
|
||||
domain: string;
|
||||
secure: boolean;
|
||||
};
|
||||
klipy: {
|
||||
apiKey?: string;
|
||||
};
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
import {createMiddleware} from 'hono/factory';
|
||||
import {getSudoModeService} from '../auth/services/SudoModeService';
|
||||
import type {HonoEnv} from '../types/HonoEnv';
|
||||
import {getSudoCookie} from '../utils/SudoCookieUtils';
|
||||
|
||||
export const SUDO_MODE_HEADER = 'X-Fluxer-Sudo-Mode-JWT';
|
||||
export const SudoModeMiddleware = createMiddleware<HonoEnv>(async (ctx, next) => {
|
||||
@@ -19,11 +18,7 @@ export const SudoModeMiddleware = createMiddleware<HonoEnv>(async (ctx, next) =>
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
const sudoToken = ctx.req.header(SUDO_MODE_HEADER);
|
||||
let tokenToVerify: string | undefined = sudoToken;
|
||||
if (!tokenToVerify) {
|
||||
tokenToVerify = getSudoCookie(ctx, user.id.toString());
|
||||
}
|
||||
const tokenToVerify = ctx.req.header(SUDO_MODE_HEADER);
|
||||
if (!tokenToVerify) {
|
||||
await next();
|
||||
return;
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import type {Context} from 'hono';
|
||||
import {getCookie, setCookie} from 'hono/cookie';
|
||||
import {seconds} from 'itty-time';
|
||||
import {Config} from '../Config';
|
||||
import type {HonoEnv} from '../types/HonoEnv';
|
||||
|
||||
const SUDO_COOKIE_PREFIX = '__flx_sudo';
|
||||
const SUDO_COOKIE_MAX_AGE = seconds('5 minutes');
|
||||
|
||||
function getCookieDomain(): string {
|
||||
const domain = Config.cookie.domain;
|
||||
if (domain) {
|
||||
return domain;
|
||||
}
|
||||
try {
|
||||
const url = new URL(Config.endpoints.webApp);
|
||||
const hostname = url.hostname;
|
||||
const parts = hostname.split('.');
|
||||
if (parts.length >= 2) {
|
||||
return `.${parts.slice(-2).join('.')}`;
|
||||
} else {
|
||||
return hostname;
|
||||
}
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function getSudoCookieOptions() {
|
||||
return {
|
||||
httpOnly: true,
|
||||
secure: Config.cookie.secure,
|
||||
sameSite: 'Strict' as const,
|
||||
domain: getCookieDomain(),
|
||||
path: '/',
|
||||
maxAge: SUDO_COOKIE_MAX_AGE,
|
||||
};
|
||||
}
|
||||
|
||||
function sudoCookieName(userId?: string | number): string {
|
||||
if (userId === undefined || userId === null) {
|
||||
return SUDO_COOKIE_PREFIX;
|
||||
}
|
||||
return `${SUDO_COOKIE_PREFIX}_${userId}`;
|
||||
}
|
||||
|
||||
export function setSudoCookie(ctx: Context<HonoEnv>, token: string, userId?: string | number): void {
|
||||
const cookieName = sudoCookieName(userId);
|
||||
const options = getSudoCookieOptions();
|
||||
setCookie(ctx, cookieName, token, options);
|
||||
}
|
||||
|
||||
export function getSudoCookie(ctx: Context<HonoEnv>, userId?: string | number): string | undefined {
|
||||
const cookieName = sudoCookieName(userId);
|
||||
return getCookie(ctx, cookieName);
|
||||
}
|
||||
Reference in new issue
Block a user