Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to create AuthenticationFailedException with attributes that puts the failure in a better context #60

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.quarkus.security.identity.IdentityProvider;

import java.util.Map;

/**
* An exception that should be thrown (or otherwise returned to the client for
* async implementations) by {@link IdentityProvider} implementations if the
Expand All @@ -11,19 +13,60 @@
* due to bad credentials vs some other form of internal failure.
*/
public final class AuthenticationFailedException extends SecurityException implements AuthenticationException {

private final Map<String, Object> attributes;

public AuthenticationFailedException() {
this(null, null, null);
}

public AuthenticationFailedException(Map<String, Object> attributes) {
this(null, null, attributes);
}

public AuthenticationFailedException(String errorMessage) {
this(errorMessage, null);
this(errorMessage, null, null);
}

public AuthenticationFailedException(String errorMessage, Map<String, Object> attributes) {
this(errorMessage, null, attributes);
}

public AuthenticationFailedException(Throwable cause) {
this(null, cause);
this(null, cause, null);
}

public AuthenticationFailedException(Throwable cause, Map<String, Object> attributes) {
this(null, cause, attributes);
}

public AuthenticationFailedException(String errorMessage, Throwable cause) {
this(errorMessage, cause, null);
}

public AuthenticationFailedException(String errorMessage, Throwable cause, Map<String, Object> attributes) {
super(errorMessage, cause);
this.attributes = attributes == null || attributes.isEmpty() ? Map.of() : Map.copyOf(attributes);
}

/**
* Provides an attribute that allows to better understand the cause of the authentication failure.
*
* @param name The authentication failure attribute name
* @return The authentication attribute value or null if the attribute with this name does not exist
* @param <T> The type of the authentication failure attribute
*/
@SuppressWarnings("unchecked")
public <T> T getAttribute(String name) {
return (T) attributes.get(name);
}

/**
* Provides attributes that allow to better understand the cause of the authentication failure.
*
* @return authentication failure attributes; never null
*/
public Map<String, Object> getAttributes() {
return attributes;
}
}
Loading