Skip to content

Commit 75d6769

Browse files
authored
Dan/connect idp integration (#16)
1 parent f5ae27f commit 75d6769

File tree

6 files changed

+514
-13
lines changed

6 files changed

+514
-13
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@nylas/connect": minor
3+
---
4+
- Added `IdentityProviderTokenCallback` type for providing JWT tokens
5+
- Added optional `identityProviderToken` callback to `ConnectConfig`
6+
- Token exchange now uses JSON format instead of form-encoded requests
7+
- Added `idp_claims` field to token exchange when IDP token is provided
8+

packages/nylas-connect/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,58 @@ Match your Nylas account region:
284284

285285
Automatic. @nylas/connect handles token refresh in the background.
286286

287+
288+
# External Identity Provider Integration Example
289+
290+
This example demonstrates how to use the new `identityProviderToken` callback feature to integrate external identity providers (via JWKS) with Nylas Connect.
291+
292+
## Basic Usage
293+
294+
```typescript
295+
import { NylasConnect } from '@nylas/connect';
296+
297+
// Example: Using a function that returns a JWT token
298+
const connect = new NylasConnect({
299+
clientId: 'your-client-id',
300+
redirectUri: 'http://localhost:3000/auth/callback',
301+
302+
// New feature: Identity provider token callback
303+
identityProviderToken: async () => {
304+
// Your logic to get the JWT token from your external identity provider
305+
// This could be from your own auth system, a third-party service, etc.
306+
const token = await getJWTFromYourIdentityProvider();
307+
return token; // Return the JWT string, or null if not available
308+
}
309+
});
310+
311+
// The rest works the same as before
312+
const result = await connect.connect({ method: 'popup' });
313+
```
314+
315+
316+
## How It Works
317+
318+
1. When you call `connect.connect()`, the authentication flow proceeds normally
319+
2. During the token exchange step (when exchanging the authorization code for access tokens), the `identityProviderToken` callback is called
320+
3. If the callback returns a JWT token, it's sent to Nylas as the `idp_claims` parameter
321+
4. If the callback returns `null` or throws an error:
322+
- Returning `null`: The auth flow continues without IDP claims
323+
- Throwing an error: The entire token exchange fails with a `NETWORK_ERROR` event
324+
325+
## Error Handling
326+
327+
If the `identityProviderToken` callback throws an error, the entire authentication flow will fail with a `NETWORK_ERROR` event. You can listen for this event to handle IDP-related errors:
328+
329+
```typescript
330+
connect.onConnectStateChange((event, session, data) => {
331+
if (event === 'NETWORK_ERROR' && data?.operation === 'identity_provider_token_callback') {
332+
// Handle IDP token callback error
333+
console.error('IDP token error:', data.error);
334+
}
335+
});
336+
```
337+
338+
287339
## License
288340

289341
MIT © [Nylas](https://nylas.com)

0 commit comments

Comments
 (0)