UNPKG

2.65 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License.
5 */
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.TokenUtils = void 0;
8var CryptoUtils_1 = require("./CryptoUtils");
9var StringUtils_1 = require("./StringUtils");
10var TimeUtils_1 = require("./TimeUtils");
11/**
12 * @hidden
13 */
14var TokenUtils = /** @class */ (function () {
15 function TokenUtils() {
16 }
17 /**
18 * decode a JWT
19 *
20 * @param jwtToken
21 */
22 TokenUtils.decodeJwt = function (jwtToken) {
23 if (StringUtils_1.StringUtils.isEmpty(jwtToken)) {
24 return null;
25 }
26 var idTokenPartsRegex = /^([^\.\s]*)\.([^\.\s]+)\.([^\.\s]*)$/;
27 var matches = idTokenPartsRegex.exec(jwtToken);
28 if (!matches || matches.length < 4) {
29 // this._requestContext.logger.warn("The returned id_token is not parseable.");
30 return null;
31 }
32 var crackedToken = {
33 header: matches[1],
34 JWSPayload: matches[2],
35 JWSSig: matches[3]
36 };
37 return crackedToken;
38 };
39 /**
40 * Evaluates whether token cache item expiration is within expiration offset range
41 * @param tokenCacheItem
42 */
43 TokenUtils.validateExpirationIsWithinOffset = function (expiration, tokenRenewalOffsetSeconds) {
44 var offset = tokenRenewalOffsetSeconds || 300;
45 return expiration && (expiration > TimeUtils_1.TimeUtils.now() + offset);
46 };
47 /**
48 * Extract IdToken by decoding the RAWIdToken
49 *
50 * @param encodedIdToken
51 */
52 TokenUtils.extractIdToken = function (encodedIdToken) {
53 // id token will be decoded to get the username
54 var decodedToken = this.decodeJwt(encodedIdToken);
55 if (!decodedToken) {
56 return null;
57 }
58 try {
59 var base64IdToken = decodedToken["JWSPayload"];
60 var base64Decoded = CryptoUtils_1.CryptoUtils.base64Decode(base64IdToken);
61 if (!base64Decoded) {
62 // this._requestContext.logger.info("The returned id_token could not be base64 url safe decoded.");
63 return null;
64 }
65 // ECMA script has JSON built-in support
66 return JSON.parse(base64Decoded);
67 }
68 catch (err) {
69 // this._requestContext.logger.error("The returned id_token could not be decoded" + err);
70 }
71 return null;
72 };
73 return TokenUtils;
74}());
75exports.TokenUtils = TokenUtils;
76//# sourceMappingURL=TokenUtils.js.map
\No newline at end of file