msal
Version:
Microsoft Authentication Library for js
64 lines • 2.17 kB
JavaScript
;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
var CryptoUtils_1 = require("./CryptoUtils");
var StringUtils_1 = require("./StringUtils");
/**
* @hidden
*/
var TokenUtils = /** @class */ (function () {
function TokenUtils() {
}
/**
* decode a JWT
*
* @param jwtToken
*/
TokenUtils.decodeJwt = function (jwtToken) {
if (StringUtils_1.StringUtils.isEmpty(jwtToken)) {
return null;
}
var idTokenPartsRegex = /^([^\.\s]*)\.([^\.\s]+)\.([^\.\s]*)$/;
var matches = idTokenPartsRegex.exec(jwtToken);
if (!matches || matches.length < 4) {
//this._requestContext.logger.warn("The returned id_token is not parseable.");
return null;
}
var crackedToken = {
header: matches[1],
JWSPayload: matches[2],
JWSSig: matches[3]
};
return crackedToken;
};
/**
* Extract IdToken by decoding the RAWIdToken
*
* @param encodedIdToken
*/
TokenUtils.extractIdToken = function (encodedIdToken) {
// id token will be decoded to get the username
var decodedToken = this.decodeJwt(encodedIdToken);
if (!decodedToken) {
return null;
}
try {
var base64IdToken = decodedToken.JWSPayload;
var base64Decoded = CryptoUtils_1.CryptoUtils.base64Decode(base64IdToken);
if (!base64Decoded) {
//this._requestContext.logger.info("The returned id_token could not be base64 url safe decoded.");
return null;
}
// ECMA script has JSON built-in support
return JSON.parse(base64Decoded);
}
catch (err) {
//this._requestContext.logger.error("The returned id_token could not be decoded" + err);
}
return null;
};
return TokenUtils;
}());
exports.TokenUtils = TokenUtils;
//# sourceMappingURL=TokenUtils.js.map