Skip to content

Commit cb38688

Browse files
Fix auth handler (#1706)
<!-- Ensure the title clearly reflects what was changed. Provide a clear and concise description of the changes made. The PR should only contain the changes related to the issue, and no other unrelated changes. --> Fixes OPS-3125 Deployed to UX <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Simplified authentication header handling in API requests * Enhanced token retrieval to prioritize cookies alongside authorization headers * Optimized internal authentication validation logic <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 6e14098 commit cb38688

2 files changed

Lines changed: 21 additions & 25 deletions

File tree

packages/react-ui/src/app/interceptors/request-interceptor.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
1-
import { API_URL, isUrlRelative } from '@/app/lib/api';
21
import { authenticationSession } from '@/app/lib/authentication-session';
32
import { InternalAxiosRequestConfig } from 'axios';
4-
5-
const unauthenticatedRoutes = [
6-
'/v1/authentication/sign-in',
7-
'/v1/authentication/sign-up',
8-
'/v1/authn/local/verify-email',
9-
'/v1/flags',
10-
'/v1/forms/',
11-
'/v1/user-invitations/accept',
12-
];
3+
import { isUrlRelative } from '../lib/api';
134

145
const needsAuthHeader = (url: string): boolean => {
15-
const resolvedUrl = !isUrlRelative(url) ? url : `${API_URL}${url}`;
16-
const isLocalUrl = resolvedUrl.includes(API_URL);
17-
const isUnauthenticatedRoute = unauthenticatedRoutes.some((route) =>
18-
url.startsWith(route),
19-
);
20-
21-
return !isUnauthenticatedRoute && isLocalUrl;
6+
return isUrlRelative(url);
227
};
238

249
export function createRequestInterceptor(): (

packages/server/api/src/app/core/security/authn/access-token-authn-handler.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cacheWrapper, logger } from '@openops/server-shared';
1+
import { logger } from '@openops/server-shared';
22
import {
33
ApplicationError,
44
ErrorCode,
@@ -11,15 +11,28 @@ import { userService } from '../../../user/user-service';
1111
import { BaseSecurityHandler } from '../security-handler';
1212

1313
export class AccessTokenAuthnHandler extends BaseSecurityHandler {
14+
private static readonly COOKIE_NAME = 'token';
1415
private static readonly HEADER_NAME = 'authorization';
1516
private static readonly HEADER_PREFIX = 'Bearer ';
1617

1718
protected canHandle(request: FastifyRequest): Promise<boolean> {
18-
const header = request.headers[AccessTokenAuthnHandler.HEADER_NAME];
19-
const prefix = AccessTokenAuthnHandler.HEADER_PREFIX;
20-
const routeMatches = header?.startsWith(prefix) ?? false;
19+
const hasToken = this.getAccessToken(request) !== undefined;
2120
const skipAuth = request.routeOptions.config?.skipAuth ?? false;
22-
return Promise.resolve(routeMatches && !skipAuth);
21+
return Promise.resolve(hasToken && !skipAuth);
22+
}
23+
24+
private getAccessToken(request: FastifyRequest): string | undefined {
25+
const cookieToken = request.cookies?.[AccessTokenAuthnHandler.COOKIE_NAME];
26+
if (!isNil(cookieToken)) {
27+
return cookieToken;
28+
}
29+
30+
const header = request.headers[AccessTokenAuthnHandler.HEADER_NAME];
31+
if (header?.startsWith(AccessTokenAuthnHandler.HEADER_PREFIX)) {
32+
return header.substring(AccessTokenAuthnHandler.HEADER_PREFIX.length);
33+
}
34+
35+
return undefined;
2336
}
2437

2538
protected async doHandle(request: FastifyRequest): Promise<void> {
@@ -50,9 +63,7 @@ export class AccessTokenAuthnHandler extends BaseSecurityHandler {
5063
}
5164

5265
private extractAccessTokenOrThrow(request: FastifyRequest): string {
53-
const header = request.headers[AccessTokenAuthnHandler.HEADER_NAME];
54-
const prefix = AccessTokenAuthnHandler.HEADER_PREFIX;
55-
const accessToken = header?.substring(prefix.length);
66+
const accessToken = this.getAccessToken(request);
5667

5768
if (isNil(accessToken)) {
5869
throw new ApplicationError({

0 commit comments

Comments
 (0)