Merge pull request #3754 from SillyTavern/fix-openrouter-oauth

OpenRouter: Fix OAuth flow with enabled accounts
This commit is contained in:
Cohee
2025-03-25 23:20:42 +02:00
committed by GitHub
2 changed files with 18 additions and 4 deletions

View File

@ -189,14 +189,17 @@ export async function findSecret(key) {
} }
function authorizeOpenRouter() { function authorizeOpenRouter() {
const openRouterUrl = `https://openrouter.ai/auth?callback_url=${encodeURIComponent(location.origin)}`; const redirectUrl = new URL('/callback/openrouter', window.location.origin);
const openRouterUrl = `https://openrouter.ai/auth?callback_url=${encodeURIComponent(redirectUrl.toString())}`;
location.href = openRouterUrl; location.href = openRouterUrl;
} }
async function checkOpenRouterAuth() { async function checkOpenRouterAuth() {
const params = new URLSearchParams(location.search); const params = new URLSearchParams(location.search);
if (params.has('code')) { const source = params.get('source');
const code = params.get('code'); if (source === 'openrouter') {
const query = new URLSearchParams(params.get('query'));
const code = query.get('code');
try { try {
const response = await fetch('https://openrouter.ai/api/v1/auth/keys', { const response = await fetch('https://openrouter.ai/api/v1/auth/keys', {
method: 'POST', method: 'POST',

View File

@ -150,7 +150,7 @@ if (cliArgs.enableCorsProxy) {
app.use(cookieSession({ app.use(cookieSession({
name: getCookieSessionName(), name: getCookieSessionName(),
sameSite: 'strict', sameSite: 'lax',
httpOnly: true, httpOnly: true,
maxAge: getSessionCookieAge(), maxAge: getSessionCookieAge(),
secret: getCookieSecret(globalThis.DATA_ROOT), secret: getCookieSecret(globalThis.DATA_ROOT),
@ -213,6 +213,17 @@ app.get('/', getCacheBusterMiddleware(), (request, response) => {
return response.sendFile('index.html', { root: path.join(process.cwd(), 'public') }); return response.sendFile('index.html', { root: path.join(process.cwd(), 'public') });
}); });
// Callback endpoint for OAuth PKCE flows (e.g. OpenRouter)
app.get('/callback/:source?', (request, response) => {
const source = request.params.source;
const query = request.url.split('?')[1];
const searchParams = new URLSearchParams();
source && searchParams.set('source', source);
query && searchParams.set('query', query);
const path = `/?${searchParams.toString()}`;
return response.redirect(307, path);
});
// Host login page // Host login page
app.get('/login', loginPageMiddleware); app.get('/login', loginPageMiddleware);