Files
directus/api/src/middleware/extract-token.ts
Jay Cammarano d8c9257058 Bearer token no longer case sensitive for API authenticatiom (#11307)
* seemingly fixes issues/11300

* Add unit tests for extract-token

Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
2022-01-27 14:28:26 -05:00

40 lines
876 B
TypeScript

/**
* Extract access token from:
*
* Authorization: Bearer
* access_token query parameter
*
* and store in req.token
*/
import { RequestHandler } from 'express';
const extractToken: RequestHandler = (req, res, next) => {
let token: string | null = null;
if (req.query && req.query.access_token) {
token = req.query.access_token as string;
}
if (req.headers && req.headers.authorization) {
const parts = req.headers.authorization.split(' ');
if (parts.length === 2 && parts[0].toLowerCase() === 'bearer') {
token = parts[1];
}
}
/**
* @TODO
* Look into RFC6750 compliance:
* In order to be fully compliant with RFC6750, we have to throw a 400 error when you have the
* token in more than 1 place afaik. We also might have to support "access_token" as a post body
* key
*/
req.token = token;
next();
};
export default extractToken;