Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

refactor: move api token extraction logic to helper function

Pranav Cb2cbfb2c005b
diff --git a/packages/nocodb/src/guards/global/global.guard.ts b/packages/nocodb/src/guards/global/global.guard.ts
index 0d1ad833fd..3f4e825d59 100644
--- a/packages/nocodb/src/guards/global/global.guard.ts
+++ b/packages/nocodb/src/guards/global/global.guard.ts
@@ -5,7 +5,7 @@ import { extractRolesObj } from 'nocodb-sdk';
import type { Request } from 'express';
import type { ExecutionContext } from '@nestjs/common';
import { JwtStrategy } from '~/strategies/jwt.strategy';
-import { getApiTokenFromAuthHeader } from '~/helpers';
+import { getApiTokenFromHeader } from '~/helpers';
@Injectable()
export class GlobalGuard extends AuthGuard(['jwt']) {
@@ -49,10 +49,7 @@ export class GlobalGuard extends AuthGuard(['jwt']) {
if (result) return true;
- if (
- req.headers['xc-token'] ||
- getApiTokenFromAuthHeader(req.headers['authorization'])
- ) {
+ if (getApiTokenFromHeader(req)) {
let canActivate = false;
try {
const guard = new (AuthGuard('authtoken'))(context);
diff --git a/packages/nocodb/src/helpers/apiHelpers.ts b/packages/nocodb/src/helpers/apiHelpers.ts
index 9a18851ebc..0057208327 100644
--- a/packages/nocodb/src/helpers/apiHelpers.ts
+++ b/packages/nocodb/src/helpers/apiHelpers.ts
@@ -2,7 +2,7 @@ import Ajv from 'ajv';
import addFormats from 'ajv-formats';
import type { ErrorObject } from 'ajv';
import type { NextFunction, Request, Response } from 'express';
-import type { NcApiVersion } from 'nocodb-sdk';
+import type { NcApiVersion, NcRequest } from 'nocodb-sdk';
import { NcError } from '~/helpers/catchError';
import swagger, { swaggerV3Validation } from '~/schema';
@@ -79,12 +79,33 @@ export const validatePayload = (
}
};
-export const getApiTokenFromAuthHeader = (headerValue?: string) => {
- if (!headerValue) return;
+/**
+ * Extracts API token from request headers.
+ * - Prefers `xc-token` header
+ * - Falls back to `Authorization: Bearer <token>`
+ */
+export function getApiTokenFromHeader(
+ req?:
+ | NcRequest
+ | {
+ headers?: Record<string, unknown>;
+ },
+): string | undefined {
+ const headers = req?.headers;
+ if (!headers) return;
- if (!headerValue.toLowerCase?.()?.startsWith('bearer ')) {
- return;
+ // 1) Prefer explicit xc-token header
+ const token = headers['xc-token'];
+ if (typeof token === 'string' && token.trim()) {
+ return token.trim();
}
- return headerValue.substring(7);
-};
+ // 2) Fallback to Authorization: Bearer <token>
+ const auth = headers['authorization'];
+ if (typeof auth !== 'string') return;
+
+ const value = auth.trim();
+ if (value.toLowerCase().startsWith('bearer ')) {
+ return value.slice(7).trim();
+ }
+}
diff --git a/packages/nocodb/src/strategies/authtoken.strategy/authtoken.strategy.ts b/packages/nocodb/src/strategies/authtoken.strategy/authtoken.strategy.ts
index 1aed5442cd..77a121757b 100644
--- a/packages/nocodb/src/strategies/authtoken.strategy/authtoken.strategy.ts
+++ b/packages/nocodb/src/strategies/authtoken.strategy/authtoken.strategy.ts
@@ -5,7 +5,7 @@ import { Strategy } from 'passport-custom';
import type { NcRequest } from '~/interface/config';
import { ApiToken, User } from '~/models';
import { sanitiseUserObj } from '~/utils';
-import { getApiTokenFromAuthHeader } from '~/helpers';
+import { getApiTokenFromHeader } from '~/helpers';
@Injectable()
export class AuthTokenStrategy extends PassportStrategy(Strategy, 'authtoken') {
@@ -15,11 +15,7 @@ export class AuthTokenStrategy extends PassportStrategy(Strategy, 'authtoken') {
let user;
// Extract token from either xc-token header or Authorization Bearer header
- let token = req.headers['xc-token'];
-
- if (!token && req.headers.authorization) {
- token = getApiTokenFromAuthHeader(req.headers.authorization);
- }
+ const token = getApiTokenFromHeader(req);
if (token) {
const apiToken = await ApiToken.getByToken(token);