@studyportals/sp-hs-misc
Version:
Miscellaneous code used in HouseStark's projects
286 lines (275 loc) • 11 kB
JavaScript
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* @deprecated Use @studyportals/client-internal-platform-sso
*/
var UserSessionCookieManager = function () {
_createClass(UserSessionCookieManager, [{
key: "intervalDuration",
get: function get() {
return this._intervalDuration;
}
}, {
key: "cognitoClientId",
get: function get() {
return this._cognitoClientId;
}
}, {
key: "cognitoPoolId",
get: function get() {
return this._cognitoPoolId;
}
}, {
key: "cognitoIdentityServiceProvider",
get: function get() {
return this._cognitoIdentityServiceProvider = this._cognitoIdentityServiceProvider || new CognitoIdentityProviderClient();
}
}, {
key: "earlyRefreshPeriod",
get: function get() {
return UserSessionCookieManager.EARLY_REFRESH_PERIOD_IN_MS;
}
}, {
key: "cookieDomain",
get: function get() {
return this._cookieDomain;
}
}]);
function UserSessionCookieManager(cognitoClientId, cognitoPoolId, domain) {
_classCallCheck(this, UserSessionCookieManager);
this._cognitoClientId = cognitoClientId;
this._cognitoPoolId = cognitoPoolId;
this._cookieDomain = domain;
}
_createClass(UserSessionCookieManager, [{
key: "getUsername",
value: function getUsername() {
return this.getCookie(UserSessionCookieManager.COOKIE_USERNAME);
}
}, {
key: "setUsername",
value: function setUsername(value) {
this.setCookie(UserSessionCookieManager.COOKIE_USERNAME, value);
}
}, {
key: "getRefreshToken",
value: function getRefreshToken() {
return this.getCookie(UserSessionCookieManager.COOKIE_REFRESH_TOKEN);
}
}, {
key: "setRefreshToken",
value: function setRefreshToken(value) {
this.setCookie(UserSessionCookieManager.COOKIE_REFRESH_TOKEN, value);
}
}, {
key: "getCreationTime",
value: function getCreationTime() {
return new Date(+this.getCookie(UserSessionCookieManager.COOKIE_CREATION_TIME));
}
}, {
key: "setCreationTime",
value: function setCreationTime(date) {
this.setCookie(UserSessionCookieManager.COOKIE_CREATION_TIME, String(date.getTime()));
}
}, {
key: "getAvailabilityLength",
value: function getAvailabilityLength() {
return +this.getCookie(UserSessionCookieManager.COOKIE_AVAILABILITY_LENGTH);
}
}, {
key: "setAvailabilityLength",
value: function setAvailabilityLength(value) {
this.setCookie(UserSessionCookieManager.COOKIE_AVAILABILITY_LENGTH, String(value));
}
}, {
key: "getIdToken",
value: function getIdToken() {
return this.getCookie(UserSessionCookieManager.COOKIE_ID_TOKEN);
}
}, {
key: "setIdToken",
value: function setIdToken(value) {
this.setCookie(UserSessionCookieManager.COOKIE_ID_TOKEN, value);
}
}, {
key: "updateCookie",
value: function updateCookie(idToken, availabilityLength) {
this.setCreationTime(this.getCurrentTime());
this.setAvailabilityLength(availabilityLength);
this.setIdToken(idToken);
}
}, {
key: "setCookie",
value: function setCookie(name, value) {
Cookies.set(name, value, { domain: "." + this.cookieDomain });
}
}, {
key: "getCookie",
value: function getCookie(name) {
return Cookies.get(name);
}
}, {
key: "getAuthorization",
value: function getAuthorization() {
return this.getIdToken();
}
}, {
key: "keepAlive",
value: function keepAlive() {
if (!this.isSessionSetup()) {
throw new Error("Set-up the session before attempting to install the manager");
}
this.generateIntervalDuration();
this.refreshSessionIfNeeded();
this.setInterval(this.refreshSessionIfNeeded, this.intervalDuration);
}
}, {
key: "setupFromSuccessfulAuthentication",
value: function setupFromSuccessfulAuthentication(authenticationResult) {
this.setUsername(authenticationResult.userIdentifier);
this.setRefreshToken(authenticationResult.refreshToken);
this.updateCookie(authenticationResult.idToken, authenticationResult.idTokenAvailabilityInMs);
}
}, {
key: "setupFromSuccessfulAuthenticationAndKeepAlive",
value: function setupFromSuccessfulAuthenticationAndKeepAlive(authenticationResult) {
this.setupFromSuccessfulAuthentication(authenticationResult);
this.keepAlive();
}
}, {
key: "isSessionSetup",
value: function isSessionSetup() {
if (!this.getCookie(UserSessionCookieManager.COOKIE_USERNAME) || !this.getCookie(UserSessionCookieManager.COOKIE_REFRESH_TOKEN) || !this.getCookie(UserSessionCookieManager.COOKIE_CREATION_TIME) || !this.getCookie(UserSessionCookieManager.COOKIE_AVAILABILITY_LENGTH) || !this.getCookie(UserSessionCookieManager.COOKIE_ID_TOKEN)) {
return false;
}
return true;
}
}, {
key: "isSessionSetupAndNotExpired",
value: function isSessionSetupAndNotExpired() {
return this.isSessionSetup() && !this.sessionExpired();
}
}, {
key: "keepAliveIfSetup",
value: function keepAliveIfSetup() {
if (true === this.isSessionSetup()) {
this.keepAlive();
}
}
}, {
key: "keepAliveIfSetupAndNotExpired",
value: function keepAliveIfSetupAndNotExpired() {
if (true === this.isSessionSetupAndNotExpired()) {
this.keepAlive();
}
}
}, {
key: "destroy",
value: function destroy() {
var unixEpoch = new Date(1970, 1, 1);
this.setUsername("");
this.setRefreshToken("");
this.setCreationTime(unixEpoch);
this.setAvailabilityLength(0);
this.setIdToken("");
}
}, {
key: "sessionExpired",
value: function sessionExpired() {
if (this.getExpirationTime() <= this.getCurrentTime()) {
return true;
}
return false;
}
}, {
key: "getExpirationTime",
value: function getExpirationTime() {
return new Date(this.getCreationTime().getTime() + this.getAvailabilityLength() - this.earlyRefreshPeriod);
}
}, {
key: "generateIntervalDuration",
value: function generateIntervalDuration() {
var minValue = 10 * 60 * 1000; /* 10 minutes, in ms */
var maxValue = 29 * 60 * 1000; /* 29 minutes, in ms */
//
// To generate a random value between a specified range (a, b),
// using an random number generator (RNG) with an uniform
// distribution on (0, 1), the following formula is used:
//
// (b - a) * RNG() + a
//
this._intervalDuration = Math.floor((maxValue - minValue) * Math.random() + minValue);
}
}, {
key: "mustRefresh",
value: function mustRefresh() {
return this.getExpirationTime().getTime() - this.getCurrentTime().getTime() - this.intervalDuration < 0;
}
}, {
key: "refreshSession",
value: function refreshSession() {
var _this = this;
var userPool = this.createCognitoUserPool(this.cognitoPoolId, this.cognitoClientId);
var user = this.createCognitoUser(this.getUsername(), userPool);
var refreshToken = this.createCognitoRefreshToken(this.getRefreshToken());
user.refreshSession(refreshToken, function (err, data) {
if (err) {
throw err;
}
_this.updateCookie(data.getIdToken().getJwtToken(), 3600000);
});
}
}, {
key: "refreshSessionIfNeeded",
value: function refreshSessionIfNeeded() {
if (true === this.mustRefresh()) {
this.refreshSession();
}
}
}, {
key: "createCognitoUserPool",
value: function createCognitoUserPool(userPoolId, clientId) {
return new CognitoUserPool({ UserPoolId: userPoolId, ClientId: clientId });
}
}, {
key: "createCognitoUser",
value: function createCognitoUser(username, pool) {
return new CognitoUser({ Username: username, Pool: pool });
}
}, {
key: "createCognitoRefreshToken",
value: function createCognitoRefreshToken(refreshToken) {
return new CognitoRefreshToken({ RefreshToken: refreshToken });
}
}, {
key: "setInterval",
value: function (_setInterval) {
function setInterval(_x, _x2) {
return _setInterval.apply(this, arguments);
}
setInterval.toString = function () {
return _setInterval.toString();
};
return setInterval;
}(function (callback, ms) {
var self = this;
setInterval(function () {
console.log("Session refresh attempt");
callback.call(self);
}, ms);
})
}, {
key: "getCurrentTime",
value: function getCurrentTime() {
return new Date();
}
}]);
return UserSessionCookieManager;
}();
UserSessionCookieManager.COOKIE_USERNAME = 'hs-sso-usr';
UserSessionCookieManager.COOKIE_REFRESH_TOKEN = 'hs-sso-token-refresh';
UserSessionCookieManager.COOKIE_CREATION_TIME = 'hs-sso-token-id-crt';
UserSessionCookieManager.COOKIE_AVAILABILITY_LENGTH = 'hs-sso-token-id-av';
UserSessionCookieManager.COOKIE_ID_TOKEN = 'hs-sso-token-id';
//# sourceMappingURL=user-session-cookie-manager.class.js.map
UserSessionCookieManager.EARLY_REFRESH_PERIOD_IN_MS = 10000;