amazon-cognito-identity-js
Version:
Amazon Cognito Identity Provider JavaScript SDK
117 lines (88 loc) • 3.41 kB
JavaScript
exports.__esModule = true;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/*!
* Copyright 2016 Amazon.com,
* Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the
* License. A copy of the License is located at
*
* http://aws.amazon.com/asl/
*
* or in the "license" file accompanying this file. This file is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, express or implied. See the License
* for the specific language governing permissions and
* limitations under the License.
*/
/** @class */
var CognitoUserSession = function () {
/**
* Constructs a new CognitoUserSession object
* @param {CognitoIdToken} IdToken The session's Id token.
* @param {CognitoRefreshToken=} RefreshToken The session's refresh token.
* @param {CognitoAccessToken} AccessToken The session's access token.
* @param {int} ClockDrift The saved computer's clock drift or undefined to force calculation.
*/
function CognitoUserSession() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
IdToken = _ref.IdToken,
RefreshToken = _ref.RefreshToken,
AccessToken = _ref.AccessToken,
ClockDrift = _ref.ClockDrift;
_classCallCheck(this, CognitoUserSession);
if (AccessToken == null || IdToken == null) {
throw new Error('Id token and Access Token must be present.');
}
this.idToken = IdToken;
this.refreshToken = RefreshToken;
this.accessToken = AccessToken;
this.clockDrift = ClockDrift === undefined ? this.calculateClockDrift() : ClockDrift;
}
/**
* @returns {CognitoIdToken} the session's Id token
*/
CognitoUserSession.prototype.getIdToken = function getIdToken() {
return this.idToken;
};
/**
* @returns {CognitoRefreshToken} the session's refresh token
*/
CognitoUserSession.prototype.getRefreshToken = function getRefreshToken() {
return this.refreshToken;
};
/**
* @returns {CognitoAccessToken} the session's access token
*/
CognitoUserSession.prototype.getAccessToken = function getAccessToken() {
return this.accessToken;
};
/**
* @returns {int} the session's clock drift
*/
CognitoUserSession.prototype.getClockDrift = function getClockDrift() {
return this.clockDrift;
};
/**
* @returns {int} the computer's clock drift
*/
CognitoUserSession.prototype.calculateClockDrift = function calculateClockDrift() {
var now = Math.floor(new Date() / 1000);
var iat = Math.min(this.accessToken.getIssuedAt(), this.idToken.getIssuedAt());
return now - iat;
};
/**
* Checks to see if the session is still valid based on session expiry information found
* in tokens and the current time (adjusted with clock drift)
* @returns {boolean} if the session is still valid
*/
CognitoUserSession.prototype.isValid = function isValid() {
var now = Math.floor(new Date() / 1000);
var adjusted = now - this.clockDrift;
return adjusted < this.accessToken.getExpiration() && adjusted < this.idToken.getExpiration();
};
return CognitoUserSession;
}();
exports.default = CognitoUserSession;
;