Skip to content
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

Added support for /v3/connect/tokeninfo endpoint #536

Merged
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

### Unreleased
* Added support for `/v3/connect/tokeninfo` endpoint
* Models can now directly be imported from the top-level `nylas` package

### 7.0.0 / 2024-02-05
Expand Down
30 changes: 30 additions & 0 deletions src/models/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,33 @@ export interface ProviderDetectResponse {
*/
type?: string;
}

/**
* Interface representing a Nylas token information response.
*/
export interface TokenInfoResponse {
/**
* The issuer of the token.
*/
iss: string;
/**
* The token's audience.
*/
aud: string;
/**
* The time that the token was issued.
*/
iat: number;
/**
* The time that the token expires.
*/
exp: number;
/**
* The token's subject.
*/
sub?: string;
/**
* The email address of the Grant belonging to the user's token.
*/
email?: string;
}
33 changes: 33 additions & 0 deletions src/resources/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
CodeExchangeResponse,
ProviderDetectParams,
ProviderDetectResponse,
TokenInfoResponse,
} from '../models/auth.js';
import { Overrides } from '../config.js';
import { NylasResponse } from '../models/response.js';
Expand Down Expand Up @@ -161,6 +162,28 @@ export class Auth extends Resource {
});
}

/**
* Get info about an ID token
* @param idToken The ID token to query.
* @return The token information
*/
public idTokenInfo(
idToken: string
): Promise<NylasResponse<TokenInfoResponse>> {
return this.getTokenInfo({ id_token: idToken });
}

/**
* Get info about an access token
* @param accessToken The access token to query.
* @return The token information
*/
public accessTokenInfo(
accessToken: string
): Promise<NylasResponse<TokenInfoResponse>> {
return this.getTokenInfo({ access_token: accessToken });
}

private urlAuthBuilder(config: Record<string, any>): URL {
const url = new URL(`${this.apiClient.serverUrl}/v3/connect/auth`);
url.searchParams.set('client_id', config.clientId);
Expand Down Expand Up @@ -204,4 +227,14 @@ export class Auth extends Resource {
.toString('base64')
.replace(/=+$/, '');
}

private getTokenInfo(
params: Record<string, any>
): Promise<NylasResponse<TokenInfoResponse>> {
return this.apiClient.request<NylasResponse<TokenInfoResponse>>({
method: 'GET',
path: `/v3/connect/tokeninfo`,
queryParams: params,
});
}
}
29 changes: 29 additions & 0 deletions tests/resources/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,33 @@ describe('Auth', () => {
});
});
});
describe('token info', () => {
describe('idTokenInfo', () => {
it('should call getTokenInfo with the correct params', async () => {
await auth.idTokenInfo('idToken123');

expect(apiClient.request).toHaveBeenCalledWith({
method: 'GET',
path: '/v3/connect/tokeninfo',
queryParams: {
id_token: 'idToken123',
},
});
});
});

describe('accessTokenInfo', () => {
it('should call getTokenInfo with the correct params', async () => {
await auth.accessTokenInfo('accessToken123');

expect(apiClient.request).toHaveBeenCalledWith({
method: 'GET',
path: '/v3/connect/tokeninfo',
queryParams: {
access_token: 'accessToken123',
},
});
});
});
});
});
Loading