mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Merge pull request #3754 from SillyTavern/fix-openrouter-oauth
OpenRouter: Fix OAuth flow with enabled accounts
This commit is contained in:
@ -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',
|
||||||
|
13
server.js
13
server.js
@ -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);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user