Files
ReadMeABook/src/app/api/setup/test-oidc/route.ts
T
kikootwo f9947b745e Add requireSetupIncompleteOrAdmin and adjust routes
Introduce a new middleware requireSetupIncompleteOrAdmin that allows unauthenticated access while the setup wizard is in progress but enforces admin authentication once setup is complete. Replace requireSetupIncomplete with the new guard in test-paths, test-abs and test-oidc API routes. Update the front-end hook to use fetchWithAuth for authenticated requests. Revise setup-guard tests to cover the new semantics: shared endpoints now return 401 when setup is complete and no auth is provided, return 403 for authenticated non-admin users, and allow admin access or unauthenticated access during setup/DB-unready conditions; also add jwt verification and user lookup mocks to the tests.
2026-02-09 21:45:37 -05:00

100 lines
3.0 KiB
TypeScript

/**
* Test OIDC Configuration Endpoint
* Documentation: documentation/features/audiobookshelf-integration.md
*/
import { NextRequest, NextResponse } from 'next/server';
import { Issuer } from 'openid-client';
import { requireSetupIncompleteOrAdmin } from '@/lib/middleware/auth';
import { RMABLogger } from '@/lib/utils/logger';
const logger = RMABLogger.create('API.Setup.TestOIDC');
export async function POST(request: NextRequest) {
return requireSetupIncompleteOrAdmin(request, async (req) => {
try {
const body = await req.json();
const { issuerUrl, clientId, clientSecret } = body;
// Validate required fields
if (!issuerUrl || !clientId || !clientSecret) {
return NextResponse.json(
{
success: false,
error: 'Issuer URL, Client ID, and Client Secret are required'
},
{ status: 400 }
);
}
// Validate issuer URL format
try {
new URL(issuerUrl);
} catch {
return NextResponse.json(
{
success: false,
error: 'Invalid issuer URL format'
},
{ status: 400 }
);
}
// Attempt OIDC discovery
const issuer = await Issuer.discover(issuerUrl);
// Validate that we got the necessary endpoints
if (!issuer.metadata.authorization_endpoint ||
!issuer.metadata.token_endpoint ||
!issuer.metadata.userinfo_endpoint) {
return NextResponse.json(
{
success: false,
error: 'OIDC provider is missing required endpoints'
},
{ status: 500 }
);
}
// Return success with discovered metadata
return NextResponse.json({
success: true,
issuer: {
issuer: issuer.issuer,
authorizationEndpoint: issuer.metadata.authorization_endpoint,
tokenEndpoint: issuer.metadata.token_endpoint,
userinfoEndpoint: issuer.metadata.userinfo_endpoint,
jwksUri: issuer.metadata.jwks_uri,
supportedScopes: issuer.metadata.scopes_supported || [],
supportedResponseTypes: issuer.metadata.response_types_supported || [],
},
});
} catch (error) {
logger.error('Discovery failed', { error: error instanceof Error ? error.message : String(error) });
// Determine error message
let errorMessage = 'OIDC discovery failed';
if (error instanceof Error) {
errorMessage = error.message;
// Provide more helpful messages for common errors
if (errorMessage.includes('ENOTFOUND') || errorMessage.includes('ECONNREFUSED')) {
errorMessage = 'Cannot reach OIDC provider. Check the issuer URL and network connectivity.';
} else if (errorMessage.includes('404')) {
errorMessage = 'OIDC discovery endpoint not found. Verify the issuer URL is correct.';
} else if (errorMessage.includes('timeout')) {
errorMessage = 'Connection to OIDC provider timed out. Check the issuer URL.';
}
}
return NextResponse.json(
{
success: false,
error: errorMessage
},
{ status: 500 }
);
}
});
}