@microsoft/dev-tunnels-management 1.3.6 → 1.3.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,150 +1,150 @@
1
- "use strict";
2
- // Copyright (c) Microsoft Corporation.
3
- // Licensed under the MIT license.
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.TunnelAccessTokenProperties = void 0;
6
- /**
7
- * Supports parsing tunnel access token JWT properties to allow for some pre-validation
8
- * and diagnostics.
9
- *
10
- * Applications generally should not attempt to interpret or rely on any token properties
11
- * other than `expiration`, because the service may change or omit those claims in the future.
12
- * Other claims are exposed here only for diagnostic purposes.
13
- */
14
- class TunnelAccessTokenProperties {
15
- constructor(clusterId, tunnelId, tunnelPorts, scopes, issuer, expiration) {
16
- this.clusterId = clusterId;
17
- this.tunnelId = tunnelId;
18
- this.tunnelPorts = tunnelPorts;
19
- this.scopes = scopes;
20
- this.issuer = issuer;
21
- this.expiration = expiration;
22
- }
23
- toString() {
24
- let s = '';
25
- if (this.tunnelId) {
26
- s += 'tunnel=';
27
- s += this.tunnelId;
28
- if (this.clusterId) {
29
- s += '.';
30
- s += this.clusterId;
31
- }
32
- }
33
- if (this.tunnelPorts && this.tunnelPorts.length > 0) {
34
- if (s.length > 0)
35
- s += ', ';
36
- if (this.tunnelPorts.length === 1) {
37
- s += `port=${this.tunnelPorts[0]}`;
38
- }
39
- else {
40
- s += `ports=[${this.tunnelPorts.join(', ')}]`;
41
- }
42
- }
43
- if (this.scopes) {
44
- if (s.length > 0)
45
- s += ', ';
46
- s += `scopes=[${this.scopes.join(', ')}]`;
47
- }
48
- if (this.issuer) {
49
- if (s.length > 0)
50
- s += ', ';
51
- s += 'issuer=';
52
- s += this.issuer;
53
- }
54
- if (this.expiration) {
55
- if (s.length > 0)
56
- s += ', ';
57
- s += `expiration=${this.expiration.toString().replace('.000Z', 'Z')}`;
58
- }
59
- return s;
60
- }
61
- /**
62
- * Checks if the tunnel access token expiration claim is in the past.
63
- * Note: uses client's system time for the validation.
64
- * (Does not throw if the token is an invalid format.)
65
- */
66
- static validateTokenExpiration(token) {
67
- const t = TunnelAccessTokenProperties.tryParse(token);
68
- if (t === null || t === void 0 ? void 0 : t.expiration) {
69
- if (t.expiration < new Date()) {
70
- throw new Error('The access token is expired: ' + t);
71
- }
72
- }
73
- }
74
- /**
75
- * Attempts to parse a tunnel access token (JWT). This does NOT validate the token
76
- * signature or any claims.
77
- */
78
- static tryParse(token) {
79
- if (typeof token !== 'string')
80
- throw new TypeError('Token string expected.');
81
- // JWTs are encoded in 3 parts: header, body, and signature.
82
- const tokenParts = token.split('.');
83
- if (tokenParts.length !== 3) {
84
- return null;
85
- }
86
- const tokenBodyJson = TunnelAccessTokenProperties.base64UrlDecode(tokenParts[1]);
87
- if (!tokenBodyJson) {
88
- return null;
89
- }
90
- try {
91
- const tokenJson = JSON.parse(tokenBodyJson);
92
- const clusterId = tokenJson.clusterId;
93
- const tunnelId = tokenJson.tunnelId;
94
- const ports = tokenJson.tunnelPorts;
95
- const scp = tokenJson.scp;
96
- const iss = tokenJson.iss;
97
- const exp = tokenJson.exp;
98
- return new TunnelAccessTokenProperties(clusterId, tunnelId, typeof ports === 'number' ? [ports] : ports, scp === null || scp === void 0 ? void 0 : scp.split(' '), iss, typeof exp === 'number' ? new Date(exp * 1000) : undefined);
99
- }
100
- catch (_a) {
101
- return null;
102
- }
103
- }
104
- /**
105
- * Gets the tunnal access token trace string.
106
- * 'none' if null or undefined, parsed token info if can be parsed, or 'token' if cannot be parsed.
107
- */
108
- static getTokenTrace(token) {
109
- var _a, _b;
110
- return !token ? 'none' : (_b = (_a = TunnelAccessTokenProperties.tryParse(token)) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : 'token';
111
- }
112
- /**
113
- * Gets a tunnel access token that matches any of the provided access token scopes.
114
- * @param tunnel The tunnel to get the access tokens from.
115
- * @param accessTokenScopes What scopes the token needs to have.
116
- * @returns Tunnel access token if found; otherwise, undefined.
117
- */
118
- static getTunnelAccessToken(tunnel, accessTokenScopes) {
119
- if (!(tunnel === null || tunnel === void 0 ? void 0 : tunnel.accessTokens) || !accessTokenScopes) {
120
- return;
121
- }
122
- if (!Array.isArray(accessTokenScopes)) {
123
- accessTokenScopes = [accessTokenScopes];
124
- }
125
- for (const scope of accessTokenScopes) {
126
- for (const [key, accessToken] of Object.entries(tunnel.accessTokens)) {
127
- // Each key may be either a single scope or space-delimited list of scopes.
128
- if (accessToken && key.split(' ').includes(scope)) {
129
- return accessToken;
130
- }
131
- }
132
- }
133
- }
134
- static base64UrlDecode(encodedString) {
135
- // Convert from base64url encoding to base64 encoding: replace chars and add padding.
136
- encodedString = encodedString.replace('-', '+');
137
- while (encodedString.length % 4 !== 0) {
138
- encodedString += '=';
139
- }
140
- try {
141
- const result = atob(encodedString);
142
- return result;
143
- }
144
- catch (_a) {
145
- return null;
146
- }
147
- }
148
- }
149
- exports.TunnelAccessTokenProperties = TunnelAccessTokenProperties;
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation.
3
+ // Licensed under the MIT license.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.TunnelAccessTokenProperties = void 0;
6
+ /**
7
+ * Supports parsing tunnel access token JWT properties to allow for some pre-validation
8
+ * and diagnostics.
9
+ *
10
+ * Applications generally should not attempt to interpret or rely on any token properties
11
+ * other than `expiration`, because the service may change or omit those claims in the future.
12
+ * Other claims are exposed here only for diagnostic purposes.
13
+ */
14
+ class TunnelAccessTokenProperties {
15
+ constructor(clusterId, tunnelId, tunnelPorts, scopes, issuer, expiration) {
16
+ this.clusterId = clusterId;
17
+ this.tunnelId = tunnelId;
18
+ this.tunnelPorts = tunnelPorts;
19
+ this.scopes = scopes;
20
+ this.issuer = issuer;
21
+ this.expiration = expiration;
22
+ }
23
+ toString() {
24
+ let s = '';
25
+ if (this.tunnelId) {
26
+ s += 'tunnel=';
27
+ s += this.tunnelId;
28
+ if (this.clusterId) {
29
+ s += '.';
30
+ s += this.clusterId;
31
+ }
32
+ }
33
+ if (this.tunnelPorts && this.tunnelPorts.length > 0) {
34
+ if (s.length > 0)
35
+ s += ', ';
36
+ if (this.tunnelPorts.length === 1) {
37
+ s += `port=${this.tunnelPorts[0]}`;
38
+ }
39
+ else {
40
+ s += `ports=[${this.tunnelPorts.join(', ')}]`;
41
+ }
42
+ }
43
+ if (this.scopes) {
44
+ if (s.length > 0)
45
+ s += ', ';
46
+ s += `scopes=[${this.scopes.join(', ')}]`;
47
+ }
48
+ if (this.issuer) {
49
+ if (s.length > 0)
50
+ s += ', ';
51
+ s += 'issuer=';
52
+ s += this.issuer;
53
+ }
54
+ if (this.expiration) {
55
+ if (s.length > 0)
56
+ s += ', ';
57
+ s += `expiration=${this.expiration.toString().replace('.000Z', 'Z')}`;
58
+ }
59
+ return s;
60
+ }
61
+ /**
62
+ * Checks if the tunnel access token expiration claim is in the past.
63
+ * Note: uses client's system time for the validation.
64
+ * (Does not throw if the token is an invalid format.)
65
+ */
66
+ static validateTokenExpiration(token) {
67
+ const t = TunnelAccessTokenProperties.tryParse(token);
68
+ if (t === null || t === void 0 ? void 0 : t.expiration) {
69
+ if (t.expiration < new Date()) {
70
+ throw new Error('The access token is expired: ' + t);
71
+ }
72
+ }
73
+ }
74
+ /**
75
+ * Attempts to parse a tunnel access token (JWT). This does NOT validate the token
76
+ * signature or any claims.
77
+ */
78
+ static tryParse(token) {
79
+ if (typeof token !== 'string')
80
+ throw new TypeError('Token string expected.');
81
+ // JWTs are encoded in 3 parts: header, body, and signature.
82
+ const tokenParts = token.split('.');
83
+ if (tokenParts.length !== 3) {
84
+ return null;
85
+ }
86
+ const tokenBodyJson = TunnelAccessTokenProperties.base64UrlDecode(tokenParts[1]);
87
+ if (!tokenBodyJson) {
88
+ return null;
89
+ }
90
+ try {
91
+ const tokenJson = JSON.parse(tokenBodyJson);
92
+ const clusterId = tokenJson.clusterId;
93
+ const tunnelId = tokenJson.tunnelId;
94
+ const ports = tokenJson.tunnelPorts;
95
+ const scp = tokenJson.scp;
96
+ const iss = tokenJson.iss;
97
+ const exp = tokenJson.exp;
98
+ return new TunnelAccessTokenProperties(clusterId, tunnelId, typeof ports === 'number' ? [ports] : ports, scp === null || scp === void 0 ? void 0 : scp.split(' '), iss, typeof exp === 'number' ? new Date(exp * 1000) : undefined);
99
+ }
100
+ catch (_a) {
101
+ return null;
102
+ }
103
+ }
104
+ /**
105
+ * Gets the tunnal access token trace string.
106
+ * 'none' if null or undefined, parsed token info if can be parsed, or 'token' if cannot be parsed.
107
+ */
108
+ static getTokenTrace(token) {
109
+ var _a, _b;
110
+ return !token ? 'none' : (_b = (_a = TunnelAccessTokenProperties.tryParse(token)) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : 'token';
111
+ }
112
+ /**
113
+ * Gets a tunnel access token that matches any of the provided access token scopes.
114
+ * @param tunnel The tunnel to get the access tokens from.
115
+ * @param accessTokenScopes What scopes the token needs to have.
116
+ * @returns Tunnel access token if found; otherwise, undefined.
117
+ */
118
+ static getTunnelAccessToken(tunnel, accessTokenScopes) {
119
+ if (!(tunnel === null || tunnel === void 0 ? void 0 : tunnel.accessTokens) || !accessTokenScopes) {
120
+ return;
121
+ }
122
+ if (!Array.isArray(accessTokenScopes)) {
123
+ accessTokenScopes = [accessTokenScopes];
124
+ }
125
+ for (const scope of accessTokenScopes) {
126
+ for (const [key, accessToken] of Object.entries(tunnel.accessTokens)) {
127
+ // Each key may be either a single scope or space-delimited list of scopes.
128
+ if (accessToken && key.split(' ').includes(scope)) {
129
+ return accessToken;
130
+ }
131
+ }
132
+ }
133
+ }
134
+ static base64UrlDecode(encodedString) {
135
+ // Convert from base64url encoding to base64 encoding: replace chars and add padding.
136
+ encodedString = encodedString.replace('-', '+');
137
+ while (encodedString.length % 4 !== 0) {
138
+ encodedString += '=';
139
+ }
140
+ try {
141
+ const result = atob(encodedString);
142
+ return result;
143
+ }
144
+ catch (_a) {
145
+ return null;
146
+ }
147
+ }
148
+ }
149
+ exports.TunnelAccessTokenProperties = TunnelAccessTokenProperties;
150
150
  //# sourceMappingURL=tunnelAccessTokenProperties.js.map