|
12 | 12 | * information: "Portions copyright [year] [name of copyright owner]".
|
13 | 13 | *
|
14 | 14 | * Copyright 2013-2016 ForgeRock AS.
|
15 |
| - * Portions Copyright 2018 Wren Security. |
| 15 | + * Portions Copyright 2018-2025 Wren Security. |
16 | 16 | */
|
17 | 17 |
|
18 | 18 | package org.forgerock.caf.authentication.framework;
|
|
24 | 24 | import static org.mockito.Mockito.mock;
|
25 | 25 | import static org.mockito.Mockito.never;
|
26 | 26 | import static org.mockito.Mockito.verify;
|
| 27 | +import static org.testng.Assert.assertEquals; |
27 | 28 |
|
| 29 | +import java.security.Principal; |
28 | 30 | import javax.security.auth.Subject;
|
29 | 31 | import javax.security.auth.message.AuthStatus;
|
30 | 32 |
|
|
44 | 46 | import org.forgerock.util.promise.NeverThrowsException;
|
45 | 47 | import org.forgerock.util.promise.Promise;
|
46 | 48 | import org.forgerock.util.promise.Promises;
|
| 49 | +import org.mockito.invocation.InvocationOnMock; |
| 50 | +import org.mockito.stubbing.Answer; |
47 | 51 | import org.slf4j.Logger;
|
48 | 52 | import org.testng.annotations.BeforeMethod;
|
49 | 53 | import org.testng.annotations.DataProvider;
|
@@ -349,4 +353,59 @@ public void whenResourceReturnsResponseExceptionItShouldBeSecuredAndReturned() t
|
349 | 353 | verify(authContext).secureResponse(any(MessageContext.class), eq(serviceSubject));
|
350 | 354 | verify(authContext).cleanSubject(any(MessageContext.class), any(Subject.class));
|
351 | 355 | }
|
| 356 | + |
| 357 | + @Test |
| 358 | + public void whenProcessingResultShouldSetPrincipalFromMessageContext() { |
| 359 | + String principal = "john.doe"; |
| 360 | + Context context = mockContext(); |
| 361 | + Request request = new Request(); |
| 362 | + Handler next = mockHandler(request, Promises.<Response, NeverThrowsException>newResultPromise(successfulResponse)); |
| 363 | + mockAuthContext(Promises.<AuthStatus, AuthenticationException>newResultPromise(AuthStatus.SUCCESS), |
| 364 | + Promises.<AuthStatus, AuthenticationException>newResultPromise(AuthStatus.SEND_SUCCESS)); |
| 365 | + given(authContext.validateRequest(any(MessageContext.class), any(Subject.class), eq(serviceSubject))).willAnswer(new Answer<Object>() { |
| 366 | + @Override |
| 367 | + public Object answer(InvocationOnMock invocation) throws Throwable { |
| 368 | + MessageContext context = (MessageContext) invocation.getArgument(0); |
| 369 | + context.getRequestContextMap().put(AuthenticationFramework.ATTRIBUTE_AUTH_PRINCIPAL, principal); |
| 370 | + return Promises.<AuthStatus, AuthenticationException>newResultPromise(AuthStatus.SUCCESS); |
| 371 | + } |
| 372 | + }); |
| 373 | + |
| 374 | + Promise<Response, NeverThrowsException> promise = runtime.processMessage(context, request, next); |
| 375 | + |
| 376 | + assertThat(promise).succeeded(); |
| 377 | + AttributesContext attributesContext = context.asContext(AttributesContext.class); |
| 378 | + assertEquals(attributesContext.getAttributes().get(AuthenticationFramework.ATTRIBUTE_AUTH_PRINCIPAL), principal); |
| 379 | + } |
| 380 | + |
| 381 | + @Test |
| 382 | + public void whenProcessingResultShouldSetPrincipalFromClientSubject() { |
| 383 | + String principal = "john.doe"; |
| 384 | + Context context = mockContext(); |
| 385 | + Request request = new Request(); |
| 386 | + Handler next = mockHandler(request, Promises.<Response, NeverThrowsException>newResultPromise(successfulResponse)); |
| 387 | + mockAuthContext(Promises.<AuthStatus, AuthenticationException>newResultPromise(AuthStatus.SUCCESS), |
| 388 | + Promises.<AuthStatus, AuthenticationException>newResultPromise(AuthStatus.SEND_SUCCESS)); |
| 389 | + given(authContext.validateRequest(any(MessageContext.class), any(Subject.class), eq(serviceSubject))).willAnswer(new Answer<Object>() { |
| 390 | + @Override |
| 391 | + public Object answer(InvocationOnMock invocation) throws Throwable { |
| 392 | + Subject subject = (Subject) invocation.getArgument(1); |
| 393 | + subject.getPrincipals().add(new Principal() { |
| 394 | + @Override |
| 395 | + public String getName() { |
| 396 | + return principal; |
| 397 | + } |
| 398 | + |
| 399 | + }); |
| 400 | + return Promises.<AuthStatus, AuthenticationException>newResultPromise(AuthStatus.SUCCESS); |
| 401 | + } |
| 402 | + }); |
| 403 | + |
| 404 | + Promise<Response, NeverThrowsException> promise = runtime.processMessage(context, request, next); |
| 405 | + |
| 406 | + assertThat(promise).succeeded(); |
| 407 | + AttributesContext attributesContext = context.asContext(AttributesContext.class); |
| 408 | + assertEquals(attributesContext.getAttributes().get(AuthenticationFramework.ATTRIBUTE_AUTH_PRINCIPAL), principal); |
| 409 | + } |
| 410 | + |
352 | 411 | }
|
0 commit comments