UNPKG

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