-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement and add
basic
authentication API in the jpro-auth-core
…
…module
- Loading branch information
Showing
9 changed files
with
282 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
jpro-auth/core/src/main/java/one/jpro/platform/auth/core/api/FluentBasicAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package one.jpro.platform.auth.core.api; | ||
|
||
import one.jpro.platform.auth.core.basic.BasicAuthenticationProvider; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Fluent Basic (username and password) Authentication interface. | ||
* | ||
* @author Besmir Beqiri | ||
*/ | ||
public interface FluentBasicAuth { | ||
|
||
/** | ||
* Set the roles. | ||
* | ||
* @param roles the roles | ||
* @return self | ||
*/ | ||
FluentBasicAuth roles(Set<String> roles); | ||
|
||
/** | ||
* Set the attributes. | ||
* | ||
* @param attributes the attributes | ||
* @return self | ||
*/ | ||
FluentBasicAuth attributes(Map<String, Object> attributes); | ||
|
||
/** | ||
* Set the authorization path. | ||
* | ||
* @param authorizationPath the authorization path | ||
* @return self | ||
*/ | ||
FluentBasicAuth authorizationPath(String authorizationPath); | ||
|
||
/** | ||
* Create a simple authentication provider. | ||
* | ||
* @return a {@link BasicAuthenticationProvider} instance. | ||
*/ | ||
BasicAuthenticationProvider create(); | ||
} |
43 changes: 43 additions & 0 deletions
43
jpro-auth/core/src/main/java/one/jpro/platform/auth/core/api/FluentBasicAuthAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package one.jpro.platform.auth.core.api; | ||
|
||
import one.jpro.platform.auth.core.basic.BasicAuthenticationProvider; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Fluent Basic (username and password) Authentication API. | ||
* | ||
* @author Besmir Beqiri | ||
*/ | ||
public class FluentBasicAuthAPI implements FluentBasicAuth { | ||
|
||
private Set<String> roles; | ||
private Map<String, Object> attributes; | ||
private String authorizationPath = BasicAuthenticationProvider.DEFAULT_AUTHORIZATION_PATH; | ||
|
||
@Override | ||
public FluentBasicAuth roles(Set<String> roles) { | ||
this.roles = roles; | ||
return this; | ||
} | ||
|
||
@Override | ||
public FluentBasicAuth attributes(Map<String, Object> attributes) { | ||
this.attributes = attributes; | ||
return this; | ||
} | ||
|
||
@Override | ||
public FluentBasicAuth authorizationPath(String authorizationPath) { | ||
this.authorizationPath = authorizationPath; | ||
return this; | ||
} | ||
|
||
@Override | ||
public BasicAuthenticationProvider create() { | ||
final var basicAuthProvider = new BasicAuthenticationProvider(roles, attributes); | ||
basicAuthProvider.setAuthorizationPath(authorizationPath); | ||
return basicAuthProvider; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...uth/core/src/main/java/one/jpro/platform/auth/core/basic/BasicAuthenticationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package one.jpro.platform.auth.core.basic; | ||
|
||
import one.jpro.platform.auth.core.authentication.*; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* The {@code BasicAuthenticationProvider} class implements the {@code AuthenticationProvider} interface | ||
* to provide basic authentication using username and password credentials. | ||
* | ||
* @author Besmir Beqiri | ||
*/ | ||
public class BasicAuthenticationProvider implements AuthenticationProvider<UsernamePasswordCredentials> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(BasicAuthenticationProvider.class); | ||
|
||
public static final String DEFAULT_AUTHORIZATION_PATH = "/auth/basic"; | ||
|
||
@NotNull | ||
private String authorizationPath = DEFAULT_AUTHORIZATION_PATH; | ||
@Nullable | ||
private Set<String> roles; | ||
@Nullable | ||
private Map<String, Object> attributes; | ||
|
||
/** | ||
* Constructs a new {@code BasicAuthenticationProvider} with specified roles and attributes. | ||
* | ||
* @param roles the set of roles to be associated with the authenticated user, may be {@code null}. | ||
* @param attributes the map of attributes to be associated with the authenticated user, may be {@code null}. | ||
*/ | ||
public BasicAuthenticationProvider(@Nullable final Set<String> roles, | ||
@Nullable final Map<String, Object> attributes) { | ||
this.roles = roles; | ||
this.attributes = attributes; | ||
} | ||
|
||
/** | ||
* Authenticates the user based on the provided {@code UsernamePasswordCredentials}. | ||
* | ||
* @param credentials the credentials containing the username and password | ||
* @return a {@code CompletableFuture} that, when completed, provides the authenticated {@code User}. | ||
* @throws CredentialValidationException if the credentials are not valid | ||
*/ | ||
@NotNull | ||
@Override | ||
public CompletableFuture<User> authenticate(@NotNull final UsernamePasswordCredentials credentials) | ||
throws CredentialValidationException { | ||
try { | ||
credentials.validate(null); | ||
} catch (CredentialValidationException ex) { | ||
logger.error("Username and password credentials not valid", ex); | ||
return CompletableFuture.failedFuture(ex); | ||
} | ||
|
||
JSONObject userJSON = new JSONObject(); | ||
userJSON.put(User.KEY_NAME, credentials.getUsername()); | ||
userJSON.put(User.KEY_ROLES, new JSONArray(roles)); | ||
|
||
JSONObject authJSON = new JSONObject(); | ||
authJSON.put("password", credentials.getPassword()); | ||
userJSON.put(User.KEY_ATTRIBUTES, new JSONObject(attributes).put("auth", authJSON)); | ||
|
||
final User user = Authentication.create(userJSON); | ||
return CompletableFuture.completedFuture(user); | ||
} | ||
|
||
/** | ||
* Gets the authorization path URI for basic authentication. | ||
* This is the URI path that the users will be redirected to if they need to be authenticated. | ||
* | ||
* @return the authorization path string | ||
*/ | ||
@NotNull | ||
public String getAuthorizationPath() { | ||
return authorizationPath; | ||
} | ||
|
||
/** | ||
* Sets the authorization path for basic authentication. | ||
* This is the URI path that the users will be redirected to if they need to be authenticated. | ||
* | ||
* @param authorizationPath the authorization path string | ||
*/ | ||
public void setAuthorizationPath(@NotNull String authorizationPath) { | ||
this.authorizationPath = authorizationPath; | ||
} | ||
|
||
/** | ||
* Gets the set of roles associated with this authentication provider. | ||
* | ||
* @return The set of roles, may be {@code null}. | ||
*/ | ||
@Nullable | ||
public Set<String> getRoles() { | ||
return roles; | ||
} | ||
|
||
/** | ||
* Sets the roles to be associated with this authentication provider. | ||
* | ||
* @param roles The set of roles, may be {@code null}. | ||
*/ | ||
public void setRoles(@Nullable Set<String> roles) { | ||
this.roles = roles; | ||
} | ||
|
||
/** | ||
* Gets the attributes associated with this authentication provider. | ||
* | ||
* @return The attributes, may be {@code null}. | ||
*/ | ||
@Nullable | ||
public Map<String, Object> getAttributes() { | ||
return attributes; | ||
} | ||
|
||
/** | ||
* Sets the attributes to be associated with this authentication provider. | ||
* | ||
* @param attributes The map of attributes, may be {@code null}. | ||
*/ | ||
public void setAttributes(@Nullable Map<String, Object> attributes) { | ||
this.attributes = attributes; | ||
} | ||
} |
Oops, something went wrong.