AbstractLoginModule implements LoginModule { @Nullable protected PrincipalProvider getPrincipalProvider() { PrincipalProvider principalProvider = null; SecurityProvider sp = getSecurityProvider(); Root r = getRoot(); if (r != null && sp != null) { PrincipalConfiguration pc = sp.getConfiguration(PrincipalConfiguration.class); principalProvider = pc.getPrincipalProvider(r, NamePathMapper.DEFAULT); } if (principalProvider == null && callbackHandler != null) { try { PrincipalProviderCallback principalCallBack = new PrincipalProviderCallback(); callbackHandler.handle(new Callback[]{principalCallBack}); principalProvider = principalCallBack.getPrincipalProvider(); } catch (IOException | UnsupportedCallbackException e) { onError(); log.error(e.getMessage(), e); } } return principalProvider; }  @Override void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options); @Override boolean logout(); @Override boolean abort(); static final String SHARED_KEY_CREDENTIALS; static final String SHARED_KEY_LOGIN_NAME; static final String SHARED_KEY_ATTRIBUTES; static final String SHARED_KEY_PRE_AUTH_LOGIN; }
@Test public void testGetPrincipalProviderFromCallback() { AbstractLoginModule loginModule = initLoginModule(new TestCallbackHandler(new TestPrincipalProvider())); assertNotNull(loginModule.getPrincipalProvider()); PrincipalProvider principalProvider = loginModule.getPrincipalProvider(); assertNotNull(principalProvider); assertSame(principalProvider, loginModule.getPrincipalProvider()); }
@Test public void testGetPrincipalProviderFromIncompleteCallback() { AbstractLoginModule loginModule = initLoginModule(new TestCallbackHandler()); assertNull(loginModule.getPrincipalProvider()); }
@Test public void testGetPrincipalProviderIOException() { AbstractLoginModule loginModule = initLoginModule(new ThrowingCallbackHandler(true)); assertNull(loginModule.getPrincipalProvider()); }
@Test public void testGetPrincipalProviderUnsupportedCallbackException() { AbstractLoginModule loginModule = initLoginModule(new ThrowingCallbackHandler(false)); assertNull(loginModule.getPrincipalProvider()); }
@Test public void testGetPrincipalProviderWithRepositoryCallbackHandler() throws Exception { Root r = mock(Root.class); ContentSession cs = when(mock(ContentSession.class).getLatestRoot()).thenReturn(r).getMock(); ContentRepository cp = when(mock(ContentRepository.class).login(null, null)).thenReturn(cs).getMock(); PrincipalProvider pp = mock(PrincipalProvider.class); PrincipalConfiguration pc = when(mock(PrincipalConfiguration.class).getPrincipalProvider(r, NamePathMapper.DEFAULT)).thenReturn(pp).getMock(); SecurityProvider sp = when(mock(SecurityProvider.class).getConfiguration(PrincipalConfiguration.class)).thenReturn(pc).getMock(); CallbackHandler cbh = new TestCallbackHandler(cp, sp); AbstractLoginModule loginModule = initLoginModule(cbh); assertEquals(pp, loginModule.getPrincipalProvider()); }
@Test public void testGetPrincipalProviderMissingCallbackHandler() { AbstractLoginModule loginModule = initLoginModule((CallbackHandler) null); assertNull(loginModule.getPrincipalProvider()); }
@Test public void testGetPrincipalProviderMissingRoot() throws Exception { ContentRepository cp = when(mock(ContentRepository.class).login(null, null)).thenReturn(mock(ContentSession.class)).getMock(); CallbackHandler cbh = new TestCallbackHandler(cp, mock(SecurityProvider.class)); AbstractLoginModule loginModule = initLoginModule(cbh); assertNull(loginModule.getPrincipalProvider()); }
@Test public void testGetPrincipalProviderMissingSecurityProvider() throws Exception { ContentSession cs = when(mock(ContentSession.class).getLatestRoot()).thenReturn(mock(Root.class)).getMock(); ContentRepository cp = when(mock(ContentRepository.class).login(null, null)).thenReturn(cs).getMock(); CallbackHandler cbh = new TestCallbackHandler(cp, null); AbstractLoginModule loginModule = initLoginModule(cbh); assertNull(loginModule.getPrincipalProvider()); }