8000 Adding support for Token Exchange by samyap4 · Pull Request #837 · auth0/auth0-react · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Adding support for Token Exchange #837

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
180 changes: 180 additions & 0 deletions __tests__/token-exchange.test.tsx
10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import {
Auth0Client,
TokenEndpointResponse,
} from '@auth0/auth0-spa-js';
import '@testing-library/jest-dom';
import { act, renderHook } from '@testing-library/react';
import { useContext } from 'react';
import { useAuth0 } from '../src';
import Auth0Context from '../src/auth0-context';
import { createWrapper } from './helpers';

const clientMock = jest.mocked(new Auth0Client({ clientId: '', domain: '' }));

describe('Token Exchange', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should exchange token successfully', async () => {
const mockTokenResponse: TokenEndpointResponse = {
access_token: 'mock-access-token',
id_token: 'mock-id-token',
refresh_token: 'mock-refresh-token',
expires_in: 3600,
scope: 'openid profile'
} as TokenEndpointResponse;

clientMock.exchangeToken.mockResolvedValue(mockTokenResponse);

const wrapper = createWrapper();
const { result } = renderHook(() => useAuth0(), { wrapper });

await act(async () => {
const response = await result.current.exchangeToken({
subject_token: 'external-token',
subject_token_type: 'urn:acme:legacy-system-token',
scope: 'openid profile',
audience: 'https://api.example.com'
});

expect(response).toEqual(mockTokenResponse);
expect(clientMock.exchangeToken).toHaveBeenCalledWith({
subject_token: 'external-token',
subject_token_type: 'urn:acme:legacy-system-token',
scope: 'openid profile',
audience: 'https://api.example.com'
});
});
});



it('should handle token exchange error', async () => {
const mockError = new Error('Token exchange failed');
clientMock.exchangeToken.mockRejectedValue(mockError);

const wrapper = createWrapper();
const { result } = renderHook(() => useAuth0(), { wrapper });

await act(async () => {
await expect(
result.current.exchangeToken({
subject_token: 'invalid-token',
subject_token_type: 'urn:acme:legacy-system-token',
audience: 'https://api.example.com'
})
).rejects.toThrow('Token exchange failed');

expect(clientMock.exchangeToken).toHaveBeenCalledWith({
subject_token: 'invalid-token',
subject_token_type: 'urn:acme:legacy-system-token',
audience: 'https://api.example.com'
});
});
});

it('should handle token exchange with audience', async () => {
const mockTokenResponse: TokenEndpointResponse = {
access_token: 'mock-access-token',
id_token: 'mock-id-token',
refresh_token: 'mock-refresh-token',
expires_in: 3600,
scope: 'openid profile',
};

clientMock.exchangeToken.mockResolvedValue(mockTokenResponse);

const wrapper = createWrapper({
authorizationParams: {
audience: 'https://api.example.com',
},
});
const { result } = renderHook(() => useContext(Auth0Context), { wrapper });

await act(async () => {
const response = await result.current.exchangeToken({
subject_token: 'external-token',
subject_token_type: 'urn:acme:legacy-system-token',
scope: 'openid profile',
audience: 'https://your-api-url',
});

expect(response).toEqual(mockTokenResponse);
expect(clientMock.exchangeToken).toHaveBeenCalledWith({
subject_token: 'external-token',
subject_token_type: 'urn:acme:legacy-system-token',
scope: 'openid profile',
});
});
});

it('should handle token exchange with custom audience', async () => {
const mockTokenResponse: TokenEndpointResponse = {
access_token: 'mock-access-token',
id_token: 'mock-id-token',
refresh_token: 'mock-refresh-token',
expires_in: 3600,
scope: 'openid profile',
};

clientMock.exchangeToken.mockResolvedValue(mockTokenResponse);

const wrapper = createWrapper({
authorizationParams: {
audience: 'https://api.example.com/v2',
},
});

const { result } = renderHook(() => useAuth0(), { wrapper });

await act(async () => {
const response = await result.current.exchangeToken({
subject_token: 'external-token',
subject_token_type: 'urn:acme:legacy-system-token',
scope: 'openid profile',
audience: 'https://api.example.com/v2',
});

expect(response).toEqual(mockTokenResponse);
expect(clientMock.exchangeToken).toHaveBeenCalledWith({
subject_token: 'external-token',
subject_token_type: 'urn:acme:legacy-system-token',
scope: 'openid profile',
audience: 'https://api.example.com/v2',
});
});
});

it('should handle token exchange with custom scope', async () => {
const mockTokenResponse: TokenEndpointResponse = {
access_token: 'mock-access-token',
id_token: 'mock-id-token',
refresh_token: 'mock-refresh-token',
expires_in: 3600,
scope: 'custom:scope openid profile'
} as TokenEndpointResponse;

clientMock.exchangeToken.mockResolvedValue(mockTokenResponse);

const wrapper = createWrapper();
const { result } = renderHook(() => useAuth0(), { wrapper });

await act(async () => {
const response = await result.current.exchangeToken({
subject_token: 'external-token',
subject_token_type: 'urn:acme:legacy-system-token',
scope: 'custom:scope openid profile',
audience: 'https://api.example.com'
});

expect(response).toEqual(mockTokenResponse);
expect(clientMock.exchangeToken).toHaveBeenCalledWith({
subject_token: 'external-token',
subject_token_type: 'urn:acme:legacy-system-token',
scope: 'custom:scope openid profile',
audience: 'https://api.example.com'
< 49B8 /td> });
});
});
});
Loading
0