Skip to content

Commit

Permalink
Add AccessKeyLoginOptions (including customClaims) for ExchangeAccess…
Browse files Browse the repository at this point in the history
…Key API (#338)

* Add AccessKeyLoginOptions (including customClaims) for ExchangeAccessKey API

* fix tests
  • Loading branch information
guyp-descope authored Feb 25, 2024
1 parent 105ff64 commit 3545625
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
11 changes: 6 additions & 5 deletions lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,21 +263,21 @@ describe('sdk', () => {
it('should fail when the server call throws', async () => {
const spyExchange = jest.spyOn(sdk.accessKey, 'exchange').mockRejectedValueOnce('error');
await expect(sdk.exchangeAccessKey('key')).rejects.toThrow('could not exchange access key');
expect(spyExchange).toHaveBeenCalledWith('key');
expect(spyExchange).toHaveBeenCalledWith('key', undefined);
});
it('should fail when getting an unexpected response from the server', async () => {
const spyExchange = jest
.spyOn(sdk.accessKey, 'exchange')
.mockResolvedValueOnce({ data: {} } as SdkResponse<ExchangeAccessKeyResponse>);
await expect(sdk.exchangeAccessKey('key')).rejects.toThrow('could not exchange access key');
expect(spyExchange).toHaveBeenCalledWith('key');
expect(spyExchange).toHaveBeenCalledWith('key', undefined);
});
it('should fail when the session token the server returns is invalid', async () => {
const spyExchange = jest.spyOn(sdk.accessKey, 'exchange').mockResolvedValueOnce({
data: { sessionJwt: expiredToken },
} as SdkResponse<ExchangeAccessKeyResponse>);
await expect(sdk.exchangeAccessKey('key')).rejects.toThrow('could not exchange access key');
expect(spyExchange).toHaveBeenCalledWith('key');
expect(spyExchange).toHaveBeenCalledWith('key', undefined);
});
it('should return the same session token it got from the server', async () => {
const spyExchange = jest.spyOn(sdk.accessKey, 'exchange').mockResolvedValueOnce({
Expand All @@ -287,8 +287,9 @@ describe('sdk', () => {
jwt: validToken,
token: { exp: 1981398111, iss: 'project-id' },
};
await expect(sdk.exchangeAccessKey('key')).resolves.toMatchObject(expected);
expect(spyExchange).toHaveBeenCalledWith('key');
const loginOptions = { customClaims: { k1: 'v1' } };
await expect(sdk.exchangeAccessKey('key', loginOptions)).resolves.toMatchObject(expected);
expect(spyExchange).toHaveBeenCalledWith('key', loginOptions);
});
});

Expand Down
15 changes: 12 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import createSdk, { ExchangeAccessKeyResponse, SdkResponse, wrapWith } from '@descope/core-js-sdk';
import createSdk, {
ExchangeAccessKeyResponse,
AccessKeyLoginOptions,
SdkResponse,
wrapWith,
} from '@descope/core-js-sdk';
import { JWK, JWTHeaderParameters, KeyLike, errors, importJWK, jwtVerify } from 'jose';
import {
permissionsClaimName,
Expand Down Expand Up @@ -184,14 +189,18 @@ const nodeSdk = ({ managementKey, publicKey, ...config }: NodeSdkArgs) => {
/**
* Exchange API key (access key) for a session key
* @param accessKey access key to exchange for a session JWT
* @param loginOptions Optional advanced controls over login parameters
* @returns AuthenticationInfo with session JWT data
*/
async exchangeAccessKey(accessKey: string): Promise<AuthenticationInfo> {
async exchangeAccessKey(
accessKey: string,
loginOptions?: AccessKeyLoginOptions,
): Promise<AuthenticationInfo> {
if (!accessKey) throw Error('access key must not be empty');

let resp: SdkResponse<ExchangeAccessKeyResponse>;
try {
resp = await sdk.accessKey.exchange(accessKey);
resp = await sdk.accessKey.exchange(accessKey, loginOptions);
} catch (error) {
logger?.error('failed to exchange access key', error);
throw Error(`could not exchange access key - Failed to exchange. Error: ${error}`);
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"typescript": "^4.6.4"
},
"dependencies": {
"@descope/core-js-sdk": "2.9.0",
"@descope/core-js-sdk": "2.11.0",
"cross-fetch": "^4.0.0",
"jose": "4.15.4",
"tslib": "^1.14.1"
Expand Down

0 comments on commit 3545625

Please sign in to comment.