/** * Creates a LoginContext using the Configuration that was specified in * {@link #setConfiguration(Configuration)}. */ @Override protected LoginContext createLoginContext(CallbackHandler handler) throws LoginException { return new LoginContext(getLoginContextName(), null, handler, getConfiguration()); }
@Before public void setUp() throws Exception { Configuration configuration = mock(Configuration.class); publisher = mock(ApplicationEventPublisher.class); log = mock(Log.class); provider = new DefaultJaasAuthenticationProvider(); provider.setConfiguration(configuration); provider.setApplicationEventPublisher(publisher); provider.setAuthorityGranters(new AuthorityGranter[] { new TestAuthorityGranter() }); provider.afterPropertiesSet(); AppConfigurationEntry[] aces = new AppConfigurationEntry[] { new AppConfigurationEntry( TestLoginModule.class.getName(), LoginModuleControlFlag.REQUIRED, Collections.<String, Object> emptyMap()) }; when(configuration.getAppConfigurationEntry(provider.getLoginContextName())) .thenReturn(aces); token = new UsernamePasswordAuthenticationToken("user", "password"); ReflectionTestUtils.setField(provider, "log", log); }
@Test(expected = IllegalArgumentException.class) public void afterPropertiesSetNullAuthorityGranters() throws Exception { provider.setAuthorityGranters(null); provider.afterPropertiesSet(); }
@Test(expected = IllegalArgumentException.class) public void afterPropertiesSetNullConfiguration() throws Exception { provider.setConfiguration(null); provider.afterPropertiesSet(); }
private void init() { try { AppConfigurationEntry appConfigurationEntry = new AppConfigurationEntry( loginModuleName, controlFlag, options); AppConfigurationEntry[] appConfigurationEntries = new AppConfigurationEntry[]{appConfigurationEntry}; Map<String, AppConfigurationEntry[]> appConfigurationEntriesOptions = new HashMap<String, AppConfigurationEntry[]>(); appConfigurationEntriesOptions.put("SPRINGSECURITY", appConfigurationEntries); Configuration configuration = new InMemoryConfiguration( appConfigurationEntriesOptions); jaasAuthenticationProvider.setConfiguration(configuration); UserAuthorityGranter authorityGranter = new UserAuthorityGranter(); UserAuthorityGranter[] authorityGranters = new UserAuthorityGranter[]{authorityGranter}; jaasAuthenticationProvider.setAuthorityGranters(authorityGranters); jaasAuthenticationProvider.afterPropertiesSet(); } catch (Exception e) { LOG.error("Failed to init PAM Authentication", e); } } }
@Test public void authenticateSuccess() throws Exception { Authentication auth = provider.authenticate(token); assertThat(auth.getPrincipal()).isEqualTo(token.getPrincipal()); assertThat(auth.getCredentials()).isEqualTo(token.getCredentials()); assertThat(auth.isAuthenticated()).isEqualTo(true); assertThat(auth.getAuthorities().isEmpty()).isEqualTo(false); verify(publisher).publishEvent(isA(JaasAuthenticationSuccessEvent.class)); verifyNoMoreInteractions(publisher); }
@Test public void javadocExample() { String resName = "/" + getClass().getName().replace('.', '/') + ".xml"; ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( resName); context.registerShutdownHook(); try { provider = context.getBean(DefaultJaasAuthenticationProvider.class); Authentication auth = provider.authenticate(token); assertThat(auth.isAuthenticated()).isEqualTo(true); assertThat(auth.getPrincipal()).isEqualTo(token.getPrincipal()); } finally { context.close(); } }
/** * Creates a LoginContext using the Configuration that was specified in * {@link #setConfiguration(Configuration)}. */ @Override protected LoginContext createLoginContext(CallbackHandler handler) throws LoginException { return new LoginContext(getLoginContextName(), null, handler, getConfiguration()); }
@Test public void authenticateBadPassword() { try { provider.authenticate(new UsernamePasswordAuthenticationToken("user", "asdf")); fail("LoginException should have been thrown for the bad password"); } catch (AuthenticationException success) { } verifyFailedLogin(); }
/** * Creates a LoginContext using the Configuration that was specified in * {@link #setConfiguration(Configuration)}. */ @Override protected LoginContext createLoginContext(CallbackHandler handler) throws LoginException { return new LoginContext(getLoginContextName(), null, handler, getConfiguration()); }
@Test public void authenticateBadUser() { try { provider.authenticate(new UsernamePasswordAuthenticationToken("asdf", "password")); fail("LoginException should have been thrown for the bad user"); } catch (AuthenticationException success) { } verifyFailedLogin(); }
/** * Creates a LoginContext using the Configuration that was specified in * {@link #setConfiguration(Configuration)}. */ @Override protected LoginContext createLoginContext(CallbackHandler handler) throws LoginException { return new LoginContext(getLoginContextName(), null, handler, getConfiguration()); }
@Test public void authenticateUnsupportedAuthentication() { assertThat(provider.authenticate(new TestingAuthenticationToken("user", "password"))).isNull(); }
.authenticate(finalAuthentication);