You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

72 lines
2.8 KiB

"use strict";
/**
* Creator: Christian Hotz
* Company: hydra newmedia GmbH
* Date: 27.06.16
*
* Copyright hydra newmedia GmbH
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
exports.__esModule = true;
/**
* Imports
*/
var _ = require("lodash");
var passport_strategy_1 = require("passport-strategy");
var BadRequestError_1 = require("./errors/BadRequestError");
var Strategy = /** @class */ (function (_super) {
__extends(Strategy, _super);
function Strategy(header, passReqToCallback, verify) {
var _this = _super.call(this) || this;
_this.apiKeyHeader = header || { header: 'X-Api-Key', prefix: '' };
if (!_this.apiKeyHeader.header)
_this.apiKeyHeader.header = 'X-Api-Key';
if (!_this.apiKeyHeader.prefix)
_this.apiKeyHeader.prefix = '';
_this.apiKeyHeader.header = _this.apiKeyHeader.header.toLowerCase();
_this.name = 'headerapikey';
_this.verify = verify;
_this.passReqToCallback = passReqToCallback || false;
return _this;
}
Strategy.prototype.authenticate = function (req, options) {
var _this = this;
var apiKey = _.get(req.headers, this.apiKeyHeader.header);
if (!apiKey) {
return this.fail(new BadRequestError_1.BadRequestError('Missing API Key'), null);
}
if (_.startsWith(apiKey, this.apiKeyHeader.prefix)) {
apiKey = apiKey.replace(new RegExp('^' + this.apiKeyHeader.prefix), '');
}
else {
return this.fail(new BadRequestError_1.BadRequestError('Invalid API Key prefix, ' + this.apiKeyHeader.header + ' header should start with "' + this.apiKeyHeader.prefix + '"'), null);
}
var verified = function (err, user, info) {
if (err) {
return _this.error(err);
}
if (!user) {
return _this.fail(info, null);
}
_this.success(user, info);
};
var optionalCallbackParams = [];
if (this.passReqToCallback)
optionalCallbackParams.push(req);
this.verify.apply(this, [apiKey, verified].concat(optionalCallbackParams));
};
return Strategy;
}(passport_strategy_1.Strategy));
exports.Strategy = Strategy;