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