2021-08-04 19:39:40 +02:00
|
|
|
/* eslint-disable no-unused-vars */
|
2021-03-25 14:59:44 +01:00
|
|
|
'use strict';
|
|
|
|
|
2021-07-23 18:04:45 +02:00
|
|
|
// const permissionsFieldsToPropertiesMigration = require('../migrations/permissions-fields-to-properties');
|
2021-03-25 14:59:44 +01:00
|
|
|
|
2021-09-06 15:04:59 +02:00
|
|
|
/**
|
|
|
|
* Tries to authenticated admin user and calls next.
|
|
|
|
* @param {KoaContext} ctx
|
|
|
|
* @param {Middleware} next
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
const authMiddleware = async (ctx, next) => {
|
|
|
|
if (!ctx.request.header.authorization) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
ctx.request.header.authorization &&
|
|
|
|
ctx.request.header.authorization.split(' ')[0] === 'Bearer'
|
|
|
|
) {
|
|
|
|
const token = ctx.request.header.authorization.split(' ')[1];
|
|
|
|
|
|
|
|
const { payload, isValid } = strapi.admin.services.token.decodeJwtToken(token);
|
|
|
|
|
|
|
|
if (isValid) {
|
|
|
|
const admin = await strapi
|
|
|
|
.query('admin::user')
|
|
|
|
.findOne({ where: { id: payload.id }, populate: ['roles'] });
|
|
|
|
|
|
|
|
if (!admin || !(admin.isActive === true)) {
|
|
|
|
return ctx.unauthorized('Invalid credentials');
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: use simple user & isAuthenticated
|
|
|
|
|
|
|
|
ctx.state.admin = admin;
|
|
|
|
ctx.state.user = admin;
|
|
|
|
ctx.state.userAbility = await strapi.admin.services.permission.engine.generateUserAbility(
|
|
|
|
admin
|
|
|
|
);
|
|
|
|
|
|
|
|
ctx.state.isAuthenticatedAdmin = true;
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.unauthorized('Invalid credentials');
|
|
|
|
};
|
|
|
|
|
2021-03-25 14:59:44 +01:00
|
|
|
module.exports = () => {
|
2021-09-06 15:04:59 +02:00
|
|
|
const passportMiddleware = strapi.admin.services.passport.init();
|
|
|
|
|
|
|
|
strapi.server.api('admin').use(passportMiddleware);
|
|
|
|
strapi.server.api('admin').use(authMiddleware);
|
|
|
|
|
2021-06-17 19:51:35 +02:00
|
|
|
// FIXME: to implement
|
|
|
|
// strapi.db.migrations.register(permissionsFieldsToPropertiesMigration);
|
2021-03-25 14:59:44 +01:00
|
|
|
};
|