UNPKG

3.13 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = void 0;
5
6/*!
7 * Copyright 2016 Amazon.com,
8 * Inc. or its affiliates. All Rights Reserved.
9 *
10 * Licensed under the Amazon Software License (the "License").
11 * You may not use this file except in compliance with the
12 * License. A copy of the License is located at
13 *
14 * http://aws.amazon.com/asl/
15 *
16 * or in the "license" file accompanying this file. This file is
17 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
18 * CONDITIONS OF ANY KIND, express or implied. See the License
19 * for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23/** @class */
24var CognitoUserSession = /*#__PURE__*/function () {
25 /**
26 * Constructs a new CognitoUserSession object
27 * @param {CognitoIdToken} IdToken The session's Id token.
28 * @param {CognitoRefreshToken=} RefreshToken The session's refresh token.
29 * @param {CognitoAccessToken} AccessToken The session's access token.
30 * @param {int} ClockDrift The saved computer's clock drift or undefined to force calculation.
31 */
32 function CognitoUserSession(_temp) {
33 var _ref = _temp === void 0 ? {} : _temp,
34 IdToken = _ref.IdToken,
35 RefreshToken = _ref.RefreshToken,
36 AccessToken = _ref.AccessToken,
37 ClockDrift = _ref.ClockDrift;
38
39 if (AccessToken == null || IdToken == null) {
40 throw new Error('Id token and Access Token must be present.');
41 }
42
43 this.idToken = IdToken;
44 this.refreshToken = RefreshToken;
45 this.accessToken = AccessToken;
46 this.clockDrift = ClockDrift === undefined ? this.calculateClockDrift() : ClockDrift;
47 }
48 /**
49 * @returns {CognitoIdToken} the session's Id token
50 */
51
52
53 var _proto = CognitoUserSession.prototype;
54
55 _proto.getIdToken = function getIdToken() {
56 return this.idToken;
57 }
58 /**
59 * @returns {CognitoRefreshToken} the session's refresh token
60 */
61 ;
62
63 _proto.getRefreshToken = function getRefreshToken() {
64 return this.refreshToken;
65 }
66 /**
67 * @returns {CognitoAccessToken} the session's access token
68 */
69 ;
70
71 _proto.getAccessToken = function getAccessToken() {
72 return this.accessToken;
73 }
74 /**
75 * @returns {int} the session's clock drift
76 */
77 ;
78
79 _proto.getClockDrift = function getClockDrift() {
80 return this.clockDrift;
81 }
82 /**
83 * @returns {int} the computer's clock drift
84 */
85 ;
86
87 _proto.calculateClockDrift = function calculateClockDrift() {
88 var now = Math.floor(new Date() / 1000);
89 var iat = Math.min(this.accessToken.getIssuedAt(), this.idToken.getIssuedAt());
90 return now - iat;
91 }
92 /**
93 * Checks to see if the session is still valid based on session expiry information found
94 * in tokens and the current time (adjusted with clock drift)
95 * @returns {boolean} if the session is still valid
96 */
97 ;
98
99 _proto.isValid = function isValid() {
100 var now = Math.floor(new Date() / 1000);
101 var adjusted = now - this.clockDrift;
102 return adjusted < this.accessToken.getExpiration() && adjusted < this.idToken.getExpiration();
103 };
104
105 return CognitoUserSession;
106}();
107
108exports["default"] = CognitoUserSession;
\No newline at end of file