|
| 1 | +/* |
| 2 | + * Copyright 2026 NetKnights GmbH - nils.behlen@netknights.it |
| 3 | + * <p> |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * <p> |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.privacyidea; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +import org.junit.After; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | +import org.mockserver.integration.ClientAndServer; |
| 27 | +import org.mockserver.model.HttpRequest; |
| 28 | +import org.mockserver.model.HttpResponse; |
| 29 | +import org.mockserver.model.MediaType; |
| 30 | + |
| 31 | +import static org.junit.Assert.assertNotNull; |
| 32 | +import static org.junit.Assert.assertTrue; |
| 33 | + |
| 34 | +/** |
| 35 | + * Regression: the remember-device {@code Set-Cookie} the server issues on a successful auth must be surfaced |
| 36 | + * on {@link PIResponse#setCookieHeaders} for ALL auth paths, not only the plain OTP {@code validateCheck}. |
| 37 | + * WebAuthn and passkey previously used a body-only request helper and dropped the cookie, so "remember this |
| 38 | + * device" silently did nothing when the user authenticated with a security key / passkey. |
| 39 | + */ |
| 40 | +public class TestRememberDeviceCookieCapture |
| 41 | +{ |
| 42 | + private ClientAndServer mockServer; |
| 43 | + private PrivacyIDEA privacyIDEA; |
| 44 | + private static final String COOKIE = "pi_remember_device=1:abcdef; Path=/; Max-Age=604800"; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setup() |
| 48 | + { |
| 49 | + mockServer = ClientAndServer.startClientAndServer(1080); |
| 50 | + mockServer.when(HttpRequest.request().withMethod("POST").withPath("/validate/check")) |
| 51 | + .respond(HttpResponse.response() |
| 52 | + .withContentType(MediaType.APPLICATION_JSON) |
| 53 | + .withHeader("Set-Cookie", COOKIE) |
| 54 | + .withBody(Utils.matchingOneToken()) |
| 55 | + .withDelay(TimeUnit.MILLISECONDS, 20)); |
| 56 | + |
| 57 | + privacyIDEA = PrivacyIDEA.newBuilder("https://127.0.0.1:1080", "test") |
| 58 | + .verifySSL(false) |
| 59 | + .logger(new PILogImplementation()) |
| 60 | + .build(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void webauthnSurfacesSetCookie() |
| 65 | + { |
| 66 | + PIResponse r = privacyIDEA.validateCheckWebAuthn("testuser", "txn-1", "{}", "https://origin"); |
| 67 | + assertNotNull(r); |
| 68 | + assertNotNull("setCookieHeaders must be populated on the WebAuthn path", r.setCookieHeaders); |
| 69 | + assertTrue(r.setCookieHeaders.stream().anyMatch(c -> c.contains("pi_remember_device"))); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void passkeySurfacesSetCookie() |
| 74 | + { |
| 75 | + PIResponse r = privacyIDEA.validateCheckPasskey("txn-2", "{}", "https://origin", Collections.emptyMap()); |
| 76 | + assertNotNull(r); |
| 77 | + assertNotNull("setCookieHeaders must be populated on the passkey path", r.setCookieHeaders); |
| 78 | + assertTrue(r.setCookieHeaders.stream().anyMatch(c -> c.contains("pi_remember_device"))); |
| 79 | + } |
| 80 | + |
| 81 | + @After |
| 82 | + public void tearDown() |
| 83 | + { |
| 84 | + mockServer.stop(); |
| 85 | + } |
| 86 | +} |
0 commit comments