Skip to content

Commit

Permalink
Refactored error messages based on review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
NSeydoux committed Oct 13, 2020
1 parent 44808e6 commit 77e1054
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/oidc-dpop-client-browser/src/dpop/dpop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
normalizeHttpUriClaim,
signJwt,
} from "./dpop";
import { generateJwk, generateJwkForDpop } from "./keyGen";
import { generateJwk, generateJwkForDpop } from "./keyGeneration";

describe("signJwt/decodeJwt", () => {
it("generates a JWT that can be decoded without signature verification", async () => {
Expand Down
20 changes: 10 additions & 10 deletions packages/oidc-dpop-client-browser/src/dpop/tokenExchange.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const mockJwk = (): JWKECKey => {
// The two following modules introduce randomness in the process, which prevents
// making assumptions on the returned values. Mocking them out makes keys and
// DPoP headers predictible.
jest.mock("./keyGen", () => {
jest.mock("./keyGeneration", () => {
return {
generateJwkForDpop: (): JWKECKey => mockJwk(),
};
Expand Down Expand Up @@ -223,7 +223,7 @@ describe("getTokens", () => {
true
);
await expect(request).rejects.toThrow(
`Invalid token endpoint response: requested a [DPoP] token, got a token_type [Bearer].`
`Invalid token endpoint response: requested a [DPoP] token, but got a 'token_type' value of [Bearer].`
);
});

Expand All @@ -237,7 +237,7 @@ describe("getTokens", () => {
false
);
await expect(request).rejects.toThrow(
`Invalid token endpoint response: requested a [Bearer] token, got a token_type [DPoP].`
`Invalid token endpoint response: requested a [Bearer] token, but got a 'token_type' value of [DPoP].`
);
});

Expand All @@ -255,9 +255,9 @@ describe("getTokens", () => {
false
);
await expect(request).rejects.toThrow(
`Invalid token endpoint response: ${JSON.stringify(
`Invalid token endpoint response (missing the field 'token_type'): ${JSON.stringify(
tokenResponse
)} is missing an token_type.`
)}`
);
});

Expand Down Expand Up @@ -327,9 +327,9 @@ describe("getTokens", () => {
await expect(
getTokens(mockIssuer(), mockClient(), mockEndpointInput(), true)
).rejects.toThrow(
`Invalid token endpoint response: ${JSON.stringify(
`Invalid token endpoint response (missing the field 'access_token'): ${JSON.stringify(
tokenEndpointResponse
)} is missing an access_token.`
)}`
);
});

Expand All @@ -344,9 +344,9 @@ describe("getTokens", () => {
await expect(
getTokens(mockIssuer(), mockClient(), mockEndpointInput(), true)
).rejects.toThrow(
`Invalid token endpoint response: ${JSON.stringify(
`Invalid token endpoint response (missing the field 'id_token'): ${JSON.stringify(
tokenEndpointResponse
)} is missing an id_token.`
)}`
);
});

Expand Down Expand Up @@ -415,7 +415,7 @@ describe("getTokens", () => {
await expect(
getTokens(mockIssuer(), mockClient(), mockEndpointInput(), false)
).rejects.toThrow(
`Cannot extract WebID from ID token: the ID token returned by ${mockIssuer().issuer.toString()} has no webid claim, nor an IRI-like sub claim: [some subject]`
`Cannot extract WebID from ID token: the ID token returned by [${mockIssuer().issuer.toString()}] has no 'webid' claim, nor an IRI-like 'sub' claim: [some subject]`
);
});

Expand Down
29 changes: 15 additions & 14 deletions packages/oidc-dpop-client-browser/src/dpop/tokenExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export type TokenEndpointInput = {
type WebIdOidcIdToken = {
sub: string;
iss: string;
webId?: string;
// The spec requires this capitalization of webid
webid?: string;
};

function isWebIdOidcIdToken(
Expand All @@ -78,8 +79,8 @@ function isWebIdOidcIdToken(
typeof token.sub === "string" &&
token.iss &&
typeof token.iss === "string" &&
!token.webId) ||
typeof token.webId === "string"
!token.webid) ||
typeof token.webid === "string"
);
}

Expand All @@ -99,12 +100,12 @@ async function deriveWebIdFromIdToken(idToken: string): Promise<string> {
)} is missing 'sub' or 'iss' claims`
);
}
if (decoded.webId) {
return decoded.webId;
if (decoded.webid) {
return decoded.webid;
}
if (!decoded.sub.match(/^https?:\/\/.+\..+$/)) {
throw new Error(
`Cannot extract WebID from ID token: the ID token returned by ${decoded.iss} has no 'webid' claim, nor an IRI-like sub claim: [${decoded.sub}]`
`Cannot extract WebID from ID token: the ID token returned by [${decoded.iss}] has no 'webid' claim, nor an IRI-like 'sub' claim: [${decoded.sub}]`
);
}
return decoded.sub;
Expand Down Expand Up @@ -138,37 +139,37 @@ function validateTokenEndpointResponse(
): Record<string, unknown> & { access_token: string; id_token: string } {
if (!hasAccessToken(tokenResponse)) {
throw new Error(
`Invalid token endpoint response: ${JSON.stringify(
`Invalid token endpoint response (missing the field 'access_token'): ${JSON.stringify(
tokenResponse
)} is missing an access_token.`
)}`
);
}

if (!hasIdToken(tokenResponse)) {
throw new Error(
`Invalid token endpoint response: ${JSON.stringify(
`Invalid token endpoint response (missing the field 'id_token'): ${JSON.stringify(
tokenResponse
)} is missing an id_token.`
)}.`
);
}

if (!hasTokenType(tokenResponse)) {
throw new Error(
`Invalid token endpoint response: ${JSON.stringify(
`Invalid token endpoint response (missing the field 'token_type'): ${JSON.stringify(
tokenResponse
)} is missing an token_type.`
)}`
);
}

if (dpop && tokenResponse.token_type.toLowerCase() !== "dpop") {
throw new Error(
`Invalid token endpoint response: requested a [DPoP] token, got a token_type [${tokenResponse.token_type}].`
`Invalid token endpoint response: requested a [DPoP] token, but got a 'token_type' value of [${tokenResponse.token_type}].`
);
}

if (!dpop && tokenResponse.token_type.toLowerCase() !== "bearer") {
throw new Error(
`Invalid token endpoint response: requested a [Bearer] token, got a token_type [${tokenResponse.token_type}].`
`Invalid token endpoint response: requested a [Bearer] token, but got a 'token_type' value of [${tokenResponse.token_type}].`
);
}
return tokenResponse;
Expand Down
2 changes: 1 addition & 1 deletion packages/oidc-dpop-client-browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export {
createDpopHeader,
privateJwkToPublicJwk,
} from "./dpop/dpop";
export { generateJwkForDpop, generateJwkRsa } from "./dpop/keyGen";
export { generateJwkForDpop, generateJwkRsa } from "./dpop/keyGeneration";
export {
getTokens,
TokenEndpointInput,
Expand Down

0 comments on commit 77e1054

Please sign in to comment.