Skip to content
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
23 changes: 17 additions & 6 deletions __tests__/unit/authenticationManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { AuthenticationManager, type AuthenticationOptions } from '../../src/authenticationManager';
import {
AuthenticationManager,
type AuthenticationOptions,
} from '../../src/authenticationManager';
import * as openidClient from 'openid-client';
import * as jwtDecodeModule from 'jwt-decode';
import * as grpc from '@grpc/grpc-js';
Expand Down Expand Up @@ -43,7 +46,7 @@ describe('AuthenticationManager', () => {
);
expect(openidClient.clientCredentialsGrant).toHaveBeenCalledWith(
mockDiscovery,
{ audience: 'crisp-athena-dev', scope: options.scope }
{ audience: 'crisp-athena-live', scope: options.scope },
);
});

Expand All @@ -67,7 +70,9 @@ describe('AuthenticationManager', () => {
...mockToken,
access_token: 'new_access_token',
});
(jwtDecodeModule.jwtDecode as any).mockReturnValue({ exp: Math.floor(Date.now() / 1000) + 3600 });
(jwtDecodeModule.jwtDecode as any).mockReturnValue({
exp: Math.floor(Date.now() / 1000) + 3600,
});

const header = await manager.getAuthenticationHeader();
expect(openidClient.refreshTokenGrant).toHaveBeenCalled();
Expand All @@ -82,7 +87,9 @@ describe('AuthenticationManager', () => {
...mockToken,
access_token: 'reacquired_access_token',
});
(jwtDecodeModule.jwtDecode as any).mockReturnValue({ exp: Math.floor(Date.now() / 1000) + 3600 });
(jwtDecodeModule.jwtDecode as any).mockReturnValue({
exp: Math.floor(Date.now() / 1000) + 3600,
});

const header = await manager.getAuthenticationHeader();
expect(openidClient.clientCredentialsGrant).toHaveBeenCalled();
Expand All @@ -93,12 +100,16 @@ describe('AuthenticationManager', () => {
await manager.getAuthenticationHeader();
(manager as any).tokenExpiration = new Date(Date.now() - 1000); // expired
(manager as any).token.refresh_token = 'mock_refresh_token';
(openidClient.refreshTokenGrant as any).mockRejectedValue(new Error('refresh failed'));
(openidClient.refreshTokenGrant as any).mockRejectedValue(
new Error('refresh failed'),
);
(openidClient.clientCredentialsGrant as any).mockResolvedValue({
...mockToken,
access_token: 'fallback_access_token',
});
(jwtDecodeModule.jwtDecode as any).mockReturnValue({ exp: Math.floor(Date.now() / 1000) + 3600 });
(jwtDecodeModule.jwtDecode as any).mockReturnValue({
exp: Math.floor(Date.now() / 1000) + 3600,
});

const header = await manager.getAuthenticationHeader();
expect(openidClient.refreshTokenGrant).toHaveBeenCalled();
Expand Down
23 changes: 18 additions & 5 deletions src/authenticationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export type AuthenticationOptions = {
/** Whether to automatically refresh the access token. */
autoRefresh?: boolean;
/** OAuth scope to request. */
scope: 'manage:classify';
scope?: string;
/** OAuth audience to request. */
audience?: 'crisp-athena-live';
};

/**
Expand All @@ -41,6 +43,10 @@ export class AuthenticationManager {
*/
constructor(options: AuthenticationOptions) {
this.options = options;

if (!this.options.issuerUrl) {
this.options.issuerUrl = 'https://crispthinking.auth0.com/';
}
}

/**
Expand Down Expand Up @@ -112,10 +118,17 @@ export class AuthenticationManager {
}

if (this.token === undefined) {
this.token = await clientCredentialsGrant(this.discovery, {
audience: 'crisp-athena-dev',
scope: this.options.scope,
});
if (this.options.scope) {
this.token = await clientCredentialsGrant(this.discovery, {
audience: 'crisp-athena-live',
scope: this.options.scope,
});
} else {
this.token = await clientCredentialsGrant(this.discovery, {
audience: 'crisp-athena-live',
});
}

this.decoded = jwtDecode(this.token.access_token);

// Calculate expiry date of jwt
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ export type ClassifierEvents = {
open: () => void;
};

export const defaultGrpcAddress =
'csam-classification-messages.crispdev.com:443';
export const defaultGrpcAddress = 'trust.messages.crispthinking.com:443';

/**
* SDK for interacting with the Athena classification service via gRPC.
Expand Down