Skip to content

Commit

Permalink
Added changes to support mTLS authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
gyaneshgouraw-okta committed Apr 27, 2024
1 parent 3bc86cb commit 0084ed0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
19 changes: 19 additions & 0 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [Use Refresh Tokens](#use-refresh-tokens)
- [Complete the Authorization Code flow with PKCE](#complete-the-authorization-code-flow-with-pkce)
- [Login with Passwordless](#login-with-passwordless)
- [mTLS request](#mtls-request)
- [Management Client](#management-client)
- [Paginate through a list of users](#paginate-through-a-list-of-users)
- [Paginate through a list of logs using checkpoint pagination](#paginate-through-a-list-of-logs-using-checkpoint-pagination)
Expand Down Expand Up @@ -129,6 +130,24 @@ const { data: tokens } = await auth.passwordless.loginWithEmail({
});
```

### mTLS request

```js
import { AuthenticationClient } from 'auth0';

// add mtls prefix to your domain name
const auth = new AuthenticationClient({
domain: 'mtls.{YOUR_TENANT_AND REGION}.auth0.com',
clientId: '{YOUR_CLIENT_ID}',
agent: new https.Agent({ ... }),
});

const { data: tokens } = await auth.oauth.clientCredentialsGrant({
audience: 'you-api',
});

```

## Management Client

### Paginate through a list of users
Expand Down
11 changes: 10 additions & 1 deletion src/auth/client-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface AddClientAuthenticationOptions {
* Adds `client_assertion` and `client_assertion_type` for Private Key JWT token endpoint auth method.
*
* If `clientAssertionSigningKey` is provided it takes precedent over `clientSecret` .
* Also skips `client_secret` & `clientAssertionSigningKey` if request(domain) is of mTLS type
*/
export const addClientAuthentication = async ({
payload,
Expand Down Expand Up @@ -55,9 +56,17 @@ export const addClientAuthentication = async ({
}
if (
(!payload.client_secret || payload.client_secret.trim().length === 0) &&
(!payload.client_assertion || payload.client_assertion.trim().length === 0)
(!payload.client_assertion || payload.client_assertion.trim().length === 0) &&
!isMTLSRequest(domain)
) {
throw new Error('The client_secret or client_assertion field is required.');
}
return payload;
};

/**
* Checks if domain name starts with mTLS keyword for mTLS requests
*/
const isMTLSRequest = (domain: string): boolean => {
return domain.toLowerCase().startsWith('mtls');
};
46 changes: 46 additions & 0 deletions test/auth/client-authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,49 @@ describe('client-authentication for par endpoint', () => {
});
});
});

describe('mTLS-authentication', () => {
const path = jest.fn();
const body = jest.fn();
const headers = jest.fn();
const clientAssertion = jest.fn();
const URL = 'https://mtls.tenant.auth0.com/';

beforeEach(() => {
async function handler(this: any, pathIn: unknown, bodyIn: string) {
const bodyParsed = Object.fromEntries(new URLSearchParams(bodyIn));
path(pathIn);
body(bodyParsed);
headers(this.req.headers);
if ((bodyParsed as any).client_assertion) {
clientAssertion(await verify(bodyParsed.client_assertion, TEST_PUBLIC_KEY, verifyOpts));
}
return {
access_token: 'test-access-token',
};
}

nock(URL, { encodedQueryParams: true }).post('/oauth/token').reply(200, handler).persist();
});

afterEach(() => {
nock.cleanAll();
jest.clearAllMocks();
});

it('should do client credentials grant without client secret or assertion', async () => {
const auth0 = new AuthenticationClient({
domain: 'mtls.tenant.auth0.com',
clientId,
});
await auth0.oauth.clientCredentialsGrant({
audience: 'my-api',
});
expect(path).toHaveBeenCalledWith('/oauth/token');
expect(body).toHaveBeenCalledWith({
grant_type: 'client_credentials',
client_id: clientId,
audience: 'my-api',
});
});
});

0 comments on commit 0084ed0

Please sign in to comment.