RemoteUserFederationProvider implements UserFederationProvider { @Override public UserModel getUserByUsername(RealmModel realm, String username) { LOG.infof("Get by username: %s", username); try { return this.createUserModel(realm, username); } catch (NotFoundException ex) { LOG.errorf("Federated user not found: %s", username); return null; } } RemoteUserFederationProvider(KeycloakSession session, UserFederationProviderModel model, String uri); protected  RemoteUserFederationProvider(KeycloakSession session, UserFederationProviderModel model, FederatedUserService federatedUserService); @Override boolean synchronizeRegistrations(); @Override UserModel register(RealmModel realm, UserModel user); @Override boolean removeUser(RealmModel realm, UserModel user); @Override UserModel getUserByUsername(RealmModel realm, String username); @Override UserModel getUserByEmail(RealmModel realm, String email); @Override List<UserModel> searchByAttributes(Map<String, String> attributes, RealmModel realm, int maxResults); @Override void preRemove(RealmModel realm); @Override void preRemove(RealmModel realm, RoleModel role); @Override void preRemove(RealmModel realm, GroupModel group); @Override Set<String> getSupportedCredentialTypes(UserModel user); @Override Set<String> getSupportedCredentialTypes(); @Override boolean validCredentials(RealmModel realm, UserModel user, List<UserCredentialModel> input); @Override boolean validCredentials(RealmModel realm, UserModel user, UserCredentialModel... input); @Override CredentialValidationOutput validCredentials(RealmModel realm, UserCredentialModel credential); @Override UserModel validateAndProxy(RealmModel realm, UserModel local); @Override boolean isValid(RealmModel realm, UserModel local); @Override void close();  }
@Test public void testGetUserByUsername() throws Exception { assertNotNull(provider.getUserByUsername(realmModel, FEDERATED_USER_KNOWN_USERNAME)); verify(federatedUserService).getUserDetails(eq(FEDERATED_USER_KNOWN_EMAIL)); verify(federatedUserService, never()).getUserDetails(eq(KEYCLOAK_EXISTING_USER_USERNAME)); }
@Test public void testGetUserByUsernameWithRole() throws Exception { when(federatedUserModel.getRoles()).thenReturn(Sets.newSet(FEDERATED_USER_ROLE)); when(realmModel.getRole(FEDERATED_USER_ROLE)).thenReturn(mock(RoleModel.class)); when(keycloakSession.userStorage().addUser(eq(realmModel), eq(FEDERATED_USER_KNOWN_USERNAME))).thenReturn(userModel); assertNotNull(provider.getUserByUsername(realmModel, FEDERATED_USER_KNOWN_USERNAME)); verify(federatedUserService).getUserDetails(eq(FEDERATED_USER_KNOWN_EMAIL)); verify(userModel).grantRole(any(RoleModel.class)); }
@Test public void testGetUserByUsernameWithNullRoles() throws Exception { when(federatedUserModel.getRoles()).thenReturn(null); when(keycloakSession.userStorage().addUser(eq(realmModel), eq(FEDERATED_USER_KNOWN_USERNAME))).thenReturn(userModel); assertNotNull(provider.getUserByUsername(realmModel, FEDERATED_USER_KNOWN_USERNAME)); verify(federatedUserService).getUserDetails(eq(FEDERATED_USER_KNOWN_EMAIL)); verify(realmModel, never()).getRole(anyString()); verify(userModel, never()).grantRole(any(RoleModel.class)); }
@Test public void testGetUserByUsernameMixedCase() throws Exception { assertNotNull(provider.getUserByUsername(realmModel, FEDERATED_USER_KNOWN_USERNAME.toUpperCase())); verify(federatedUserService).getUserDetails(eq(FEDERATED_USER_KNOWN_USERNAME)); }
@Test public void testGetUserByUsernameWithLeadingSpace() throws Exception { assertNotNull(provider.getUserByUsername(realmModel, " " + FEDERATED_USER_KNOWN_USERNAME)); verify(federatedUserService).getUserDetails(eq(FEDERATED_USER_KNOWN_USERNAME)); }
@Test public void testGetUserByUsernameWithLeadingAndTrailingSpace() throws Exception { assertNotNull(provider.getUserByUsername(realmModel, " " + FEDERATED_USER_KNOWN_USERNAME + " \t")); verify(federatedUserService).getUserDetails(eq(FEDERATED_USER_KNOWN_USERNAME)); }
@Test public void testGetUserByUsernameWithTrailingSpace() throws Exception { assertNotNull(provider.getUserByUsername(realmModel, FEDERATED_USER_KNOWN_USERNAME + " ")); verify(federatedUserService).getUserDetails(eq(FEDERATED_USER_KNOWN_USERNAME)); }
@Test public void testGetUserByUsernameWithAttributes() throws Exception { provider.getUserByUsername(realmModel, FEDERATED_USER_KNOWN_USERNAME); verify(federatedUserModel, times(2)).getAttributes(); }
@Test public void testGetUserByUsernameWithoutAttributes() throws Exception { UserModel user = provider.getUserByUsername(realmModel, FEDERATED_USER_KNOWN_USERNAME); verify(federatedUserModel, times(2)).getAttributes(); verify(user, never()).setAttribute(anyString(), anyListOf(String.class)); }
@Test public void testGetUserByEmailMixedCase() throws Exception { assertNotNull(provider.getUserByUsername(realmModel, FEDERATED_USER_KNOWN_EMAIL.toUpperCase())); verify(federatedUserService).getUserDetails(eq(FEDERATED_USER_KNOWN_EMAIL)); }