UNPKG

1.76 kBJavaScriptView Raw
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 */
17import { Buffer } from 'buffer';
18/** @class */
19
20var CognitoJwtToken = /*#__PURE__*/function () {
21 /**
22 * Constructs a new CognitoJwtToken object
23 * @param {string=} token The JWT token.
24 */
25 function CognitoJwtToken(token) {
26 // Assign object
27 this.jwtToken = token || '';
28 this.payload = this.decodePayload();
29 }
30 /**
31 * @returns {string} the record's token.
32 */
33
34
35 var _proto = CognitoJwtToken.prototype;
36
37 _proto.getJwtToken = function getJwtToken() {
38 return this.jwtToken;
39 }
40 /**
41 * @returns {int} the token's expiration (exp member).
42 */
43 ;
44
45 _proto.getExpiration = function getExpiration() {
46 return this.payload.exp;
47 }
48 /**
49 * @returns {int} the token's "issued at" (iat member).
50 */
51 ;
52
53 _proto.getIssuedAt = function getIssuedAt() {
54 return this.payload.iat;
55 }
56 /**
57 * @returns {object} the token's payload.
58 */
59 ;
60
61 _proto.decodePayload = function decodePayload() {
62 var payload = this.jwtToken.split('.')[1];
63
64 try {
65 return JSON.parse(Buffer.from(payload, 'base64').toString('utf8'));
66 } catch (err) {
67 return {};
68 }
69 };
70
71 return CognitoJwtToken;
72}();
73
74export { CognitoJwtToken as default };
\No newline at end of file