Skip to content

Commit

Permalink
Allow to create AuthenticationFailedException with attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvavrik committed Jan 14, 2025
1 parent 8eb9e15 commit 2486f2b
Showing 1 changed file with 45 additions and 2 deletions.
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 ? null : 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 attributes != null ? ((T) attributes.get(name)) : null;
}

/**
* 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 == null ? Map.of() : attributes;
}
}

0 comments on commit 2486f2b

Please sign in to comment.