diff --git a/e2e/oauth2/display-test.spec.mjs b/e2e/oauth2/display-test.spec.mjs index 7e8e8cee34..2725a58fc7 100644 --- a/e2e/oauth2/display-test.spec.mjs +++ b/e2e/oauth2/display-test.spec.mjs @@ -15,7 +15,7 @@ */ /* - * The ?display= surface of the consent page on /oauth2/authorize. + * The ?display= surface of the consent pages on /oauth2/authorize and /oauth2/device/user. * OAuth2Representation picks the template folder from the * parameter: page/ (also for an absent or empty value), popup/ (composed into popup.ftl), touch/ or wap/. This spec * pins that black-box contract before /oauth2 moves to CHF. A flip may change a pinned value only as a divergence @@ -32,6 +32,7 @@ import { REALM, REDIRECT_URI, SCOPE, ensureProviderConfig, pkce, sessionContext /** This spec's own clients: Playwright runs spec files in parallel, and a client another file rewrites mid-run * would break this one. */ const CONSENT_CLIENT_ID = "test_client_display"; +const DEVICE_CLIENT_ID = "test_client_display_device"; const OAUTH2 = `${OPENAM_BASE}/oauth2`; /** The deployment's context path, so the form-target checks survive a deployment mounted elsewhere. */ @@ -85,6 +86,11 @@ test.beforeAll(async ({ request }) => { await writeClient(request, CONSENT_CLIENT_ID, { "com.forgerock.openam.oauth2provider.grantTypes": ["[0]=authorization_code"], }); + await writeClient(request, DEVICE_CLIENT_ID, { + "com.forgerock.openam.oauth2provider.grantTypes": [ + "[0]=authorization_code", "[1]=urn:ietf:params:oauth:grant-type:device_code", + ], + }); // The provider enforces PKCE (codeVerifierEnforced defaults to true), so every response_type=code request below // needs an S256 challenge, or it fails validation before a template is chosen. @@ -141,6 +147,34 @@ function pageDataValue(html, key) { return match && match[1]; } +/** Mints a device code for the consent-requiring device client and returns its user code. */ +async function deviceCode(request) { + const response = await request.post(`${OAUTH2}/device/code`, { + form: { + client_id: DEVICE_CLIENT_ID, scope: SCOPE, response_type: "code", + code_challenge: challenge, code_challenge_method: "S256", + }, + }); + if (!response.ok()) { + throw new Error(`device/code failed: ${response.status()}`); + } + const userCode = (await response.json()).user_code; + expect(typeof userCode === "string").toBe(true); + expect(Boolean(userCode)).toBe(true); + return userCode; +} + +/** + * Submits a user code as the code-entry form does, with the demo session cookie and with the session id as the csrf + * value (accepted for non-browser clients). display travels in the query; the user code only in the body. + */ +function submitUserCode(userCode, display) { + return demoCtx.post(`${OAUTH2}/device/user?display=${display}`, { + form: { user_code: userCode, csrf: demoToken }, + maxRedirects: 0, + }); +} + /** ESAPI HTML-encodes the name before FreeMarker interpolates it; decode once as inert text, never markup. */ async function userNameValue(page, html) { return page.evaluate((encoded) => { @@ -352,3 +386,114 @@ test.describe("?display= on /oauth2/authorize", () => { }); }); + +test.describe("?display= on /oauth2/device/user", () => { + + test("the device CONSENT page honours every display, exactly as /authorize does", async ({ request, page }) => { + // DeviceCodeVerificationResource extends ConsentRequiredResource and renders the consent page through the same + // OAuth2Representation, with the model built from the device code's record. A GET never acts on a user code (it + // is redirected without it), so the page is reached by the POST the code-entry form makes. + // + // display travels in the query, so the post-back target carries it: through pageData.formTarget on the three XUI + // templates, through the form's own action on wap. The user code is not in the target; the XUI templates carry it + // as pageData.userCode. + for (const [display, assertions] of [ + ["page", (html, target, userCode) => { + expect(html.startsWith("")).toBe(true); + expect(pageDataValue(html, "displayName")).toBe(DEVICE_CLIENT_ID); + expect(html.includes("isSaveConsentEnabled: true")).toBe(true); + expect(pageDataValue(html, "formTarget") === `\\${target}`, "Consent target must preserve display").toBe(true); + expect(pageDataValue(html, "userCode") === userCode, "Consent must preserve the user code").toBe(true); + }], + ["popup", (html, target, userCode, consentHtml) => { + expect(html.startsWith("")).toBe(true); + expect(html.includes("
${DEVICE_CLIENT_ID}:
`)).toBe(true); + expect(pageDataValue(html, "formTarget")).toBeNull(); + expect(html.includes(`