UNPKG

3.42 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.ClientInfo = void 0;
8var CryptoUtils_1 = require("./utils/CryptoUtils");
9var ClientAuthError_1 = require("./error/ClientAuthError");
10var StringUtils_1 = require("./utils/StringUtils");
11/**
12 * @hidden
13 */
14var ClientInfo = /** @class */ (function () {
15 function ClientInfo(rawClientInfo, authority) {
16 if (!rawClientInfo || StringUtils_1.StringUtils.isEmpty(rawClientInfo)) {
17 this.uid = "";
18 this.utid = "";
19 return;
20 }
21 try {
22 var decodedClientInfo = CryptoUtils_1.CryptoUtils.base64Decode(rawClientInfo);
23 var clientInfo = JSON.parse(decodedClientInfo);
24 if (clientInfo) {
25 if (clientInfo.hasOwnProperty("uid")) {
26 this.uid = authority ? ClientInfo.stripPolicyFromUid(clientInfo.uid, authority) : clientInfo.uid;
27 }
28 if (clientInfo.hasOwnProperty("utid")) {
29 this.utid = clientInfo.utid;
30 }
31 }
32 }
33 catch (e) {
34 throw ClientAuthError_1.ClientAuthError.createClientInfoDecodingError(e);
35 }
36 }
37 Object.defineProperty(ClientInfo.prototype, "uid", {
38 get: function () {
39 return this._uid ? this._uid : "";
40 },
41 set: function (uid) {
42 this._uid = uid;
43 },
44 enumerable: false,
45 configurable: true
46 });
47 Object.defineProperty(ClientInfo.prototype, "utid", {
48 get: function () {
49 return this._utid ? this._utid : "";
50 },
51 set: function (utid) {
52 this._utid = utid;
53 },
54 enumerable: false,
55 configurable: true
56 });
57 ClientInfo.createClientInfoFromIdToken = function (idToken, authority) {
58 var clientInfo = {
59 uid: idToken.subject,
60 utid: ""
61 };
62 return new ClientInfo(CryptoUtils_1.CryptoUtils.base64Encode(JSON.stringify(clientInfo)), authority);
63 };
64 ClientInfo.stripPolicyFromUid = function (uid, authority) {
65 var uidSegments = uid.split("-");
66 // Reverse the url segments so the last one is more easily accessible
67 var urlSegments = authority.split("/").reverse();
68 var policy = "";
69 if (!StringUtils_1.StringUtils.isEmpty(urlSegments[0])) {
70 policy = urlSegments[0];
71 }
72 else if (urlSegments.length > 1) {
73 // If the original url had a trailing slash, urlSegments[0] would be "" so take the next element
74 policy = urlSegments[1];
75 }
76 if (uidSegments[uidSegments.length - 1] === policy) {
77 // If the last segment of uid matches the last segment of authority url, remove the last segment of uid
78 return uidSegments.slice(0, uidSegments.length - 1).join("-");
79 }
80 return uid;
81 };
82 ClientInfo.prototype.encodeClientInfo = function () {
83 var clientInfo = JSON.stringify({ uid: this.uid, utid: this.utid });
84 return CryptoUtils_1.CryptoUtils.base64Encode(clientInfo);
85 };
86 return ClientInfo;
87}());
88exports.ClientInfo = ClientInfo;
89//# sourceMappingURL=ClientInfo.js.map
\No newline at end of file