Skip to content

Commit

Permalink
no result to parse on success (#27336) (#27985)
Browse files Browse the repository at this point in the history
* no result to parse on success

fixes: #27245
Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>

* translate error message

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>

---------

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
(cherry picked from commit 7d104db)

Co-authored-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
  • Loading branch information
jonkoops and edewit committed Mar 18, 2024
1 parent bf4a320 commit f3165fc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
7 changes: 6 additions & 1 deletion js/apps/account-ui/src/account-security/AccountRow.tsx
Expand Up @@ -22,7 +22,11 @@ type AccountRowProps = {
refresh: () => void;
};

export const AccountRow = ({ account, isLinked = false }: AccountRowProps) => {
export const AccountRow = ({
account,
isLinked = false,
refresh,
}: AccountRowProps) => {
const { t } = useTranslation();
const context = useEnvironment();
const { addAlert, addError } = useAlerts();
Expand All @@ -31,6 +35,7 @@ export const AccountRow = ({ account, isLinked = false }: AccountRowProps) => {
try {
await unLinkAccount(context, account);
addAlert(t("unLinkSuccess"));
refresh();
} catch (error) {
addError(t("unLinkError", { error }).toString());
}
Expand Down
1 change: 1 addition & 0 deletions js/apps/account-ui/src/api/methods.ts
Expand Up @@ -124,6 +124,7 @@ export async function unLinkAccount(
method: "DELETE",
},
);
if (response.ok) return;
return parseResponse(response);
}

Expand Down
Expand Up @@ -16,6 +16,7 @@
*/
package org.keycloak.services.resources.account;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
Expand Down Expand Up @@ -60,6 +61,7 @@
import org.keycloak.services.managers.Auth;
import org.keycloak.services.messages.Messages;
import org.keycloak.services.validation.Validation;
import org.keycloak.theme.Theme;

import static org.keycloak.models.Constants.ACCOUNT_CONSOLE_CLIENT_ID;

Expand Down Expand Up @@ -201,12 +203,12 @@ public Response removeLinkedAccount(@PathParam("providerAlias") String providerA

FederatedIdentityModel link = session.users().getFederatedIdentity(realm, user, providerAlias);
if (link == null) {
throw ErrorResponse.error(Messages.FEDERATED_IDENTITY_NOT_ACTIVE, Response.Status.BAD_REQUEST);
throw ErrorResponse.error(translateErrorMessage(Messages.FEDERATED_IDENTITY_NOT_ACTIVE), Response.Status.BAD_REQUEST);
}

// Removing last social provider is not possible if you don't have other possibility to authenticate
if (!(session.users().getFederatedIdentitiesStream(realm, user).count() > 1 || user.getFederationLink() != null || isPasswordSet())) {
throw ErrorResponse.error(Messages.FEDERATED_IDENTITY_REMOVING_LAST_PROVIDER, Response.Status.BAD_REQUEST);
throw ErrorResponse.error(translateErrorMessage(Messages.FEDERATED_IDENTITY_REMOVING_LAST_PROVIDER), Response.Status.BAD_REQUEST);
}

session.users().removeFederatedIdentity(realm, user, providerAlias);
Expand Down Expand Up @@ -239,6 +241,14 @@ private String checkCommonPreconditions(String providerAlias) {

return null;
}

private String translateErrorMessage(String errorCode) {
try {
return session.theme().getTheme(Theme.Type.ACCOUNT).getMessages(session.getContext().resolveLocale(user)).getProperty(errorCode);
} catch (IOException e) {
return errorCode;
}
}

private boolean isPasswordSet() {
return user.credentialManager().isConfiguredFor(PasswordCredentialModel.TYPE);
Expand Down

0 comments on commit f3165fc

Please sign in to comment.