|
@@ -1,30 +1,20 @@
|
|
|
import type { IUser, IUserHasId } from '@growi/core/dist/interfaces';
|
|
import type { IUser, IUserHasId } from '@growi/core/dist/interfaces';
|
|
|
import { serializeUserSecurely } from '@growi/core/dist/models/serializers';
|
|
import { serializeUserSecurely } from '@growi/core/dist/models/serializers';
|
|
|
-import type { NextFunction, Response } from 'express';
|
|
|
|
|
|
|
+import type { Response } from 'express';
|
|
|
import type { HydratedDocument } from 'mongoose';
|
|
import type { HydratedDocument } from 'mongoose';
|
|
|
import mongoose from 'mongoose';
|
|
import mongoose from 'mongoose';
|
|
|
|
|
|
|
|
import loggerFactory from '~/utils/logger';
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
|
|
|
|
+import { extractBearerToken } from './extract-bearer-token';
|
|
|
import type { AccessTokenParserReq } from './interfaces';
|
|
import type { AccessTokenParserReq } from './interfaces';
|
|
|
|
|
|
|
|
const logger = loggerFactory('growi:middleware:access-token-parser:api-token');
|
|
const logger = loggerFactory('growi:middleware:access-token-parser:api-token');
|
|
|
|
|
|
|
|
-const extractBearerToken = (authHeader: string | undefined): string | null => {
|
|
|
|
|
- if (authHeader == null) {
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (!authHeader.startsWith('Bearer ')) {
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return authHeader.substring(7); // Remove 'Bearer ' prefix
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
export const parserForApiToken = async(req: AccessTokenParserReq, res: Response): Promise<void> => {
|
|
export const parserForApiToken = async(req: AccessTokenParserReq, res: Response): Promise<void> => {
|
|
|
// Extract token from Authorization header first
|
|
// Extract token from Authorization header first
|
|
|
|
|
+ // It is more efficient to call it only once in "AccessTokenParser," which is the caller of the method
|
|
|
const bearerToken = extractBearerToken(req.headers.authorization);
|
|
const bearerToken = extractBearerToken(req.headers.authorization);
|
|
|
|
|
|
|
|
// Try all possible token sources in order of priority
|
|
// Try all possible token sources in order of priority
|