From 4d6691ef949f7009b0c816611ccb202db457ebcf Mon Sep 17 00:00:00 2001 From: Zach Date: Fri, 17 Apr 2026 20:56:44 -0400 Subject: [PATCH] fix(auth): fix authentication request handling to exclude specific endpoints from authorization checks leading to permission denied errors (#566) --- .../config/security/SecurityConfig.java | 5 +- .../security/auth-interceptor.service.spec.ts | 54 +++++++++++++++++++ .../core/security/auth-interceptor.service.ts | 13 ++++- 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/booklore-api/src/main/java/org/booklore/config/security/SecurityConfig.java b/booklore-api/src/main/java/org/booklore/config/security/SecurityConfig.java index a8af686e1..acdeaa527 100644 --- a/booklore-api/src/main/java/org/booklore/config/security/SecurityConfig.java +++ b/booklore-api/src/main/java/org/booklore/config/security/SecurityConfig.java @@ -57,7 +57,10 @@ public class SecurityConfig { "/kobo/**", // Kobo API requests (auth handled in KoboAuthFilter) "/api/docs", // API documentation UI "/api/openapi.json", // OpenAPI spec (used by API documentation UI) - "/api/v1/auth/**", // Login and token refresh endpoints (must remain public) + "/api/v1/auth/login", // Login endpoint (must remain public) + "/api/v1/auth/refresh", // Token refresh endpoint (must remain public) + "/api/v1/auth/remote", // Remote auth endpoint (must remain public) + "/api/v1/auth/oidc/**", // OIDC authentication endpoints "/api/v1/public-settings", // Public endpoint for checking OIDC or other app settings "/api/v1/setup/**", // Setup wizard endpoints (must remain accessible before initial setup) "/api/v1/healthcheck/**" // Healthcheck endpoints (must remain accessible for Docker healthchecks) diff --git a/frontend/src/app/core/security/auth-interceptor.service.spec.ts b/frontend/src/app/core/security/auth-interceptor.service.spec.ts index 0509c4a23..3ad49bd61 100644 --- a/frontend/src/app/core/security/auth-interceptor.service.spec.ts +++ b/frontend/src/app/core/security/auth-interceptor.service.spec.ts @@ -176,4 +176,58 @@ describe('AuthInterceptorService', () => { expect((firstResponse as HttpResponse).body).toBe('Bearer refreshed-token'); expect((secondResponse as HttpResponse).body).toBe('Bearer refreshed-token'); }); + + describe('isExcludedAuthRequest', () => { + const next = vi.fn((request: HttpRequest) => + of(new HttpResponse({status: 200, body: request.headers.has('Authorization')})) + ); + + it('excludes /api/v1/auth/login', async () => { + authService.getInternalAccessToken.mockReturnValue('token-123'); + const response = await firstValueFrom(interceptor(new HttpRequest('POST', `${API_CONFIG.BASE_URL}/api/v1/auth/login`, null), next)); + expect((response as HttpResponse).body).toBe(false); + }); + + it('excludes /api/v1/auth/refresh', async () => { + authService.getInternalAccessToken.mockReturnValue('token-123'); + const response = await firstValueFrom(interceptor(new HttpRequest('POST', `${API_CONFIG.BASE_URL}/api/v1/auth/refresh`, null), next)); + expect((response as HttpResponse).body).toBe(false); + }); + + it('excludes /api/v1/auth/remote', async () => { + authService.getInternalAccessToken.mockReturnValue('token-123'); + const response = await firstValueFrom(interceptor(new HttpRequest('GET', `${API_CONFIG.BASE_URL}/api/v1/auth/remote`), next)); + expect((response as HttpResponse).body).toBe(false); + }); + + it('excludes /api/v1/auth/oidc/state', async () => { + authService.getInternalAccessToken.mockReturnValue('token-123'); + const response = await firstValueFrom(interceptor(new HttpRequest('GET', `${API_CONFIG.BASE_URL}/api/v1/auth/oidc/state`), next)); + expect((response as HttpResponse).body).toBe(false); + }); + + it('excludes /api/v1/auth/oidc/callback', async () => { + authService.getInternalAccessToken.mockReturnValue('token-123'); + const response = await firstValueFrom(interceptor(new HttpRequest('POST', `${API_CONFIG.BASE_URL}/api/v1/auth/oidc/callback`, null), next)); + expect((response as HttpResponse).body).toBe(false); + }); + + it('does NOT exclude /api/v1/auth/login-history (exact match test)', async () => { + authService.getInternalAccessToken.mockReturnValue('token-123'); + const response = await firstValueFrom(interceptor(new HttpRequest('GET', `${API_CONFIG.BASE_URL}/api/v1/auth/login-history`), next)); + expect((response as HttpResponse).body).toBe(true); + }); + + it('does NOT exclude /api/v1/auth/login?query=1 (exact match test)', async () => { + authService.getInternalAccessToken.mockReturnValue('token-123'); + const response = await firstValueFrom(interceptor(new HttpRequest('GET', `${API_CONFIG.BASE_URL}/api/v1/auth/login?query=1`), next)); + expect((response as HttpResponse).body).toBe(false); + }); + + it('does NOT exclude /api/v1/auth/logout', async () => { + authService.getInternalAccessToken.mockReturnValue('token-123'); + const response = await firstValueFrom(interceptor(new HttpRequest('POST', `${API_CONFIG.BASE_URL}/api/v1/auth/logout`, null), next)); + expect((response as HttpResponse).body).toBe(true); + }); + }); }); diff --git a/frontend/src/app/core/security/auth-interceptor.service.ts b/frontend/src/app/core/security/auth-interceptor.service.ts index 588cf42d4..c7c8fbe4f 100644 --- a/frontend/src/app/core/security/auth-interceptor.service.ts +++ b/frontend/src/app/core/security/auth-interceptor.service.ts @@ -13,7 +13,18 @@ export const AuthInterceptorService: HttpInterceptorFn = (req, next: HttpHandler const token = authService.getInternalAccessToken(); const isApiRequest = req.url.startsWith(`${API_CONFIG.BASE_URL}/api/`); - const isAuthRequest = req.url.startsWith(`${API_CONFIG.BASE_URL}/api/v1/auth/`); + const authExcludePaths = [ + '/api/v1/auth/login', + '/api/v1/auth/refresh', + '/api/v1/auth/remote', + '/api/v1/auth/oidc/state', + '/api/v1/auth/oidc/callback', + '/api/v1/auth/oidc/redirect', + '/api/v1/auth/oidc/mobile/callback', + '/api/v1/auth/oidc/backchannel-logout', + ]; + const urlPath = req.url.split('?')[0]; + const isAuthRequest = authExcludePaths.some(path => urlPath === `${API_CONFIG.BASE_URL}${path}`); const hasAuthHeader = req.headers.has('Authorization'); const authReq = (token && isApiRequest && !isAuthRequest && !hasAuthHeader) ? req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }) : req;