Java Code Examples for org.keycloak.representations.idm.UserRepresentation#getUsername()

The following examples show how to use org.keycloak.representations.idm.UserRepresentation#getUsername() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeyCloakServiceImpl.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
@Override
public String getUsernameById(String userId) {
  String fedUserId = getFederatedUserId(userId);
  try {
    UserResource resource =
        keycloak.realm(KeyCloakConnectionProvider.SSO_REALM).users().get(fedUserId);
    UserRepresentation ur = resource.toRepresentation();
    return ur.getUsername();
  } catch (Exception e) {
    ProjectLogger.log(
        "KeyCloakServiceImpl:getUsernameById: User not found for userId = "
            + userId
            + " error message = "
            + e.getMessage(),
        e);
  }
  ProjectLogger.log(
      "KeyCloakServiceImpl:getUsernameById: User not found for userId = " + userId,
      LoggerEnum.INFO.name());
  return "";
}
 
Example 2
Source File: ServiceAccountTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void refreshTokenRefreshForDisabledServiceAccount() throws Exception {
    try {
        oauth.clientId("service-account-cl");
        OAuthClient.AccessTokenResponse response = oauth.doClientCredentialsGrantAccessTokenRequest("secret1");
        assertEquals(200, response.getStatusCode());

        ClientManager.realm(adminClient.realm("test")).clientId("service-account-cl").setServiceAccountsEnabled(false);

        response = oauth.doRefreshTokenRequest(response.getRefreshToken(), "secret1");
        assertEquals(400, response.getStatusCode());
    }
    finally {
        ClientManager.realm(adminClient.realm("test")).clientId("service-account-cl").setServiceAccountsEnabled(true);
        UserRepresentation user = ClientManager.realm(adminClient.realm("test")).clientId("service-account-cl").getServiceAccountUser();
        userId = user.getId();
        userName = user.getUsername();
    }
}
 
Example 3
Source File: UserAttributesForm.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public void setValues(UserRepresentation user) {
    waitUntilElement(usernameInput).is().present();
    if (user.getUsername() != null) {
        setUsername(user.getUsername());
    }
    setEmail(user.getEmail());
    setFirstName(user.getFirstName());
    setLastName(user.getLastName());
    if (user.isEnabled() != null) setEnabled(user.isEnabled());
    if (user.isEmailVerified() != null) setEmailVerified(user.isEmailVerified());
    if (user.getRequiredActions() != null) setRequiredActions(new HashSet<>(user.getRequiredActions()));
}
 
Example 4
Source File: UsersPartialImport.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public String getModelId(RealmModel realm, KeycloakSession session, UserRepresentation user) {
    if (createdIds.containsKey(getName(user))) return createdIds.get(getName(user));

    String userName = user.getUsername();
    if (userName != null) {
        return session.users().getUserByUsername(userName, realm).getId();
    } else if (!realm.isDuplicateEmailsAllowed()) {
        String email = user.getEmail();
        return session.users().getUserByEmail(email, realm).getId();
    }
    
    return null;
}
 
Example 5
Source File: UserImportService.java    From keycloak-config-cli with Apache License 2.0 4 votes vote down vote up
private UserImport(String realm, UserRepresentation userToImport) {
    this.realm = realm;
    this.userToImport = userToImport;
    this.username = userToImport.getUsername();
}
 
Example 6
Source File: UsersPartialImport.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public String getName(UserRepresentation user) {
    if (user.getUsername() != null) return user.getUsername();

    return user.getEmail();
}
 
Example 7
Source File: UsernameTemplateMapperTest.java    From keycloak with Apache License 2.0 3 votes vote down vote up
/**
 * See: KEYCLOAK-8100
 */
@Test
public void usernameShouldBeDerivedFromAliasAndIdpSubClaim() {

    logInAsUserInIDP();
    logoutFromRealm(getConsumerRoot(), bc.consumerRealmName());

    UserRepresentation user = adminClient.realm(bc.consumerRealmName()).users().search(bc.getUserEmail(), 0, 1).get(0);

    String username = user.getUsername();

    assertEquals("Should render alias:sub as Username", bc.getIDPAlias() + ":" + idpUserId, username);
}