124 lines
2.9 KiB
JavaScript
Raw Normal View History

'use strict';
const { castArray } = require('lodash/fp');
2021-10-27 18:54:58 +02:00
const { UnauthorizedError, ForbiddenError } = require('@strapi/utils').errors;
const constants = require('../services/constants');
const { getService } = require('../utils');
2022-08-08 23:33:39 +02:00
const isReadScope = (scope) => scope.endsWith('find') || scope.endsWith('findOne');
2022-08-08 23:33:39 +02:00
const extractToken = (ctx) => {
2021-11-15 17:54:17 +01:00
if (ctx.request && ctx.request.header && ctx.request.header.authorization) {
const parts = ctx.request.header.authorization.split(/\s+/);
2021-11-15 17:54:17 +01:00
if (parts[0].toLowerCase() !== 'bearer' || parts.length !== 2) {
return null;
}
return parts[1];
}
2021-11-15 17:54:17 +01:00
return null;
};
2022-08-22 09:25:55 +02:00
/**
* Authenticate the validity of the token
*
* @type {import('.').AuthenticateFunction} */
2022-08-08 23:33:39 +02:00
const authenticate = async (ctx) => {
2021-11-15 17:54:17 +01:00
const apiTokenService = getService('api-token');
const token = extractToken(ctx);
2021-11-15 17:54:17 +01:00
if (!token) {
return { authenticated: false };
}
const apiToken = await apiTokenService.getBy({
accessKey: apiTokenService.hash(token),
});
2022-08-22 09:25:55 +02:00
// token not found
if (!apiToken) {
return { authenticated: false };
}
2022-08-24 10:32:24 +02:00
// token has expired
if (apiToken.expiresAt && apiToken.expiresAt < Date.now()) {
throw new UnauthorizedError('Token expired');
}
2022-08-19 16:36:28 +02:00
// update lastUsedAt
2022-08-17 23:28:35 +02:00
await apiTokenService.update(apiToken.id, {
2022-08-19 16:36:28 +02:00
lastUsedAt: new Date(),
2022-08-17 23:28:35 +02:00
});
if (apiToken.type === constants.API_TOKEN_TYPE.CUSTOM) {
const ability = await strapi.contentAPI.permissions.engine.generateAbility(
apiToken.permissions.map((action) => ({ action }))
);
return { authenticated: true, ability, credentials: apiToken };
}
return { authenticated: true, credentials: apiToken };
};
2022-08-22 09:25:55 +02:00
/**
* Verify the token has the required abilities for the requested scope
*
* @type {import('.').VerifyFunction} */
const verify = (auth, config) => {
const { credentials: apiToken, ability } = auth;
if (!apiToken) {
2022-08-24 10:32:24 +02:00
throw new UnauthorizedError('Token not found');
}
2022-08-22 09:25:55 +02:00
// token has expired
if (apiToken.expiresAt && apiToken.expiresAt < Date.now()) {
2022-08-24 10:32:24 +02:00
throw new UnauthorizedError('Token expired');
2022-08-22 09:25:55 +02:00
}
// Full access
if (apiToken.type === constants.API_TOKEN_TYPE.FULL_ACCESS) {
return;
}
// Read only
if (apiToken.type === constants.API_TOKEN_TYPE.READ_ONLY) {
/**
* If you don't have `full-access` you can only access `find` and `findOne`
* scopes. If the route has no scope, then you can't get access to it.
*/
const scopes = castArray(config.scope);
if (config.scope && scopes.every(isReadScope)) {
return;
}
}
// Custom
2022-08-11 12:18:53 +02:00
else if (apiToken.type === constants.API_TOKEN_TYPE.CUSTOM) {
if (!ability) {
throw new ForbiddenError();
}
const scopes = castArray(config.scope);
const isAllowed = scopes.every((scope) => ability.can(scope));
if (isAllowed) {
return;
}
}
2021-10-27 18:54:58 +02:00
throw new ForbiddenError();
};
/** @type {import('.').AuthStrategy} */
module.exports = {
name: 'api-token',
authenticate,
verify,
};