@Test public void shouldAuthenticate_skipExternalProvider() { AuthenticationProvider authenticationProvider2 = mock(AuthenticationProvider.class); when(authenticationProvider2.loadUserByUsername(any(io.gravitee.am.identityprovider.api.Authentication.class))).thenReturn(Maybe.just(new DefaultUser("username"))); IdentityProvider externalIdp = new IdentityProvider(); externalIdp.setExternal(true); when(identityProviderManager.getIdentityProvider("idp1")).thenReturn(externalIdp); when(identityProviderManager.getIdentityProvider("idp2")).thenReturn(new IdentityProvider()); when(identityProviderManager.get("idp2")).thenReturn(authenticationProvider2); Authentication authentication = managementAuthenticationProvider.authenticate(new UsernamePasswordAuthenticationToken("username", "password")); Assert.assertNotNull(authentication); verify(identityProviderManager, times(0)).get("idp1"); verify(identityProviderManager, times(1)).get("idp2"); }
@Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { WebAuthenticationDetails webAuthenticationDetails = (WebAuthenticationDetails) authentication.getDetails(); Map<String, String> details = new HashMap(); details.put(Claims.organization, Organization.DEFAULT); if (webAuthenticationDetails != null) { details.put(Claims.ip_address, webAuthenticationDetails.getRemoteAddress()); details.put(Claims.user_agent, webAuthenticationDetails.getUserAgent()); } Organization organization = organizationService.findById(Organization.DEFAULT).blockingGet(); List<String> identities = organization.getIdentities(); Iterator<String> iter = identities.iterator(); io.gravitee.am.identityprovider.api.User user = null; AuthenticationException lastException = null; io.gravitee.am.identityprovider.api.Authentication provAuthentication = new EndUserAuthentication( authentication.getName(), authentication.getCredentials(), new SimpleAuthenticationContext()); while (iter.hasNext() && user == null) { String provider = iter.next(); IdentityProvider identityProvider = identityProviderManager.getIdentityProvider(provider); if(identityProvider == null || identityProvider.isExternal()) { continue; } io.gravitee.am.identityprovider.api.AuthenticationProvider authenticationProvider = identityProviderManager.get(provider); if (authenticationProvider == null) { lastException = new BadCredentialsException("Unable to load authentication provider " + provider + ", an error occurred during the initialization stage"); continue; } try { user = authenticationProvider.loadUserByUsername(provAuthentication).blockingGet(); details.put(SOURCE, provider); lastException = null; } catch (Exception ex) { logger.info("Unable to authenticate user {} with provider {}", authentication.getName(), provider, ex); lastException = new BadCredentialsException(ex.getMessage(), ex); } } if (lastException != null) { throw lastException; } if (user != null) { UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user, provAuthentication.getCredentials(), AuthorityUtils.NO_AUTHORITIES); authenticationToken.setDetails(details); return authenticationToken; } throw new BadCredentialsException("No user found for providers " + StringUtils.collectionToDelimitedString(identities, ",")); }
ManagementAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { WebAuthenticationDetails webAuthenticationDetails = (WebAuthenticationDetails) authentication.getDetails(); Map<String, String> details = new HashMap(); details.put(Claims.organization, Organization.DEFAULT); if (webAuthenticationDetails != null) { details.put(Claims.ip_address, webAuthenticationDetails.getRemoteAddress()); details.put(Claims.user_agent, webAuthenticationDetails.getUserAgent()); } Organization organization = organizationService.findById(Organization.DEFAULT).blockingGet(); List<String> identities = organization.getIdentities(); Iterator<String> iter = identities.iterator(); io.gravitee.am.identityprovider.api.User user = null; AuthenticationException lastException = null; io.gravitee.am.identityprovider.api.Authentication provAuthentication = new EndUserAuthentication( authentication.getName(), authentication.getCredentials(), new SimpleAuthenticationContext()); while (iter.hasNext() && user == null) { String provider = iter.next(); IdentityProvider identityProvider = identityProviderManager.getIdentityProvider(provider); if(identityProvider == null || identityProvider.isExternal()) { continue; } io.gravitee.am.identityprovider.api.AuthenticationProvider authenticationProvider = identityProviderManager.get(provider); if (authenticationProvider == null) { lastException = new BadCredentialsException("Unable to load authentication provider " + provider + ", an error occurred during the initialization stage"); continue; } try { user = authenticationProvider.loadUserByUsername(provAuthentication).blockingGet(); details.put(SOURCE, provider); lastException = null; } catch (Exception ex) { logger.info("Unable to authenticate user {} with provider {}", authentication.getName(), provider, ex); lastException = new BadCredentialsException(ex.getMessage(), ex); } } if (lastException != null) { throw lastException; } if (user != null) { UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user, provAuthentication.getCredentials(), AuthorityUtils.NO_AUTHORITIES); authenticationToken.setDetails(details); return authenticationToken; } throw new BadCredentialsException("No user found for providers " + StringUtils.collectionToDelimitedString(identities, ",")); } }
ManagementAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { WebAuthenticationDetails webAuthenticationDetails = (WebAuthenticationDetails) authentication.getDetails(); Map<String, String> details = new HashMap(); details.put(Claims.organization, Organization.DEFAULT); if (webAuthenticationDetails != null) { details.put(Claims.ip_address, webAuthenticationDetails.getRemoteAddress()); details.put(Claims.user_agent, webAuthenticationDetails.getUserAgent()); } Organization organization = organizationService.findById(Organization.DEFAULT).blockingGet(); List<String> identities = organization.getIdentities(); Iterator<String> iter = identities.iterator(); io.gravitee.am.identityprovider.api.User user = null; AuthenticationException lastException = null; io.gravitee.am.identityprovider.api.Authentication provAuthentication = new EndUserAuthentication( authentication.getName(), authentication.getCredentials(), new SimpleAuthenticationContext()); while (iter.hasNext() && user == null) { String provider = iter.next(); IdentityProvider identityProvider = identityProviderManager.getIdentityProvider(provider); if(identityProvider == null || identityProvider.isExternal()) { continue; } io.gravitee.am.identityprovider.api.AuthenticationProvider authenticationProvider = identityProviderManager.get(provider); if (authenticationProvider == null) { lastException = new BadCredentialsException("Unable to load authentication provider " + provider + ", an error occurred during the initialization stage"); continue; } try { user = authenticationProvider.loadUserByUsername(provAuthentication).blockingGet(); details.put(SOURCE, provider); lastException = null; } catch (Exception ex) { logger.info("Unable to authenticate user {} with provider {}", authentication.getName(), provider, ex); lastException = new BadCredentialsException(ex.getMessage(), ex); } } if (lastException != null) { throw lastException; } if (user != null) { UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user, provAuthentication.getCredentials(), AuthorityUtils.NO_AUTHORITIES); authenticationToken.setDetails(details); return authenticationToken; } throw new BadCredentialsException("No user found for providers " + StringUtils.collectionToDelimitedString(identities, ",")); }  }
ManagementAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { WebAuthenticationDetails webAuthenticationDetails = (WebAuthenticationDetails) authentication.getDetails(); Map<String, String> details = new HashMap(); details.put(Claims.organization, Organization.DEFAULT); if (webAuthenticationDetails != null) { details.put(Claims.ip_address, webAuthenticationDetails.getRemoteAddress()); details.put(Claims.user_agent, webAuthenticationDetails.getUserAgent()); } Organization organization = organizationService.findById(Organization.DEFAULT).blockingGet(); List<String> identities = organization.getIdentities(); Iterator<String> iter = identities.iterator(); io.gravitee.am.identityprovider.api.User user = null; AuthenticationException lastException = null; io.gravitee.am.identityprovider.api.Authentication provAuthentication = new EndUserAuthentication( authentication.getName(), authentication.getCredentials(), new SimpleAuthenticationContext()); while (iter.hasNext() && user == null) { String provider = iter.next(); IdentityProvider identityProvider = identityProviderManager.getIdentityProvider(provider); if(identityProvider == null || identityProvider.isExternal()) { continue; } io.gravitee.am.identityprovider.api.AuthenticationProvider authenticationProvider = identityProviderManager.get(provider); if (authenticationProvider == null) { lastException = new BadCredentialsException("Unable to load authentication provider " + provider + ", an error occurred during the initialization stage"); continue; } try { user = authenticationProvider.loadUserByUsername(provAuthentication).blockingGet(); details.put(SOURCE, provider); lastException = null; } catch (Exception ex) { logger.info("Unable to authenticate user {} with provider {}", authentication.getName(), provider, ex); lastException = new BadCredentialsException(ex.getMessage(), ex); } } if (lastException != null) { throw lastException; } if (user != null) { UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user, provAuthentication.getCredentials(), AuthorityUtils.NO_AUTHORITIES); authenticationToken.setDetails(details); return authenticationToken; } throw new BadCredentialsException("No user found for providers " + StringUtils.collectionToDelimitedString(identities, ",")); }  @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> authentication); void setIdentityProviderManager(IdentityProviderManager identityProviderManager); }
ManagementAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { WebAuthenticationDetails webAuthenticationDetails = (WebAuthenticationDetails) authentication.getDetails(); Map<String, String> details = new HashMap(); details.put(Claims.organization, Organization.DEFAULT); if (webAuthenticationDetails != null) { details.put(Claims.ip_address, webAuthenticationDetails.getRemoteAddress()); details.put(Claims.user_agent, webAuthenticationDetails.getUserAgent()); } Organization organization = organizationService.findById(Organization.DEFAULT).blockingGet(); List<String> identities = organization.getIdentities(); Iterator<String> iter = identities.iterator(); io.gravitee.am.identityprovider.api.User user = null; AuthenticationException lastException = null; io.gravitee.am.identityprovider.api.Authentication provAuthentication = new EndUserAuthentication( authentication.getName(), authentication.getCredentials(), new SimpleAuthenticationContext()); while (iter.hasNext() && user == null) { String provider = iter.next(); IdentityProvider identityProvider = identityProviderManager.getIdentityProvider(provider); if(identityProvider == null || identityProvider.isExternal()) { continue; } io.gravitee.am.identityprovider.api.AuthenticationProvider authenticationProvider = identityProviderManager.get(provider); if (authenticationProvider == null) { lastException = new BadCredentialsException("Unable to load authentication provider " + provider + ", an error occurred during the initialization stage"); continue; } try { user = authenticationProvider.loadUserByUsername(provAuthentication).blockingGet(); details.put(SOURCE, provider); lastException = null; } catch (Exception ex) { logger.info("Unable to authenticate user {} with provider {}", authentication.getName(), provider, ex); lastException = new BadCredentialsException(ex.getMessage(), ex); } } if (lastException != null) { throw lastException; } if (user != null) { UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user, provAuthentication.getCredentials(), AuthorityUtils.NO_AUTHORITIES); authenticationToken.setDetails(details); return authenticationToken; } throw new BadCredentialsException("No user found for providers " + StringUtils.collectionToDelimitedString(identities, ",")); }  @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> authentication); void setIdentityProviderManager(IdentityProviderManager identityProviderManager);  }