mirror of
https://github.com/outline/outline.git
synced 2026-11-28 08:14:58 +03:00
feat: Add support for OIDC provider returning JWT from userinfo endpoint (#12879)
* feat: Add support for OIDC provider returning JWT from userinfo endpoint closes #12878 * PR feedback --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d15fb6c6bc
commit
032379da93
5 files changed
+121
-167
No files matched your search
@@ -34,7 +34,7 @@ const DefaultOptions = {
|
||||
maxCachedSessions: 500,
|
||||
};
|
||||
|
||||
export type { RequestInit } from "node-fetch";
|
||||
export type { RequestInit, Response } from "node-fetch";
|
||||
export { Headers };
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import JWT from "jsonwebtoken";
|
||||
import { http, HttpResponse } from "msw";
|
||||
import { server } from "@server/test/msw";
|
||||
import OAuthClient from "./oauth";
|
||||
@@ -30,4 +31,62 @@ describe("userInfo", () => {
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("should parse claims from an application/json response", async () => {
|
||||
server.use(
|
||||
http.get("http://example.com/userinfo", () =>
|
||||
HttpResponse.json({ sub: "1234", email: "test@example.com" })
|
||||
)
|
||||
);
|
||||
|
||||
const client = new MinimalOAuthClient("clientid", "clientsecret");
|
||||
const data = await client.userInfo("token");
|
||||
expect(data.sub).toBe("1234");
|
||||
expect(data.email).toBe("test@example.com");
|
||||
});
|
||||
|
||||
it("should parse claims from a signed application/jwt response", async () => {
|
||||
const token = JWT.sign(
|
||||
{ sub: "1234", email: "test@example.com" },
|
||||
"secret"
|
||||
);
|
||||
server.use(
|
||||
http.get(
|
||||
"http://example.com/userinfo",
|
||||
() =>
|
||||
new HttpResponse(token, {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/jwt" },
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
const client = new MinimalOAuthClient("clientid", "clientsecret");
|
||||
const data = await client.userInfo("token");
|
||||
expect(data.sub).toBe("1234");
|
||||
expect(data.email).toBe("test@example.com");
|
||||
});
|
||||
|
||||
it("should throw for an undecodable application/jwt response", async () => {
|
||||
server.use(
|
||||
http.get(
|
||||
"http://example.com/userinfo",
|
||||
() =>
|
||||
new HttpResponse("not-a-jwt", {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/jwt" },
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
const client = new MinimalOAuthClient("clientid", "clientsecret");
|
||||
try {
|
||||
expect.assertions(1);
|
||||
await client.userInfo("token");
|
||||
} catch (e) {
|
||||
expect(e instanceof Error && "id" in e ? e.id : undefined).toBe(
|
||||
"invalid_request"
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
+40
-2
@@ -1,4 +1,5 @@
|
||||
import { addMinutes, subMinutes } from "date-fns";
|
||||
import JWT from "jsonwebtoken";
|
||||
import type { Context } from "koa";
|
||||
import { errToString } from "@shared/utils/error";
|
||||
import { randomString } from "@shared/random";
|
||||
@@ -11,7 +12,7 @@ import {
|
||||
OAuthStateMismatchError,
|
||||
} from "../errors";
|
||||
import { safeEqual } from "./crypto";
|
||||
import fetch from "./fetch";
|
||||
import fetch, { type Response } from "./fetch";
|
||||
|
||||
/**
|
||||
* Generate a random nonce, persist it in a same-origin cookie, and return it
|
||||
@@ -67,6 +68,43 @@ export function verifyOAuthStateNonce(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a UserInfo endpoint response according to OIDC Core 1.0. Claims are
|
||||
* normally returned as a JSON object with `Content-Type: application/json`,
|
||||
* but when a signed and/or encrypted response was requested during client
|
||||
* registration the provider returns a JWT with `Content-Type: application/jwt`
|
||||
* instead.
|
||||
*
|
||||
* @param response The response received from the userinfo endpoint.
|
||||
* @returns The claims contained in the response body.
|
||||
* @throws {Error} When the body cannot be parsed as JSON or a decodable JWT.
|
||||
*/
|
||||
export async function parseUserInfoResponse<T = JWT.JwtPayload>(
|
||||
response: Response
|
||||
): Promise<T> {
|
||||
const contentType = response.headers.get("content-type") ?? "";
|
||||
const mediaType = contentType.split(";")[0].trim().toLowerCase();
|
||||
const text = await response.text();
|
||||
|
||||
if (mediaType === "application/jwt") {
|
||||
// The JWT arrives directly from the provider over TLS so, as with the
|
||||
// id_token, claims are read without local signature verification.
|
||||
const claims = JWT.decode(text.trim());
|
||||
if (!claims || typeof claims !== "object") {
|
||||
throw new Error(
|
||||
"Failed to decode application/jwt response, note that encrypted responses are not supported"
|
||||
);
|
||||
}
|
||||
return claims as T;
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch (_err) {
|
||||
throw new Error(`Expected JSON response from OIDC provider, got: ${text}`);
|
||||
}
|
||||
}
|
||||
|
||||
export default abstract class OAuthClient {
|
||||
private clientId: string;
|
||||
private clientSecret: string;
|
||||
@@ -105,7 +143,7 @@ export default abstract class OAuthClient {
|
||||
}
|
||||
|
||||
try {
|
||||
data = await response.json();
|
||||
data = await parseUserInfoResponse(response);
|
||||
} catch (err) {
|
||||
throw InvalidRequestError(errToString(err));
|
||||
}
|
||||
|
||||
+16
-13
@@ -1,12 +1,13 @@
|
||||
import crypto from "node:crypto";
|
||||
import { addMinutes, subMinutes } from "date-fns";
|
||||
import type { JwtPayload } from "jsonwebtoken";
|
||||
import type { Context, Next } from "koa";
|
||||
import type {
|
||||
StateStoreStoreCallback,
|
||||
StateStoreVerifyCallback,
|
||||
} from "passport-oauth2";
|
||||
import type { Primitive } from "utility-types";
|
||||
import { toError } from "@shared/utils/error";
|
||||
import { errToString, toError } from "@shared/utils/error";
|
||||
import { Client } from "@shared/types";
|
||||
import { getCookieDomain, parseDomain } from "@shared/utils/domains";
|
||||
import env from "@server/env";
|
||||
@@ -16,6 +17,7 @@ import { InternalError, OAuthStateMismatchError } from "../errors";
|
||||
import { hash, safeEqual } from "./crypto";
|
||||
import fetch from "./fetch";
|
||||
import { getUserForJWT } from "./jwt";
|
||||
import { parseUserInfoResponse } from "./oauth";
|
||||
import {
|
||||
hashOAuthStateNonce,
|
||||
signOAuthIntent,
|
||||
@@ -193,19 +195,21 @@ export class StateStore {
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes an authenticated JSON request to an OAuth provider endpoint.
|
||||
* Perform an authenticated request against an OAuth provider endpoint and
|
||||
* parse the response body. Handles both JSON responses and, for OIDC userinfo
|
||||
* endpoints configured for signed responses, `application/jwt` bodies.
|
||||
*
|
||||
* @param method the HTTP method to use.
|
||||
* @param endpoint the URL to request.
|
||||
* @param accessToken the bearer token to authenticate with.
|
||||
* @returns the parsed JSON response.
|
||||
* @throws InternalError if the response is not valid JSON.
|
||||
* @param method The HTTP method to use.
|
||||
* @param endpoint The endpoint to request.
|
||||
* @param accessToken The access token to authenticate with.
|
||||
* @returns The parsed response body.
|
||||
* @throws {InternalError} When the response body cannot be parsed.
|
||||
*/
|
||||
export async function request(
|
||||
export async function request<T = JwtPayload>(
|
||||
method: "GET" | "POST",
|
||||
endpoint: string,
|
||||
accessToken: string
|
||||
) {
|
||||
): Promise<T> {
|
||||
const response = await fetch(endpoint, {
|
||||
method,
|
||||
allowPrivateIPAddress: true,
|
||||
@@ -214,13 +218,12 @@ export async function request(
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
const text = await response.text();
|
||||
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch (_err) {
|
||||
return await parseUserInfoResponse<T>(response);
|
||||
} catch (err) {
|
||||
throw InternalError(
|
||||
`Failed to parse response from ${endpoint}. Expected JSON, got: ${text}`
|
||||
`Failed to parse response from ${endpoint}. ${errToString(err)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4429,39 +4429,7 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@radix-ui/react-dialog@npm:^1.1.1":
|
||||
version: 1.1.17
|
||||
resolution: "@radix-ui/react-dialog@npm:1.1.17"
|
||||
dependencies:
|
||||
"@radix-ui/primitive": "npm:1.1.4"
|
||||
"@radix-ui/react-compose-refs": "npm:1.1.3"
|
||||
"@radix-ui/react-context": "npm:1.1.4"
|
||||
"@radix-ui/react-dismissable-layer": "npm:1.1.13"
|
||||
"@radix-ui/react-focus-guards": "npm:1.1.4"
|
||||
"@radix-ui/react-focus-scope": "npm:1.1.10"
|
||||
"@radix-ui/react-id": "npm:1.1.2"
|
||||
"@radix-ui/react-portal": "npm:1.1.12"
|
||||
"@radix-ui/react-presence": "npm:1.1.6"
|
||||
"@radix-ui/react-primitive": "npm:2.1.6"
|
||||
"@radix-ui/react-slot": "npm:1.3.0"
|
||||
"@radix-ui/react-use-controllable-state": "npm:1.2.3"
|
||||
aria-hidden: "npm:^1.2.4"
|
||||
react-remove-scroll: "npm:^2.7.2"
|
||||
peerDependencies:
|
||||
"@types/react": "*"
|
||||
"@types/react-dom": "*"
|
||||
react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||
react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||
peerDependenciesMeta:
|
||||
"@types/react":
|
||||
optional: true
|
||||
"@types/react-dom":
|
||||
optional: true
|
||||
checksum: 10c0/3fcb6d084d1825861020056be9f35ed4df198e1f2c685faddea9a15eed21ed169edfe83ed8020894974c606a9660f417da732b1e819db8fec05d19f46a43f33f
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@radix-ui/react-dialog@npm:^1.1.18":
|
||||
"@radix-ui/react-dialog@npm:^1.1.1, @radix-ui/react-dialog@npm:^1.1.18":
|
||||
version: 1.1.18
|
||||
resolution: "@radix-ui/react-dialog@npm:1.1.18"
|
||||
dependencies:
|
||||
@@ -4506,29 +4474,6 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@radix-ui/react-dismissable-layer@npm:1.1.13":
|
||||
version: 1.1.13
|
||||
resolution: "@radix-ui/react-dismissable-layer@npm:1.1.13"
|
||||
dependencies:
|
||||
"@radix-ui/primitive": "npm:1.1.4"
|
||||
"@radix-ui/react-compose-refs": "npm:1.1.3"
|
||||
"@radix-ui/react-primitive": "npm:2.1.6"
|
||||
"@radix-ui/react-use-callback-ref": "npm:1.1.2"
|
||||
"@radix-ui/react-use-escape-keydown": "npm:1.1.2"
|
||||
peerDependencies:
|
||||
"@types/react": "*"
|
||||
"@types/react-dom": "*"
|
||||
react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||
react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||
peerDependenciesMeta:
|
||||
"@types/react":
|
||||
optional: true
|
||||
"@types/react-dom":
|
||||
optional: true
|
||||
checksum: 10c0/800a96e82b3719511c40cfdace1a7250690cf38eae2a0114ddc8e980a76756993b8f898359ed296e15b18a60b319b4700e7c8fb6206a6fef66b3907fbbcebef6
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@radix-ui/react-dismissable-layer@npm:1.1.14":
|
||||
version: 1.1.14
|
||||
resolution: "@radix-ui/react-dismissable-layer@npm:1.1.14"
|
||||
@@ -4590,27 +4535,6 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@radix-ui/react-focus-scope@npm:1.1.10":
|
||||
version: 1.1.10
|
||||
resolution: "@radix-ui/react-focus-scope@npm:1.1.10"
|
||||
dependencies:
|
||||
"@radix-ui/react-compose-refs": "npm:1.1.3"
|
||||
"@radix-ui/react-primitive": "npm:2.1.6"
|
||||
"@radix-ui/react-use-callback-ref": "npm:1.1.2"
|
||||
peerDependencies:
|
||||
"@types/react": "*"
|
||||
"@types/react-dom": "*"
|
||||
react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||
react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||
peerDependenciesMeta:
|
||||
"@types/react":
|
||||
optional: true
|
||||
"@types/react-dom":
|
||||
optional: true
|
||||
checksum: 10c0/f949c621b5a5b89b19df8223aebebb27e08be76398c2015631d3f613bd941dde2967d74848cc798d01a9b6af3d567e3c13cd82ee463cad92017f5f4bbb70a547
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@radix-ui/react-focus-scope@npm:1.1.11":
|
||||
version: 1.1.11
|
||||
resolution: "@radix-ui/react-focus-scope@npm:1.1.11"
|
||||
@@ -4774,27 +4698,7 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@radix-ui/react-portal@npm:1.1.12, @radix-ui/react-portal@npm:^1.0.1":
|
||||
version: 1.1.12
|
||||
resolution: "@radix-ui/react-portal@npm:1.1.12"
|
||||
dependencies:
|
||||
"@radix-ui/react-primitive": "npm:2.1.6"
|
||||
"@radix-ui/react-use-layout-effect": "npm:1.1.2"
|
||||
peerDependencies:
|
||||
"@types/react": "*"
|
||||
"@types/react-dom": "*"
|
||||
react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||
react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||
peerDependenciesMeta:
|
||||
"@types/react":
|
||||
optional: true
|
||||
"@types/react-dom":
|
||||
optional: true
|
||||
checksum: 10c0/9b1bcfdb0577c0972f09c44d9f55fc365ab18f7777cc412fc993e733318cf08c382824ab8d8bc1eb2305b7d2eb8d4e478900b610f8a7f5ea233ffc3cc14d1427
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@radix-ui/react-portal@npm:1.1.13":
|
||||
"@radix-ui/react-portal@npm:1.1.13, @radix-ui/react-portal@npm:^1.0.1":
|
||||
version: 1.1.13
|
||||
resolution: "@radix-ui/react-portal@npm:1.1.13"
|
||||
dependencies:
|
||||
@@ -4833,25 +4737,6 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@radix-ui/react-primitive@npm:2.1.6":
|
||||
version: 2.1.6
|
||||
resolution: "@radix-ui/react-primitive@npm:2.1.6"
|
||||
dependencies:
|
||||
"@radix-ui/react-slot": "npm:1.3.0"
|
||||
peerDependencies:
|
||||
"@types/react": "*"
|
||||
"@types/react-dom": "*"
|
||||
react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||
react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||
peerDependenciesMeta:
|
||||
"@types/react":
|
||||
optional: true
|
||||
"@types/react-dom":
|
||||
optional: true
|
||||
checksum: 10c0/2d497bf05275df598a1435fe0c45d0c4da6647d5ddd692ec77780242cab57819f6acd2c3b56086f202f7998a649697c680daf93e68a08bf3bc660b0155ebd88e
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@radix-ui/react-primitive@npm:2.1.7":
|
||||
version: 2.1.7
|
||||
resolution: "@radix-ui/react-primitive@npm:2.1.7"
|
||||
@@ -5168,21 +5053,6 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@radix-ui/react-use-escape-keydown@npm:1.1.2":
|
||||
version: 1.1.2
|
||||
resolution: "@radix-ui/react-use-escape-keydown@npm:1.1.2"
|
||||
dependencies:
|
||||
"@radix-ui/react-use-callback-ref": "npm:1.1.2"
|
||||
peerDependencies:
|
||||
"@types/react": "*"
|
||||
react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||
peerDependenciesMeta:
|
||||
"@types/react":
|
||||
optional: true
|
||||
checksum: 10c0/3fce06fbb986b21712db2ba3ec52b79c0928c7341983b3f08b878258d6b6f35247882c87a990bc1cd30ca44662cfe86733263d3b00cbdf34252311b8c7195138
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@radix-ui/react-use-is-hydrated@npm:0.1.1":
|
||||
version: 0.1.1
|
||||
resolution: "@radix-ui/react-use-is-hydrated@npm:0.1.1"
|
||||
@@ -10670,7 +10540,7 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"entities@npm:^4.2.0, entities@npm:^4.4.0, entities@npm:^4.5.0":
|
||||
"entities@npm:^4.2.0, entities@npm:^4.5.0":
|
||||
version: 4.5.0
|
||||
resolution: "entities@npm:4.5.0"
|
||||
checksum: 10c0/5b039739f7621f5d1ad996715e53d964035f75ad3b9a4d38c6b3804bb226e282ffeae2443624d8fdd9c47d8e926ae9ac009c54671243f0c3294c26af7cc85250
|
||||
@@ -14111,7 +13981,7 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"linkify-it@npm:5.0.1, linkify-it@npm:^5.0.1":
|
||||
"linkify-it@npm:5.0.1":
|
||||
version: 5.0.1
|
||||
resolution: "linkify-it@npm:5.0.1"
|
||||
dependencies:
|
||||
@@ -14499,23 +14369,7 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"markdown-it@npm:^14.0.0":
|
||||
version: 14.2.0
|
||||
resolution: "markdown-it@npm:14.2.0"
|
||||
dependencies:
|
||||
argparse: "npm:^2.0.1"
|
||||
entities: "npm:^4.4.0"
|
||||
linkify-it: "npm:^5.0.1"
|
||||
mdurl: "npm:^2.0.0"
|
||||
punycode.js: "npm:^2.3.1"
|
||||
uc.micro: "npm:^2.1.0"
|
||||
bin:
|
||||
markdown-it: bin/markdown-it.mjs
|
||||
checksum: 10c0/1d3a50061d2fe4efbcf317aac853dbee6892ed6f5a217570eead723f2ef2dd1c9baaeef5a687cd283480c45c2d20724a73e84a9ed72843cf7b3b719067af40ef
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"markdown-it@npm:^14.3.0":
|
||||
"markdown-it@npm:^14.0.0, markdown-it@npm:^14.3.0":
|
||||
version: 14.3.0
|
||||
resolution: "markdown-it@npm:14.3.0"
|
||||
dependencies:
|
||||
|
||||
Reference in new issue
Block a user