Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(oidc): let OAuth apps read discovery, JWKS and userinfo (#19753)

Arvin Xua54bddbcc635
diff --git a/src/libs/next/proxy/define-config.test.ts b/src/libs/next/proxy/define-config.test.ts
index 386ac2ed338..9bedabe371e 100644
--- a/src/libs/next/proxy/define-config.test.ts
+++ b/src/libs/next/proxy/define-config.test.ts
@@ -132,3 +132,30 @@ describe('Acceptance installation guide', () => {
expect(guide).toContain('.agents/skills/acceptance/SKILL.md');
});
});
+
+describe('OIDC protocol endpoints', () => {
+ const callMiddleware = async (path: string) => {
+ const { auth } = await import('@/auth');
+ vi.mocked(auth.api.getSession).mockClear();
+ const response = await middleware(new NextRequest(`http://localhost:3010${path}`));
+ return { getSession: vi.mocked(auth.api.getSession), response };
+ };
+
+ it.each(['/oidc/.well-known/openid-configuration', '/oidc/jwks', '/oidc/me'])(
+ 'serves %s without sending an unauthenticated client to sign-in',
+ async (path) => {
+ const { getSession, response } = await callMiddleware(path);
+
+ expect(getSession).not.toHaveBeenCalled();
+ expect(response?.headers.get('x-middleware-next')).toBe('1');
+ expect(response?.headers.get('location')).toBeNull();
+ expect(response?.headers.get('x-middleware-rewrite')).toBeNull();
+ },
+ );
+
+ it('still gates the authorization endpoint on a session', async () => {
+ const { getSession } = await callMiddleware('/oidc/auth?client_id=lca_1&response_type=code');
+
+ expect(getSession).toHaveBeenCalled();
+ });
+});
diff --git a/src/libs/next/proxy/define-config.ts b/src/libs/next/proxy/define-config.ts
index 4aa0ad3f943..9bf5413a055 100644
--- a/src/libs/next/proxy/define-config.ts
+++ b/src/libs/next/proxy/define-config.ts
@@ -240,6 +240,14 @@ export function defineConfig() {
'/oidc/handoff',
'/oidc/device/auth',
'/oidc/token',
+ // OIDC protocol endpoints a client reads before (or without) any browser
+ // session: discovery and JWKS are public by spec, and userinfo authenticates
+ // with the bearer access token the provider itself checks. Session-gating them
+ // redirects non-browser callers to the sign-in HTML, which is what a
+ // third-party app registered through Settings β†’ OAuth Apps hits first.
+ '/oidc/.well-known/openid-configuration',
+ '/oidc/jwks',
+ '/oidc/me',
// Interaction details for the consent/login page β€” must be reachable
// before the user has a session, so it cannot be session-gated.
'/oidc/interaction/(.*)',
diff --git a/src/libs/oidc-provider/http-adapter.test.ts b/src/libs/oidc-provider/http-adapter.test.ts
index f7a4fac039b..e889221e655 100644
--- a/src/libs/oidc-provider/http-adapter.test.ts
+++ b/src/libs/oidc-provider/http-adapter.test.ts
@@ -48,6 +48,28 @@ describe('OIDC HTTP adapter', () => {
});
describe('createNodeRequest', () => {
+ it('hands discovery to the provider route it hardcodes', async () => {
+ const request = new Request(
+ 'https://example.com/oidc/.well-known/openid-configuration',
+ ) as unknown as NextRequest;
+
+ const { createNodeRequest } = await import('./http-adapter');
+ const nodeRequest = await createNodeRequest(request);
+
+ expect(nodeRequest.url).toBe('/.well-known/openid-configuration');
+ });
+
+ it('leaves the prefixed routes untouched', async () => {
+ const { createNodeRequest } = await import('./http-adapter');
+
+ for (const path of ['/oidc/jwks', '/oidc/me', '/oidc/auth', '/oidc/token']) {
+ const request = new Request(`https://example.com${path}`) as unknown as NextRequest;
+ const nodeRequest = await createNodeRequest(request);
+
+ expect(nodeRequest.url).toBe(path);
+ }
+ });
+
it('passes POST bodies through as a readable Node stream without pre-parsing', async () => {
const body = 'grant_type=authorization_code&code=test-code';
const request = new Request('https://example.com/oidc/token?client_id=test', {
diff --git a/src/libs/oidc-provider/http-adapter.ts b/src/libs/oidc-provider/http-adapter.ts
index 59df93a99d4..a97efc4d233 100644
--- a/src/libs/oidc-provider/http-adapter.ts
+++ b/src/libs/oidc-provider/http-adapter.ts
@@ -27,6 +27,9 @@ export const convertHeadersToNodeHeaders = (nextHeaders: Headers): Record<string
* Create a Node.js HTTP request object for OIDC Provider
* @param req Next.js request object
*/
+const DISCOVERY_PUBLIC_PATH = '/oidc/.well-known/openid-configuration';
+const DISCOVERY_PROVIDER_PATH = '/.well-known/openid-configuration';
+
export const createNodeRequest = async (req: NextRequest): Promise<IncomingMessage> => {
// Build URL object
const url = new URL(req.url);
@@ -39,6 +42,14 @@ export const createNodeRequest = async (req: NextRequest): Promise<IncomingMessa
providerPath = '/' + providerPath;
}
+ // Discovery is the one route oidc-provider hardcodes, so it cannot be moved
+ // under the `/oidc` prefix the way every other route is. The document lives
+ // where clients look for it (`/oidc/.well-known/openid-configuration`, the
+ // issuer plus the well-known suffix) and is translated back here.
+ if (providerPath === DISCOVERY_PUBLIC_PATH) {
+ providerPath = DISCOVERY_PROVIDER_PATH;
+ }
+
log('Creating Node.js request from Next.js request');
log('Original path: %s, Provider path: %s', url.pathname, providerPath);
diff --git a/src/libs/oidc-provider/provider.ts b/src/libs/oidc-provider/provider.ts
index d98e32ba3f9..97b2e350f57 100644
--- a/src/libs/oidc-provider/provider.ts
+++ b/src/libs/oidc-provider/provider.ts
@@ -298,12 +298,18 @@ export const createOIDCProvider = async (db: LobeChatDatabase): Promise<Provider
// Added: enable refresh token rotation
rotateRefreshToken: true,
+ // Every route is written with the `/oidc` prefix because the provider is
+ // mounted under it and the adapter hands it the untouched pathname. A route
+ // left at its default (e.g. `/jwks`) is therefore unreachable: the request
+ // arrives as `/oidc/jwks` and the provider answers 404.
routes: {
authorization: '/oidc/auth',
code_verification: '/oidc/device',
device_authorization: '/oidc/device/auth',
end_session: '/oidc/session/end',
+ jwks: '/oidc/jwks',
token: '/oidc/token',
+ userinfo: '/oidc/me',
},
// 3. Scopes definition
scopes: defaultScopes,