DirectAccessGrantAuthenticationProvider implements AuthenticationProvider { protected String resolveUsername(Object principal) { if (principal instanceof String) return (String) principal; if (principal instanceof UserDetails) return ((UserDetails)principal).getUsername(); throw new AuthenticationCredentialsNotFoundException("Can't find username on: " + principal); }  @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> authentication); @Required void setKeycloakDeployment(KeycloakDeployment keycloakDeployment); @Required void setDirectAccessGrantService(DirectAccessGrantService directAccessGrantService); void setGrantedAuthoritiesMapper(GrantedAuthoritiesMapper grantedAuthoritiesMapper);  }
@Test public void testResolveUsername() throws Exception { String username = directAccessGrantAuthenticationProvider.resolveUsername(AppConfig.KNOWN_USERNAME); assertEquals(AppConfig.KNOWN_USERNAME, username); }
@Test public void testResolveUsernameFromUserDetails() throws Exception { User user = new User(AppConfig.KNOWN_USERNAME, AppConfig.KNOWN_PASSWORD, Collections.singleton(new KeycloakRole("user"))); String username = directAccessGrantAuthenticationProvider.resolveUsername(user); assertEquals(AppConfig.KNOWN_USERNAME, username); }
@Test(expected = AuthenticationCredentialsNotFoundException.class) public void testResolveUsernameUnknownPricipalClass() throws Exception { directAccessGrantAuthenticationProvider.resolveUsername(new Object()); }
@Test(expected = AuthenticationCredentialsNotFoundException.class) public void testResolveUsernameNull() throws Exception { directAccessGrantAuthenticationProvider.resolveUsername(null); }