An authorization middleware's sole purpose is to authorize incoming requests into your application by either verifying session data, header data or tokens sent through the input of an application.
Authorization middleware functions should be placed among the first middlewares of your action, since it verifies
the source of a request and can stop any unauthorized requests from further processing. Once it validates the
authorization source, it should attach any custom data or object to the intentObj via the data()
function.
The core authorization middleware class is under thorin.Authorization
and can be extended by other thorin
components, using the pattern below:
'use strict'
const CoreAuthorization = thorin.Authorization;
thorin.Authorization = class ThorinAuthorization extends CoreAuthorization {
constructor(name) {
super(name);
// your custom functionality
}
}
func(intentObj, next)
,
after all input validation had completed. Note that it only works with inline pure functions and not named middleware.
_runStack
function, it will execute the internal authorization call stack in the
chronological order they were defined. This is usually called by the dispatcher.
'use strict';
// Register our authorization header that will check the Authorization headers (default authorization source in thorin-transport-http)
const dispatcher = thorin.dispatcher;
const ADMIN_TOKEN = 'admin123';
dispatcher
.addAuthorization('api.token')
.use((intentObj, next) => {
const authType = intentObj.authorizationSource,
authToken = intentObj.authorization;
if(authType !== 'TOKEN') {
return next(thorin.error('AUTHORIZATION', 'Authorization method unsupported', 401));
}
if(authToken !== ADMIN_TOKEN) {
return next(thorin.error('AUTHORIZATION', 'Invalid authorization token', 401));
}
// Attach any kind of information to the intent
next();
});
// Define an action that uses the above authorization
dispatcher
.addAction('admin.show')
.alias('GET', '/resource')
.authorize('api.token')
.use((intentObj, next) => {
log.info('Admin request completed');
next();
})
.before('authorize', (intentObj) => {
log.info('Authorization will happen');
});
You can always create a new issue on GitHub or contact one of the core founders by chat.