Skip to content

fix: Fix downscope token to use retrieveToken method for token retrieval (box/box-codegen#731) #618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codegen.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "engineHash": "c6650a9", "specHash": "a8825be", "version": "1.15.1" }
{ "engineHash": "20cb559", "specHash": "a8825be", "version": "1.15.1" }
6 changes: 4 additions & 2 deletions docs/docgen.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ See the endpoint docs at
<!-- sample get_docgen_jobs_id_v2025.0 -->

```ts
await client.docgen.getDocgenJobByIdV2025R0(docgenJobs.entries![0].id);
await client.docgen.getDocgenJobByIdV2025R0(docgenJobItemFromList.id);
```

### Arguments
Expand Down Expand Up @@ -45,7 +45,9 @@ See the endpoint docs at
<!-- sample get_docgen_jobs_v2025.0 -->

```ts
await client.docgen.getDocgenJobsV2025R0();
await client.docgen.getDocgenJobsV2025R0({
limit: 500,
} satisfies GetDocgenJobsV2025R0QueryParams);
```

### Arguments
Expand Down
202 changes: 101 additions & 101 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/box/ccgAuth.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ export class BoxCcgAuth implements Authentication {
sharedLink?: string,
networkSession?: NetworkSession,
): Promise<AccessToken> {
const token: undefined | AccessToken = await this.tokenStorage.get();
const token: undefined | AccessToken =
await this.retrieveToken(networkSession);
if (token == void 0) {
throw new BoxSdkError({
message:
Expand Down
3 changes: 2 additions & 1 deletion src/box/developerTokenAuth.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export class BoxDeveloperTokenAuth implements Authentication {
sharedLink?: string,
networkSession?: NetworkSession,
): Promise<AccessToken> {
const token: undefined | AccessToken = await this.tokenStorage.get();
const token: undefined | AccessToken =
await this.retrieveToken(networkSession);
if (token == void 0 || token!.accessToken == void 0) {
throw new BoxSdkError({ message: 'No access token is available.' });
}
Expand Down
3 changes: 2 additions & 1 deletion src/box/jwtAuth.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ export class BoxJwtAuth implements Authentication {
sharedLink?: string,
networkSession?: NetworkSession,
): Promise<AccessToken> {
const token: undefined | AccessToken = await this.tokenStorage.get();
const token: undefined | AccessToken =
await this.retrieveToken(networkSession);
if (token == void 0) {
throw new BoxSdkError({
message:
Expand Down
3 changes: 2 additions & 1 deletion src/box/oauth.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ export class BoxOAuth implements Authentication {
sharedLink?: string,
networkSession?: NetworkSession,
): Promise<AccessToken> {
const token: undefined | AccessToken = await this.tokenStorage.get();
const token: undefined | AccessToken =
await this.retrieveToken(networkSession);
if (token == void 0 || token!.accessToken == void 0) {
throw new BoxSdkError({ message: 'No access token is available.' });
}
Expand Down
83 changes: 83 additions & 0 deletions src/test/auth.generated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,32 @@ test('test_jwt_auth_downscope', async function test_jwt_auth_downscope(): Promis
}).rejects.toThrow();
await parentClient.files.deleteFileById(file.id);
});
test('test_jwt_downscope_token_succeeds_if_no_token_available', async function test_jwt_downscope_token_succeeds_if_no_token_available(): Promise<any> {
const jwtConfig: JwtConfig = JwtConfig.fromConfigJsonString(
decodeBase64(getEnvVar('JWT_CONFIG_BASE_64')),
);
const auth: BoxJwtAuth = new BoxJwtAuth({ config: jwtConfig });
const downscopedToken: AccessToken = await auth.downscopeToken([
'root_readonly',
]);
if (!!(downscopedToken.accessToken == void 0)) {
throw new Error('Assertion failed');
}
const downscopedClient: BoxClient = new BoxClient({
auth: new BoxDeveloperTokenAuth({ token: downscopedToken.accessToken! }),
});
await expect(async () => {
await downscopedClient.uploads.uploadFile({
attributes: {
name: getUuid(),
parent: {
id: '0',
} satisfies UploadFileRequestBodyAttributesParentField,
} satisfies UploadFileRequestBodyAttributesField,
file: generateByteStream(1024 * 1024),
} satisfies UploadFileRequestBody);
}).rejects.toThrow();
});
test('test_jwt_auth_revoke', async function test_jwt_auth_revoke(): Promise<any> {
const jwtConfig: JwtConfig = JwtConfig.fromConfigJsonString(
decodeBase64(getEnvVar('JWT_CONFIG_BASE_64')),
Expand Down Expand Up @@ -175,6 +201,19 @@ test('test_oauth_auth_authorizeUrl', function test_oauth_auth_authorizeUrl(): an
throw new Error('Assertion failed');
}
});
test('test_oauth_downscope_token_succeeds_if_no_token_available', async function test_oauth_downscope_token_succeeds_if_no_token_available(): Promise<any> {
const config: OAuthConfig = new OAuthConfig({
clientId: getEnvVar('CLIENT_ID'),
clientSecret: getEnvVar('CLIENT_SECRET'),
});
const auth: BoxOAuth = new BoxOAuth({ config: config });
const resourcePath: string = ''.concat(
'https://api.box.com/2.0/files/12345',
) as string;
await expect(async () => {
await auth.downscopeToken(['item_rename', 'item_preview'], resourcePath);
}).rejects.toThrow();
});
test('test_ccg_auth', async function test_ccg_auth(): Promise<any> {
const userId: string = getEnvVar('USER_ID');
const enterpriseId: string = getEnvVar('ENTERPRISE_ID');
Expand Down Expand Up @@ -240,6 +279,34 @@ test('test_ccg_auth_downscope', async function test_ccg_auth_downscope(): Promis
}).rejects.toThrow();
await parentClient.folders.deleteFolderById(folder.id);
});
test('test_ccg_downscope_token_succeeds_if_no_token_available', async function test_ccg_downscope_token_succeeds_if_no_token_available(): Promise<any> {
const ccgConfig: CcgConfig = new CcgConfig({
clientId: getEnvVar('CLIENT_ID'),
clientSecret: getEnvVar('CLIENT_SECRET'),
userId: getEnvVar('USER_ID'),
});
const auth: BoxCcgAuth = new BoxCcgAuth({ config: ccgConfig });
const downscopedToken: AccessToken = await auth.downscopeToken([
'root_readonly',
]);
if (!!(downscopedToken.accessToken == void 0)) {
throw new Error('Assertion failed');
}
const downscopedClient: BoxClient = new BoxClient({
auth: new BoxDeveloperTokenAuth({ token: downscopedToken.accessToken! }),
});
await expect(async () => {
await downscopedClient.uploads.uploadFile({
attributes: {
name: getUuid(),
parent: {
id: '0',
} satisfies UploadFileRequestBodyAttributesParentField,
} satisfies UploadFileRequestBodyAttributesField,
file: generateByteStream(1024 * 1024),
} satisfies UploadFileRequestBody);
}).rejects.toThrow();
});
test('test_ccg_auth_revoke', async function test_ccg_auth_revoke(): Promise<any> {
const ccgConfig: CcgConfig = new CcgConfig({
clientId: getEnvVar('CLIENT_ID'),
Expand All @@ -262,6 +329,22 @@ test('test_ccg_auth_revoke', async function test_ccg_auth_revoke(): Promise<any>
throw new Error('Assertion failed');
}
});
test('test_developer_downscope_token_succeeds_if_no_token_available', async function test_developer_downscope_token_succeeds_if_no_token_available(): Promise<any> {
const developerTokenConfig: DeveloperTokenConfig = {
clientId: getEnvVar('CLIENT_ID'),
clientSecret: getEnvVar('CLIENT_SECRET'),
} satisfies DeveloperTokenConfig;
const auth: BoxDeveloperTokenAuth = new BoxDeveloperTokenAuth({
token: '',
config: developerTokenConfig,
});
const resourcePath: string = ''.concat(
'https://api.box.com/2.0/folders/12345',
) as string;
await expect(async () => {
await auth.downscopeToken(['item_rename', 'item_preview'], resourcePath);
}).rejects.toThrow();
});
test('test_developer_token_auth_revoke', async function test_developer_token_auth_revoke(): Promise<any> {
const developerTokenConfig: DeveloperTokenConfig = {
clientId: getEnvVar('CLIENT_ID'),
Expand Down
4 changes: 1 addition & 3 deletions src/test/collections.generated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ test('testCollections', async function testCollections(): Promise<any> {
} satisfies UpdateFolderByIdOptionalsInput);
const collectionItemsAfterUpdate: ItemsOffsetPaginated =
await client.collections.getCollectionItems(favouriteCollection.id!);
if (
!(collectionItemsAfterUpdate.totalCount! == collectionItems.totalCount! + 1)
) {
if (!(collectionItemsAfterUpdate.totalCount! > 0)) {
throw new Error('Assertion failed');
}
await client.folders.updateFolderById(folder.id, {
Expand Down
13 changes: 11 additions & 2 deletions src/test/docgen.generated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { serializeDocGenJobsV2025R0 } from '../schemas/v2025R0/docGenJobsV2025R0
import { deserializeDocGenJobsV2025R0 } from '../schemas/v2025R0/docGenJobsV2025R0.generated.js';
import { serializeDocGenJobsFullV2025R0 } from '../schemas/v2025R0/docGenJobsFullV2025R0.generated.js';
import { deserializeDocGenJobsFullV2025R0 } from '../schemas/v2025R0/docGenJobsFullV2025R0.generated.js';
import { serializeDocGenJobFullV2025R0 } from '../schemas/v2025R0/docGenJobFullV2025R0.generated.js';
import { deserializeDocGenJobFullV2025R0 } from '../schemas/v2025R0/docGenJobFullV2025R0.generated.js';
import { serializeDocGenJobV2025R0 } from '../schemas/v2025R0/docGenJobV2025R0.generated.js';
import { deserializeDocGenJobV2025R0 } from '../schemas/v2025R0/docGenJobV2025R0.generated.js';
import { BoxClient } from '../client.generated.js';
Expand All @@ -34,6 +36,8 @@ import { DocGenBatchCreateRequestV2025R0DestinationFolderField } from '../schema
import { DocGenDocumentGenerationDataV2025R0 } from '../schemas/v2025R0/docGenDocumentGenerationDataV2025R0.generated.js';
import { DocGenJobsV2025R0 } from '../schemas/v2025R0/docGenJobsV2025R0.generated.js';
import { DocGenJobsFullV2025R0 } from '../schemas/v2025R0/docGenJobsFullV2025R0.generated.js';
import { GetDocgenJobsV2025R0QueryParams } from '../managers/docgen.generated.js';
import { DocGenJobFullV2025R0 } from '../schemas/v2025R0/docGenJobFullV2025R0.generated.js';
import { DocGenJobV2025R0 } from '../schemas/v2025R0/docGenJobV2025R0.generated.js';
import { getDefaultClient } from './commons.generated.js';
import { uploadNewFile } from './commons.generated.js';
Expand Down Expand Up @@ -103,7 +107,9 @@ test('testDocgenBatchAndJobs', async function testDocgenBatchAndJobs(): Promise<
throw new Error('Assertion failed');
}
const docgenJobs: DocGenJobsFullV2025R0 =
await client.docgen.getDocgenJobsV2025R0();
await client.docgen.getDocgenJobsV2025R0({
limit: 500,
} satisfies GetDocgenJobsV2025R0QueryParams);
if (!(docgenJobs.entries!.length >= 1)) {
throw new Error('Assertion failed');
}
Expand Down Expand Up @@ -150,8 +156,11 @@ test('testDocgenBatchAndJobs', async function testDocgenBatchAndJobs(): Promise<
if (!((toString(docgenJobs.entries![0].type) as string) == 'docgen_job')) {
throw new Error('Assertion failed');
}
const indexOfItem: number = 0;
const docgenJobItemFromList: DocGenJobFullV2025R0 =
docgenJobs.entries![indexOfItem];
const docgenJob: DocGenJobV2025R0 =
await client.docgen.getDocgenJobByIdV2025R0(docgenJobs.entries![0].id);
await client.docgen.getDocgenJobByIdV2025R0(docgenJobItemFromList.id);
if (!!(docgenJob.batch.id == '')) {
throw new Error('Assertion failed');
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/events.generated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ test('testEventSourceFileOrFolder', async function testEventSourceFileOrFolder()
if (
!(
(toString(source.type) as string) == 'file' ||
(toString(source.type) as string) == 'folder'
(toString(source.type) as string) == 'folder' ||
(toString(source.type) as string) == 'collaboration'
)
) {
throw new Error('Assertion failed');
Expand Down
1 change: 1 addition & 0 deletions test-browser/sdkTest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const testConfig = {
test_jwt_auth: 'skip',
test_jwt_auth_downscope: 'skip',
test_jwt_auth_revoke: 'skip',
test_jwt_downscope_token_succeeds_if_no_token_available: 'skip',

// Unknown reason, these tests fail in browser
testDocgenBatchAndJobs: 'skip',
Expand Down
Loading