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 | import { Buffer } from 'buffer';
|
19 |
|
20 | /** @class */
|
21 | export default class CognitoJwtToken {
|
22 | /**
|
23 | * Constructs a new CognitoJwtToken object
|
24 | * @param {string=} token The JWT token.
|
25 | */
|
26 | constructor(token) {
|
27 | // Assign object
|
28 | this.jwtToken = token || '';
|
29 | this.payload = this.decodePayload();
|
30 | }
|
31 |
|
32 | /**
|
33 | * @returns {string} the record's token.
|
34 | */
|
35 | getJwtToken() {
|
36 | return this.jwtToken;
|
37 | }
|
38 |
|
39 | /**
|
40 | * @returns {int} the token's expiration (exp member).
|
41 | */
|
42 | getExpiration() {
|
43 | return this.payload.exp;
|
44 | }
|
45 |
|
46 | /**
|
47 | * @returns {int} the token's "issued at" (iat member).
|
48 | */
|
49 | getIssuedAt() {
|
50 | return this.payload.iat;
|
51 | }
|
52 |
|
53 | /**
|
54 | * @returns {object} the token's payload.
|
55 | */
|
56 | decodePayload() {
|
57 | const payload = this.jwtToken.split('.')[1];
|
58 | try {
|
59 | return JSON.parse(Buffer.from(payload, 'base64').toString('utf8'));
|
60 | } catch (err) {
|
61 | return {};
|
62 | }
|
63 | }
|
64 | }
|