2018-01-12 15:20:13 +01:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module dependencies
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
const Grant = require('grant-koa');
|
|
|
|
|
const mount = require('koa-mount');
|
|
|
|
|
|
|
|
|
|
module.exports = strapi => {
|
|
|
|
|
return {
|
|
|
|
|
beforeInitialize: function() {
|
|
|
|
|
strapi.config.middleware.load.after.push('provider');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
initialize: function(cb) {
|
2018-01-19 15:14:51 +01:00
|
|
|
|
const grantConfig = Object.assign(_.clone(_.get(strapi.plugins['users-permissions'].config, 'grant', {})), {
|
2018-01-12 15:20:13 +01:00
|
|
|
|
server: {
|
|
|
|
|
protocol: 'http',
|
|
|
|
|
host: 'localhost:1337'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-19 15:14:51 +01:00
|
|
|
|
const grant = new Grant(grantConfig);
|
2018-01-12 15:20:13 +01:00
|
|
|
|
|
2018-01-18 15:45:02 +01:00
|
|
|
|
strapi.app.use(async (ctx, next) => {
|
|
|
|
|
if (_.startsWith(ctx.request.url, '/connect') && ctx.request.method === 'GET') {
|
|
|
|
|
const provider = _.last(ctx.request.url.split('/'));
|
|
|
|
|
const config = strapi.plugins['users-permissions'].config.grant[provider];
|
|
|
|
|
|
|
|
|
|
if (_.get(config, 'enabled')) {
|
|
|
|
|
await next();
|
|
|
|
|
} else {
|
|
|
|
|
return ctx.badRequest(null, 'This provider is disabled.');
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
await next();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-12 15:20:13 +01:00
|
|
|
|
strapi.app.use(mount(grant));
|
|
|
|
|
|
|
|
|
|
cb();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|