fix(auth): fix authentication request handling to exclude specific endpoints from authorization checks leading to permission denied errors (#566)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -176,4 +176,58 @@ describe('AuthInterceptorService', () => {
|
||||
expect((firstResponse as HttpResponse<string>).body).toBe('Bearer refreshed-token');
|
||||
expect((secondResponse as HttpResponse<string>).body).toBe('Bearer refreshed-token');
|
||||
});
|
||||
|
||||
describe('isExcludedAuthRequest', () => {
|
||||
const next = vi.fn((request: HttpRequest<unknown>) =>
|
||||
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<boolean>).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<boolean>).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<boolean>).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<boolean>).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<boolean>).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<boolean>).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<boolean>).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<boolean>).body).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user