• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

box / box-typescript-sdk-gen / 8298652836

15 Mar 2024 03:31PM UTC coverage: 43.864% (+1.9%) from 42.003%
8298652836

Pull #97

github

web-flow
Merge ef32e8277 into 14e115481
Pull Request #97: feat: Support `additionalProperties` of type any in Python and TS (box/box-codegen#453)

2288 of 9072 branches covered (25.22%)

Branch coverage included in aggregate %.

355 of 416 new or added lines in 17 files covered. (85.34%)

186 existing lines in 5 files now uncovered.

7226 of 12618 relevant lines covered (57.27%)

68.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/src/box/jwtAuth.generated.ts
1
import { PostOAuth2TokenGrantTypeField } from '../schemas.generated.js';
2
import { PostOAuth2TokenSubjectTokenTypeField } from '../schemas.generated.js';
3
import { Authentication } from '../networking/auth.generated.js';
4
import { NetworkSession } from '../networking/network.generated.js';
5
import { AccessToken } from '../schemas.generated.js';
6
import { PostOAuth2Token } from '../schemas.generated.js';
7
import { PostOAuth2Revoke } from '../schemas.generated.js';
8
import { TokenStorage } from './tokenStorage.generated.js';
9
import { InMemoryTokenStorage } from './tokenStorage.generated.js';
136✔
10
import { jsonToSerializedData } from '../serialization/json.js';
136✔
11
import { SerializedData } from '../serialization/json.js';
12
import { getUuid } from '../internal/utils.js';
136✔
13
import { readTextFromFile } from '../internal/utils.js';
136✔
14
import { isBrowser } from '../internal/utils.js';
136✔
15
import { getEpochTimeInSeconds } from '../internal/utils.js';
136✔
16
import { createJwtAssertion } from '../internal/utils.js';
136✔
17
import { JwtSignOptions } from '../internal/utils.js';
18
import { JwtKey } from '../internal/utils.js';
19
import { JwtAlgorithm } from '../internal/utils.js';
20
import { AuthorizationManager } from '../managers/authorization.generated.js';
136✔
21
import { BoxSdkError } from './errors.js';
136✔
22
import { sdIsEmpty } from '../serialization/json.js';
23
import { sdIsBoolean } from '../serialization/json.js';
24
import { sdIsNumber } from '../serialization/json.js';
25
import { sdIsString } from '../serialization/json.js';
26
import { sdIsList } from '../serialization/json.js';
27
import { sdIsMap } from '../serialization/json.js';
28
export interface JwtConfigAppSettingsAppAuth {
29
  readonly publicKeyId: string;
30
  readonly privateKey: string;
31
  readonly passphrase: string;
32
}
33
export interface JwtConfigAppSettings {
34
  readonly clientId: string;
35
  readonly clientSecret: string;
36
  readonly appAuth: JwtConfigAppSettingsAppAuth;
37
}
38
export interface JwtConfigFile {
39
  readonly enterpriseId?: string;
40
  readonly userId?: string;
41
  readonly boxAppSettings: JwtConfigAppSettings;
42
}
43
export class JwtConfig {
136✔
44
  readonly clientId!: string;
45
  readonly clientSecret!: string;
46
  readonly jwtKeyId!: string;
47
  readonly privateKey!: string;
48
  readonly privateKeyPassphrase!: string;
49
  readonly enterpriseId?: string;
50
  readonly userId?: string;
51
  readonly algorithm?: JwtAlgorithm = 'RS256' as JwtAlgorithm;
272✔
52
  readonly tokenStorage: TokenStorage = new InMemoryTokenStorage({});
272✔
53
  constructor(
54
    fields:
55
      | Omit<
56
          JwtConfig,
57
          | 'algorithm'
58
          | 'tokenStorage'
59
          | 'fromConfigJsonString'
60
          | 'fromConfigFile'
61
        >
62
      | Partial<Pick<JwtConfig, 'algorithm' | 'tokenStorage'>>
63
  ) {
64
    Object.assign(this, fields);
272✔
65
  }
66
  static fromConfigJsonString(
67
    configJsonString: string,
68
    tokenStorage?: TokenStorage
69
  ): JwtConfig {
70
    const configJson: JwtConfigFile = deserializeJwtConfigFile(
230✔
71
      jsonToSerializedData(configJsonString)
72
    );
73
    const newConfig: JwtConfig = !(tokenStorage == void 0)
230!
74
      ? new JwtConfig({
75
          clientId: configJson.boxAppSettings.clientId,
76
          clientSecret: configJson.boxAppSettings.clientSecret,
77
          enterpriseId: configJson.enterpriseId,
78
          userId: configJson.userId,
79
          jwtKeyId: configJson.boxAppSettings.appAuth.publicKeyId,
80
          privateKey: configJson.boxAppSettings.appAuth.privateKey,
81
          privateKeyPassphrase: configJson.boxAppSettings.appAuth.passphrase,
82
          tokenStorage: tokenStorage,
83
        })
84
      : new JwtConfig({
85
          clientId: configJson.boxAppSettings.clientId,
86
          clientSecret: configJson.boxAppSettings.clientSecret,
87
          enterpriseId: configJson.enterpriseId,
88
          userId: configJson.userId,
89
          jwtKeyId: configJson.boxAppSettings.appAuth.publicKeyId,
90
          privateKey: configJson.boxAppSettings.appAuth.privateKey,
91
          privateKeyPassphrase: configJson.boxAppSettings.appAuth.passphrase,
92
        });
93
    return newConfig;
230✔
94
  }
95
  static fromConfigFile(
96
    configFilePath: string,
97
    tokenStorage?: TokenStorage
98
  ): JwtConfig {
99
    const configJsonString: string = readTextFromFile(configFilePath);
×
100
    return JwtConfig.fromConfigJsonString(configJsonString, tokenStorage);
×
101
  }
102
}
103
export class BoxJwtAuth implements Authentication {
136✔
104
  readonly config!: JwtConfig;
105
  readonly tokenStorage: TokenStorage;
106
  readonly subjectId?: string;
107
  readonly subjectType?: string;
108
  constructor(
109
    fields: Omit<
110
      BoxJwtAuth,
111
      | 'tokenStorage'
112
      | 'subjectId'
113
      | 'subjectType'
114
      | 'refreshToken'
115
      | 'retrieveToken'
116
      | 'retrieveAuthorizationHeader'
117
      | 'asUser'
118
      | 'asEnterprise'
119
      | 'downscopeToken'
120
      | 'revokeToken'
121
    >
122
  ) {
123
    Object.assign(this, fields);
272✔
124
    this.tokenStorage =
272✔
125
      this.config.tokenStorage == void 0
272!
126
        ? new InMemoryTokenStorage({})
127
        : this.config.tokenStorage;
128
    this.subjectId = !(this.config.enterpriseId == void 0)
272✔
129
      ? this.config.enterpriseId
130
      : this.config.userId;
131
    this.subjectType = !(this.config.enterpriseId == void 0)
272✔
132
      ? 'enterprise'
133
      : 'user';
134
  }
135
  async refreshToken(networkSession?: NetworkSession): Promise<AccessToken> {
136
    if (isBrowser()) {
225!
137
      throw new BoxSdkError({
×
138
        message: 'JWT auth is not supported in browser environment.',
139
      });
140
    }
141
    const alg: JwtAlgorithm = !(this.config.algorithm == void 0)
225!
142
      ? this.config.algorithm!
143
      : ('RS256' as JwtAlgorithm);
144
    const claims: {
145
      readonly [key: string]: any;
146
    } = {
225✔
147
      ['exp']: getEpochTimeInSeconds() + 30,
148
      ['box_sub_type']: this.subjectType,
149
    };
150
    const jwtOptions: JwtSignOptions = {
225✔
151
      algorithm: alg,
152
      audience: 'https://api.box.com/oauth2/token',
153
      subject: this.subjectId,
154
      issuer: this.config.clientId,
155
      jwtid: getUuid(),
156
      keyid: this.config.jwtKeyId,
157
    } satisfies JwtSignOptions;
158
    const jwtKey: JwtKey = {
225✔
159
      key: this.config.privateKey,
160
      passphrase: this.config.privateKeyPassphrase,
161
    } satisfies JwtKey;
162
    const assertion: string = await createJwtAssertion(
225✔
163
      claims,
164
      jwtKey,
165
      jwtOptions
166
    );
167
    const authManager: AuthorizationManager = !(networkSession == void 0)
225✔
168
      ? new AuthorizationManager({ networkSession: networkSession })
169
      : new AuthorizationManager({});
170
    const token: AccessToken = await authManager.requestAccessToken({
225✔
171
      grantType:
172
        'urn:ietf:params:oauth:grant-type:jwt-bearer' as PostOAuth2TokenGrantTypeField,
173
      assertion: assertion,
174
      clientId: this.config.clientId,
175
      clientSecret: this.config.clientSecret,
176
    } satisfies PostOAuth2Token);
177
    await this.tokenStorage.store(token);
225✔
178
    return token;
225✔
179
  }
180
  async retrieveToken(networkSession?: NetworkSession): Promise<AccessToken> {
181
    const oldToken: undefined | AccessToken = await this.tokenStorage.get();
1,055✔
182
    if (oldToken == void 0) {
1,055✔
183
      const newToken: AccessToken = await this.refreshToken(networkSession);
224✔
184
      return newToken;
224✔
185
    }
186
    return oldToken;
831✔
187
  }
188
  async retrieveAuthorizationHeader(
189
    networkSession?: NetworkSession
190
  ): Promise<string> {
191
    const token: AccessToken = await this.retrieveToken(networkSession);
1,053✔
192
    return ''.concat('Bearer ', token.accessToken!) as string;
1,053✔
193
  }
194
  asUser(
195
    userId: string,
196
    tokenStorage: TokenStorage = new InMemoryTokenStorage({})
40✔
197
  ): BoxJwtAuth {
198
    const newConfig: JwtConfig = new JwtConfig({
40✔
199
      clientId: this.config.clientId,
200
      clientSecret: this.config.clientSecret,
201
      enterpriseId: void 0,
202
      userId: userId,
203
      jwtKeyId: this.config.jwtKeyId,
204
      privateKey: this.config.privateKey,
205
      privateKeyPassphrase: this.config.privateKeyPassphrase,
206
      tokenStorage: tokenStorage,
207
    });
208
    const newAuth: BoxJwtAuth = new BoxJwtAuth({ config: newConfig });
40✔
209
    return newAuth;
40✔
210
  }
211
  asEnterprise(
212
    userId: string,
213
    tokenStorage: TokenStorage = new InMemoryTokenStorage({})
2✔
214
  ): BoxJwtAuth {
215
    const newConfig: JwtConfig = new JwtConfig({
2✔
216
      clientId: this.config.clientId,
217
      clientSecret: this.config.clientSecret,
218
      enterpriseId: userId,
219
      userId: void 0,
220
      jwtKeyId: this.config.jwtKeyId,
221
      privateKey: this.config.privateKey,
222
      privateKeyPassphrase: this.config.privateKeyPassphrase,
223
      tokenStorage: tokenStorage,
224
    });
225
    const newAuth: BoxJwtAuth = new BoxJwtAuth({ config: newConfig });
2✔
226
    return newAuth;
2✔
227
  }
228
  async downscopeToken(
229
    scopes: readonly string[],
230
    resource?: string,
231
    sharedLink?: string,
232
    networkSession?: NetworkSession
233
  ): Promise<AccessToken> {
234
    const token: undefined | AccessToken = await this.tokenStorage.get();
2✔
235
    if (token == void 0) {
2!
236
      throw new BoxSdkError({
×
237
        message:
238
          'No access token is available. Make an API call to retrieve a token before calling this method.',
239
      });
240
    }
241
    const authManager: AuthorizationManager = !(networkSession == void 0)
2!
242
      ? new AuthorizationManager({ networkSession: networkSession })
243
      : new AuthorizationManager({});
244
    const downscopedToken: AccessToken = await authManager.requestAccessToken({
2✔
245
      grantType:
246
        'urn:ietf:params:oauth:grant-type:token-exchange' as PostOAuth2TokenGrantTypeField,
247
      subjectToken: token.accessToken,
248
      subjectTokenType:
249
        'urn:ietf:params:oauth:token-type:access_token' as PostOAuth2TokenSubjectTokenTypeField,
250
      resource: resource,
251
      scope: scopes.join(' ') as string,
252
      boxSharedLink: sharedLink,
253
    } satisfies PostOAuth2Token);
254
    return downscopedToken;
2✔
255
  }
256
  async revokeToken(networkSession?: NetworkSession): Promise<undefined> {
257
    const oldToken: undefined | AccessToken = await this.tokenStorage.get();
2✔
258
    if (oldToken == void 0) {
2!
259
      return void 0;
×
260
    }
261
    const authManager: AuthorizationManager = !(networkSession == void 0)
2!
262
      ? new AuthorizationManager({ networkSession: networkSession })
263
      : new AuthorizationManager({});
264
    await authManager.revokeAccessToken({
2✔
265
      token: oldToken.accessToken,
266
      clientId: this.config.clientId,
267
      clientSecret: this.config.clientSecret,
268
    } satisfies PostOAuth2Revoke);
269
    return await this.tokenStorage.clear();
2✔
270
  }
271
}
272
export function serializeJwtConfigAppSettingsAppAuth(val: any): SerializedData {
136✔
UNCOV
273
  return {
×
274
    ['publicKeyID']: val.publicKeyId,
275
    ['privateKey']: val.privateKey,
276
    ['passphrase']: val.passphrase,
277
  };
278
}
279
export function deserializeJwtConfigAppSettingsAppAuth(
136✔
280
  val: any
281
): JwtConfigAppSettingsAppAuth {
282
  const publicKeyId: string = val.publicKeyID;
230✔
283
  const privateKey: string = val.privateKey;
230✔
284
  const passphrase: string = val.passphrase;
230✔
285
  return {
230✔
286
    publicKeyId: publicKeyId,
287
    privateKey: privateKey,
288
    passphrase: passphrase,
289
  } satisfies JwtConfigAppSettingsAppAuth;
290
}
291
export function serializeJwtConfigAppSettings(val: any): SerializedData {
136✔
UNCOV
292
  return {
×
293
    ['clientID']: val.clientId,
294
    ['clientSecret']: val.clientSecret,
295
    ['appAuth']: serializeJwtConfigAppSettingsAppAuth(val.appAuth),
296
  };
297
}
298
export function deserializeJwtConfigAppSettings(
136✔
299
  val: any
300
): JwtConfigAppSettings {
301
  const clientId: string = val.clientID;
230✔
302
  const clientSecret: string = val.clientSecret;
230✔
303
  const appAuth: JwtConfigAppSettingsAppAuth =
304
    deserializeJwtConfigAppSettingsAppAuth(val.appAuth);
230✔
305
  return {
230✔
306
    clientId: clientId,
307
    clientSecret: clientSecret,
308
    appAuth: appAuth,
309
  } satisfies JwtConfigAppSettings;
310
}
311
export function serializeJwtConfigFile(val: any): SerializedData {
136✔
312
  return {
×
313
    ['enterpriseID']: val.enterpriseId == void 0 ? void 0 : val.enterpriseId,
×
314
    ['userID']: val.userId == void 0 ? void 0 : val.userId,
×
315
    ['boxAppSettings']: serializeJwtConfigAppSettings(val.boxAppSettings),
316
  };
317
}
318
export function deserializeJwtConfigFile(val: any): JwtConfigFile {
136✔
319
  const enterpriseId: undefined | string =
320
    val.enterpriseID == void 0 ? void 0 : val.enterpriseID;
230!
321
  const userId: undefined | string = val.userID == void 0 ? void 0 : val.userID;
230!
322
  const boxAppSettings: JwtConfigAppSettings = deserializeJwtConfigAppSettings(
230✔
323
    val.boxAppSettings
324
  );
325
  return {
230✔
326
    enterpriseId: enterpriseId,
327
    userId: userId,
328
    boxAppSettings: boxAppSettings,
329
  } satisfies JwtConfigFile;
330
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc