| 1 |  |  | package org.apereo.cas.web.flow.actions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import org.apereo.cas.authentication.Authentication; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import org.apereo.cas.authentication.AuthenticationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import org.apereo.cas.authentication.ContextualAuthenticationPolicy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | import org.apereo.cas.ticket.InvalidTicketException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | import org.apereo.cas.ticket.UnsatisfiedAuthenticationPolicyException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | import org.apereo.cas.util.CollectionUtils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | import org.junit.Test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | import org.junit.runner.RunWith; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | import org.junit.runners.JUnit4; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | import org.mockito.ArgumentCaptor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | import org.springframework.binding.message.DefaultMessageResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | import org.springframework.binding.message.MessageContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | import org.springframework.webflow.execution.RequestContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | import javax.security.auth.login.AccountLockedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | import javax.security.auth.login.AccountNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | import java.security.GeneralSecurityException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | import java.util.HashMap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | import java.util.Map; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | import java.util.Optional; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | import static org.junit.Assert.*; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | import static org.mockito.Mockito.*; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @author Marvin S. Addison | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @since 4.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | @RunWith(JUnit4.class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | public class AuthenticationExceptionHandlerActionTests { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |   | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     @Test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public void handleAccountNotFoundExceptionByDefault() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         final AuthenticationExceptionHandlerAction handler = new AuthenticationExceptionHandlerAction( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 CollectionUtils.wrapSet(AccountLockedException.class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                         AccountNotFoundException.class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         final RequestContext req = getMockRequestContext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         final Map<String, Class<? extends Throwable>> map = new HashMap<>(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         map.put("notFound", AccountNotFoundException.class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         final String id = handler.handle(new AuthenticationException(map), req); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         assertEquals(id, AccountNotFoundException.class.getSimpleName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     private RequestContext getMockRequestContext() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         final RequestContext ctx = mock(RequestContext.class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         when(ctx.getMessageContext()).thenReturn(mock(MessageContext.class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return ctx; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     @Test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public void handleUnknownExceptionByDefault() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         final AuthenticationExceptionHandlerAction handler = new AuthenticationExceptionHandlerAction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         final RequestContext req = getMockRequestContext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         final Map<String, Class<? extends Throwable>> map = new HashMap<>(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         map.put("unknown", GeneralSecurityException.class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         final String id = handler.handle(new AuthenticationException(map), req); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         assertEquals(id, "UNKNOWN"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     @Test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public void handleUnknownTicketExceptionByDefault() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         final AuthenticationExceptionHandlerAction handler = new AuthenticationExceptionHandlerAction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         final RequestContext req = getMockRequestContext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         final String id = handler.handle(new InvalidTicketException("TGT"), req); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         assertEquals(id, "UNKNOWN"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     @Test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public void handleUnsatisfiedAuthenticationPolicyExceptionByDefault() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         final AuthenticationExceptionHandlerAction handler = new AuthenticationExceptionHandlerAction( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 CollectionUtils.wrapSet(UnsatisfiedAuthenticationPolicyException.class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                         AccountNotFoundException.class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         final RequestContext req = getMockRequestContext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         final ContextualAuthenticationPolicy<?> policy = new TestContextualAuthenticationPolicy(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         final String id = handler.handle(new UnsatisfiedAuthenticationPolicyException(policy), req); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         assertEquals(id, "UnsatisfiedAuthenticationPolicyException"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         final ArgumentCaptor<DefaultMessageResolver> message = ArgumentCaptor.forClass(DefaultMessageResolver.class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         verify(req.getMessageContext(), times(1)).addMessage(message.capture()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         assertArrayEquals(new String[]{policy.getCode().get()}, message.getValue().getCodes()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     private static class TestContextualAuthenticationPolicy implements ContextualAuthenticationPolicy<Object> { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         @Override | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         public Optional<String> getCode() { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             return Optional.of("CUSTOM_CODE"); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         @Override | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         public Object getContext() { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         @Override | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         public boolean isSatisfiedBy(final Authentication authentication) { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |