azurite
Version:
An open source Azure Storage API compatible server
44 lines • 2.07 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const BlobStorageContext_1 = tslib_1.__importDefault(require("../context/BlobStorageContext"));
const StorageErrorFactory_1 = tslib_1.__importDefault(require("../errors/StorageErrorFactory"));
const ExpressRequestAdapter_1 = tslib_1.__importDefault(require("../generated/ExpressRequestAdapter"));
const ExpressResponseAdapter_1 = tslib_1.__importDefault(require("../generated/ExpressResponseAdapter"));
const constants_1 = require("../utils/constants");
class AuthenticationMiddlewareFactory {
constructor(logger) {
this.logger = logger;
}
createAuthenticationMiddleware(authenticators) {
return (req, res, next) => {
const request = new ExpressRequestAdapter_1.default(req);
const response = new ExpressResponseAdapter_1.default(res);
const context = new BlobStorageContext_1.default(res.locals, constants_1.DEFAULT_CONTEXT_PATH);
this.authenticate(context, request, response, authenticators)
.then(pass => {
// TODO: To support public access, we need to modify here to reject request later in handler
if (pass) {
next();
}
else {
next(StorageErrorFactory_1.default.getAuthorizationFailure(context.contextId));
}
})
.catch(next);
};
}
async authenticate(context, req, res, authenticators) {
this.logger.verbose(`AuthenticationMiddlewareFactory:createAuthenticationMiddleware() Validating authentications.`, context.contextId);
let pass = false;
for (const authenticator of authenticators) {
pass = await authenticator.validate(req, context);
if (pass === true) {
return true;
}
}
return false;
}
}
exports.default = AuthenticationMiddlewareFactory;
//# sourceMappingURL=AuthenticationMiddlewareFactory.js.map
;