/** * Creates a new instance */ public OpenIDLoginConfigurer() { super(new OpenIDAuthenticationFilter(), "/login/openid"); }
/** * Created by the <tt>OpenIDAuthenticationProvider</tt> on successful authentication. * * @param principal usually the <tt>UserDetails</tt> returned by the configured * <tt>UserDetailsService</tt> used by the <tt>OpenIDAuthenticationProvider</tt>. * */ public OpenIDAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities, String identityUrl, List<OpenIDAttribute> attributes) { super(authorities); this.principal = principal; this.status = OpenIDAuthenticationStatus.SUCCESS; this.identityUrl = identityUrl; this.message = null; this.attributes = attributes; setAuthenticated(true); }
/** * Creates an {@link RegexBasedAxFetchListFactory} using the attributes populated by * {@link AttributeExchangeConfigurer} * * @return the {@link AxFetchListFactory} to use */ private AxFetchListFactory attributesToFetchFactory() { Map<String, List<OpenIDAttribute>> identityToAttrs = new HashMap<String, List<OpenIDAttribute>>(); for (AttributeExchangeConfigurer conf : this.attributeExchangeConfigurers) { identityToAttrs.put(conf.identifier, conf.getAttributes()); } return new RegexBasedAxFetchListFactory(identityToAttrs); }
@Test public void testIgnoresUserPassAuthToken() { OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider(); provider.setUserDetailsService(new MockUserDetailsService()); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( USERNAME, "password"); assertThat(provider.authenticate(token)).isNull(); }
@Test public void fetchAttributesReturnsExpectedValues() throws Exception { OpenID4JavaConsumer consumer = new OpenID4JavaConsumer( new NullAxFetchListFactory()); Message msg = mock(Message.class); FetchResponse fr = mock(FetchResponse.class); when(msg.hasExtension(AxMessage.OPENID_NS_AX)).thenReturn(true); when(msg.getExtension(AxMessage.OPENID_NS_AX)).thenReturn(fr); when(fr.getAttributeValues("a")).thenReturn(Arrays.asList("x", "y")); List<OpenIDAttribute> fetched = consumer.fetchAxAttributes(msg, attributes); assertThat(fetched).hasSize(1); assertThat(fetched.get(0).getValues()).hasSize(2); }
@Test public void testAuthenticateCancel() { OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider(); provider.setUserDetailsService(new MockUserDetailsService()); provider.setAuthoritiesMapper(new NullAuthoritiesMapper()); Authentication preAuth = new OpenIDAuthenticationToken( OpenIDAuthenticationStatus.CANCELLED, USERNAME, "", null); assertThat(preAuth.isAuthenticated()).isFalse(); try { provider.authenticate(preAuth); fail("Should throw an AuthenticationException"); } catch (AuthenticationCancelledException expected) { assertThat(expected.getMessage()).isEqualTo("Log in cancelled"); } }
@Test public void testDoesntSupport() { OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider(); provider.setUserDetailsService(new MockUserDetailsService()); assertThat( provider.supports(UsernamePasswordAuthenticationToken.class)).isFalse(); }
@Before public void setUp() throws Exception { filter = new OpenIDAuthenticationFilter(); filter.setConsumer(new MockOpenIDConsumer(REDIRECT_URL)); SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); filter.setAuthenticationSuccessHandler(new SavedRequestAwareAuthenticationSuccessHandler()); successHandler.setDefaultTargetUrl(DEFAULT_TARGET_URL); filter.setAuthenticationManager(new AuthenticationManager() { public Authentication authenticate(Authentication a) { return a; } }); filter.afterPropertiesSet(); }
@Override public void init(H http) throws Exception { super.init(http); OpenIDAuthenticationProvider authenticationProvider = new OpenIDAuthenticationProvider(); authenticationProvider.setAuthenticationUserDetailsService( getAuthenticationUserDetailsService(http)); authenticationProvider = postProcess(authenticationProvider); http.authenticationProvider(authenticationProvider); initDefaultLoginFilter(http); }
@Test(expected = OpenIDConsumerException.class) public void missingDiscoveryInformationThrowsException() throws Exception { OpenID4JavaConsumer consumer = new OpenID4JavaConsumer( new NullAxFetchListFactory()); consumer.endConsumption(new MockHttpServletRequest()); }
@Test public void additionalConstructorsWork() throws Exception { new OpenID4JavaConsumer(); new OpenID4JavaConsumer(new MockAttributesFactory()); }
@Test public void testValidation() throws Exception { OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider(); try { provider.afterPropertiesSet(); fail("IllegalArgumentException expected, ssoAuthoritiesPopulator is null"); } catch (IllegalArgumentException e) { // expected } provider = new OpenIDAuthenticationProvider(); provider.setUserDetailsService(new MockUserDetailsService()); provider.afterPropertiesSet(); }
/** * Gets the {@link OpenIDConsumer} that was configured or defaults to an * {@link OpenID4JavaConsumer}. * @return the {@link OpenIDConsumer} to use * @throws ConsumerException */ private OpenIDConsumer getConsumer() throws ConsumerException { if (this.openIDConsumer == null) { this.openIDConsumer = new OpenID4JavaConsumer(getConsumerManager(), attributesToFetchFactory()); } return this.openIDConsumer; }
@Test public void testDetectsMissingAuthoritiesPopulator() throws Exception { OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider(); try { provider.afterPropertiesSet(); fail("Should have thrown Exception"); } catch (IllegalArgumentException expected) { // ignored } }
@Override public void configure(H http) throws Exception { getAuthenticationFilter().setConsumer(getConsumer()); super.configure(http); }
public OpenID4JavaConsumer() throws ConsumerException { this(new ConsumerManager(), new NullAxFetchListFactory()); }
@Test public void testAuthenticateError() { OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider(); provider.setUserDetailsService(new MockUserDetailsService()); Authentication preAuth = new OpenIDAuthenticationToken( OpenIDAuthenticationStatus.ERROR, USERNAME, "", null); assertThat(preAuth.isAuthenticated()).isFalse(); try { provider.authenticate(preAuth); fail("Should throw an AuthenticationException"); } catch (AuthenticationServiceException expected) { assertThat(expected.getMessage()).isEqualTo("Error message from server: "); } }
@Test public void testSupports() { OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider(); provider.setUserDetailsService(new MockUserDetailsService()); assertThat(provider.supports(OpenIDAuthenticationToken.class)).isTrue(); }
public OpenIDAuthenticationToken(OpenIDAuthenticationStatus status, String identityUrl, String message, List<OpenIDAttribute> attributes) { super(new ArrayList<>(0)); this.principal = identityUrl; this.status = status; this.identityUrl = identityUrl; this.message = message; this.attributes = attributes; setAuthenticated(false); }
@Test public void testAuthenticateSetupNeeded() { OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider(); provider.setUserDetailsService(new MockUserDetailsService()); Authentication preAuth = new OpenIDAuthenticationToken( OpenIDAuthenticationStatus.SETUP_NEEDED, USERNAME, "", null); assertThat(preAuth.isAuthenticated()).isFalse(); try { provider.authenticate(preAuth); fail("Should throw an AuthenticationException"); } catch (AuthenticationServiceException expected) { assertThat( "The server responded setup was needed, which shouldn't happen").isEqualTo( expected.getMessage()); } }