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

feat: add id token support #169

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
6 changes: 5 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,11 @@ export class OAuth2Client {

switch(authMethod) {
case 'client_secret_basic' :
// Per RFC 6749 section 2.3.1, the client_id and client_secret need
// to be encoded using application/x-www-form-urlencoded for the
// basic auth.
headers.Authorization = 'Basic ' +
btoa(this.settings.clientId + ':' + this.settings.clientSecret);
btoa(encodeURIComponent(this.settings.clientId) + ':' + encodeURIComponent(this.settings.clientSecret!));
break;
case 'client_secret_post' :
body.client_id = this.settings.clientId;
Expand Down Expand Up @@ -464,6 +467,7 @@ export class OAuth2Client {

return {
accessToken: body.access_token,
idToken: body.id_token,
expiresAt: body.expires_in ? Date.now() + (body.expires_in * 1000) : null,
refreshToken: body.refresh_token ?? null,
};
Expand Down
1 change: 1 addition & 0 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export type TokenResponse = {
token_type: string;
expires_in?: number;
refresh_token?: string;
id_token?: string;
scope?: string;
}

Expand Down
5 changes: 5 additions & 0 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ export type OAuth2Token = {
* OAuth2 refresh token
*/
refreshToken: string | null;

/**
* OAuth2 ID Token
*/
idToken?: string;
};
6 changes: 3 additions & 3 deletions test/client-credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe('client-credentials', () => {
const client = new OAuth2Client({
server: server.url,
tokenEndpoint: '/token',
clientId: 'test-client-id',
clientSecret: 'test-client-secret',
clientId: 'test-client-id:10',
clientSecret: 'test=client=secret',
});

const result = await client.clientCredentials();
Expand All @@ -32,7 +32,7 @@ describe('client-credentials', () => {
const request = server.lastRequest();
assert.equal(
request.headers.get('Authorization'),
'Basic ' + btoa('test-client-id:test-client-secret')
'Basic ' + btoa('test-client-id%3A10:test%3Dclient%3Dsecret')
);

assert.deepEqual(request.body, {
Expand Down
22 changes: 22 additions & 0 deletions test/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,33 @@ describe('tokenResponseToOAuth2Token', () => {

assert.deepEqual(token, {
accessToken: 'foo-bar',
idToken: undefined,
expiresAt: null,
refreshToken: null,
});
});

it('should respond with all tokens', async () => {
const client = new OAuth2Client({
clientId: 'foo',
});
const token = await client.tokenResponseToOAuth2Token(
Promise.resolve({
token_type: 'bearer',
access_token: 'foo',
id_token: 'bar',
refresh_token: 'baz',
})
);

assert.deepEqual(token, {
accessToken: 'foo',
idToken: 'bar',
expiresAt: null,
refreshToken: 'baz',
});
});

it('should error when an invalid JSON object is passed', async () => {
const client = new OAuth2Client({
clientId: 'foo',
Expand Down