1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.ClientInfo = void 0;
|
8 | var CryptoUtils_1 = require("./utils/CryptoUtils");
|
9 | var ClientAuthError_1 = require("./error/ClientAuthError");
|
10 | var StringUtils_1 = require("./utils/StringUtils");
|
11 |
|
12 |
|
13 |
|
14 | var ClientInfo = (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 |
|
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 |
|
74 | policy = urlSegments[1];
|
75 | }
|
76 | if (uidSegments[uidSegments.length - 1] === policy) {
|
77 |
|
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 | }());
|
88 | exports.ClientInfo = ClientInfo;
|
89 |
|
\ | No newline at end of file |