passport-telegram-web-app
Version:
A [Passport](https://www.passportjs.org/) strategy for [telegram web app (bots)](https://core.telegram.org/bots/webapps) authentication.
82 lines (81 loc) • 3.61 kB
JavaScript
;
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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Strategy = void 0;
var passport_strategy_1 = require("passport-strategy");
var constants_1 = require("./constants");
var extract_data_class_1 = require("./extract-data.class");
var extract_hash_class_1 = require("./extract-hash.class");
var utils_1 = require("./utils");
var Strategy = /** @class */ (function (_super) {
__extends(Strategy, _super);
function Strategy(options, verify) {
var _this = this;
var _a, _b, _c, _d;
_this = _super.call(this) || this;
_this.options = options;
_this.verify = verify;
_this.name = constants_1.STRATEGY_NAME;
_this.dataFromRequest = (_a = options.dataFromRequest) !== null && _a !== void 0 ? _a : extract_data_class_1.ExtractData.fromHeaders;
_this.hashFromRequest = (_b = options.hashFromRequest) !== null && _b !== void 0 ? _b : extract_hash_class_1.ExtractHash.fromHeaders;
_this.dataToCheckString = (_c = options.dataToCheckString) !== null && _c !== void 0 ? _c : utils_1.Utils.defaultDataToCheckString;
_this.hashVerifier = (_d = options.hashVerifier) !== null && _d !== void 0 ? _d : utils_1.Utils.defaultHashVerifier;
return _this;
}
Strategy.prototype.authenticate = function (request, options) {
var _this = this;
var data = this.dataFromRequest(request);
var hash = this.hashFromRequest(request);
if (!data) {
return this.fail(new Error("No data"), 401);
}
if (!hash) {
return this.fail(new Error("No hash"), 401);
}
this.hashVerifier(this.options.token, this.dataToCheckString(data), hash, function (error) {
if (error) {
return _this.fail(error, 401);
}
if (_this.options.expiration && Date.now() / 1000 - parseInt(data.authDate, 10) > _this.options.expiration) {
return _this.fail(new Error("Expired"), 401);
}
var done = _this.done.bind(_this);
try {
if (_this.options.passRequestToCallback) {
_this.verify(request, data.user, done);
}
else {
_this.verify(data.user, done);
}
}
catch (error) {
_this.fail(error, 401);
}
});
};
Strategy.prototype.done = function (error, user, info) {
if (error) {
return this.error(error);
}
else if (!user) {
return this.fail(info);
}
return this.success(user, info);
};
return Strategy;
}(passport_strategy_1.Strategy));
exports.Strategy = Strategy;