Skip to content

Commit

Permalink
Changed clientSecret to optional for token exchange methods; defaul…
Browse files Browse the repository at this point in the history
…ts to API Key now (#531)

clientSecret is not required if the API Key used for the SDK and the clientId belong to the same application.
  • Loading branch information
mrashed-dev authored Jan 30, 2024
1 parent d1fff50 commit 24eca5f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

### Unreleased
* Changed `clientSecret` to optional for token exchange methods; defaults to API Key now
* Fix missing `type` field in `Event` model

### 7.0.0-beta.4 / 2024-01-12
Expand Down
8 changes: 4 additions & 4 deletions src/models/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ export interface CodeExchangeRequest {
*/
clientId: string;
/**
* Client secret of the application.
* Client secret of the application. If not provided, the API Key will be used instead.
*/
clientSecret: string;
clientSecret?: string;
/**
* The original plain text code verifier (code_challenge) used in the initial authorization request (PKCE).
*/
Expand All @@ -106,9 +106,9 @@ export interface TokenExchangeRequest {
*/
clientId: string;
/**
* Client secret of the application.
* Client secret of the application. If not provided, the API Key will be used instead.
*/
clientSecret: string;
clientSecret?: string;
}

/**
Expand Down
18 changes: 11 additions & 7 deletions src/resources/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ export class Auth extends Resource {
public exchangeCodeForToken(
request: CodeExchangeRequest
): Promise<CodeExchangeResponse> {
const body: Record<string, unknown> = {
...request,
grantType: 'authorization_code',
};
if (request.codeVerifier) {
body.codeVerifier = request.codeVerifier;
if (!request.clientSecret) {
request.clientSecret = this.apiClient.apiKey;
}

return this.apiClient.request<CodeExchangeResponse>({
method: 'POST',
path: `/v3/connect/token`,
body,
body: {
...request,
grantType: 'authorization_code',
},
});
}

Expand All @@ -68,6 +68,10 @@ export class Auth extends Resource {
public refreshAccessToken(
request: TokenExchangeRequest
): Promise<CodeExchangeResponse> {
if (!request.clientSecret) {
request.clientSecret = this.apiClient.apiKey;
}

return this.apiClient.request<CodeExchangeResponse>({
method: 'POST',
path: `/v3/connect/token`,
Expand Down
42 changes: 42 additions & 0 deletions tests/resources/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ describe('Auth', () => {
});
});

it('should default clientSecret to the API key', async () => {
const payload: CodeExchangeRequest = {
clientId: 'clientId',
redirectUri: 'https://redirect.uri/path',
code: 'code',
};
await auth.exchangeCodeForToken(payload);

expect(apiClient.request).toHaveBeenCalledWith({
method: 'POST',
path: '/v3/connect/token',
body: {
clientId: 'clientId',
clientSecret: 'apiKey',
redirectUri: 'https://redirect.uri/path',
code: 'code',
grantType: 'authorization_code',
},
});
});

it('should set codeVerifier', async () => {
const payload: CodeExchangeRequest = {
clientId: 'clientId',
Expand Down Expand Up @@ -92,6 +113,27 @@ describe('Auth', () => {
},
});
});

it('should default clientSecret to the API key', async () => {
const payload: TokenExchangeRequest = {
clientId: 'clientId',
redirectUri: 'https://redirect.uri/path',
refreshToken: 'refreshToken',
};
await auth.refreshAccessToken(payload);

expect(apiClient.request).toHaveBeenCalledWith({
method: 'POST',
path: '/v3/connect/token',
body: {
clientId: 'clientId',
clientSecret: 'apiKey',
redirectUri: 'https://redirect.uri/path',
refreshToken: 'refreshToken',
grantType: 'refresh_token',
},
});
});
});
});
describe('customAuthentication', () => {
Expand Down

0 comments on commit 24eca5f

Please sign in to comment.