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

Expose id token #32

Open
wants to merge 1 commit into
base: master
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
13 changes: 13 additions & 0 deletions src/grant_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface AccessTokenResponse {
"token_type": string;
"expires_in"?: number;
"refresh_token"?: string;
"id_token"?: string;
scope?: string;
}

Expand Down Expand Up @@ -111,6 +112,15 @@ export abstract class OAuth2GrantBase {
response,
);
}
if (
body.id_token !== undefined &&
typeof body.id_token !== "string"
) {
throw new TokenResponseError(
"id_token is not a string",
response,
);
}
if (
body.expires_in !== undefined && typeof body.expires_in !== "number"
) {
Expand All @@ -134,6 +144,9 @@ export abstract class OAuth2GrantBase {
if (body.refresh_token) {
tokens.refreshToken = body.refresh_token;
}
if (body.id_token) {
tokens.idToken = body.id_token;
}
if (body.expires_in) {
tokens.expiresIn = body.expires_in;
}
Expand Down
19 changes: 19 additions & 0 deletions src/resource_owner_password_credentials_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ Deno.test("ResourceOwnerPasswordCredentialsGrant.getToken throws if the server r
);
});

Deno.test("ResourceOwnerPasswordCredentialsGrant.getToken throws if the server response's id_token property is present but not a string", async () => {
await assertRejects(
() =>
mockATResponse(
() =>
getOAuth2Client().ropc.getToken({ username: "un", password: "pw" }),
{
body: {
access_token: "at",
token_type: "tt",
id_token: 123 as any,
},
},
),
TokenResponseError,
"Invalid token response: id_token is not a string",
);
});

Deno.test("ResourceOwnerPasswordCredentialsGrant.getToken throws if the server response's expires_in property is present but not a number", async () => {
await assertRejects(
() =>
Expand Down
1 change: 1 addition & 0 deletions src/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface AccessTokenResponse {
"token_type": string;
"expires_in"?: number;
"refresh_token"?: string;
"id_token"?: string;
scope?: string;
}

Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export interface Tokens {
* which circumstances you'll receive one.
*/
refreshToken?: string;
/**
* The optional ID token returned by the authorization server.
*/
idToken?: string;
/**
* The scopes that were granted by the user.
*
Expand Down