-
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.
Revert "Revert "feat(multiple-auth): add support for multiple auth (#79…
- Loading branch information
1 parent
e0c0e13
commit 078c234
Showing
13 changed files
with
1,033 additions
and
85 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
139 changes: 139 additions & 0 deletions
139
src/main/java/io/apimatic/core/authentication/AuthBuilder.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,139 @@ | ||
package io.apimatic.core.authentication; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import io.apimatic.core.authentication.multiple.And; | ||
import io.apimatic.core.authentication.multiple.Or; | ||
import io.apimatic.core.authentication.multiple.Single; | ||
import io.apimatic.coreinterfaces.authentication.Authentication; | ||
|
||
/** | ||
* A builder for authentication. | ||
*/ | ||
public class AuthBuilder { | ||
|
||
/** | ||
* Constant for AND group identifier. | ||
*/ | ||
private static final String AND = "AND"; | ||
|
||
/** | ||
* Constant for OR group identifier. | ||
*/ | ||
private static final String OR = "OR"; | ||
|
||
/** | ||
* Holds nested combination of authentication. | ||
*/ | ||
private Map<String, List<AuthBuilder>> authBuilders; | ||
|
||
/** | ||
* Holds the authentication keys, must belong to the provided authentication managers. | ||
*/ | ||
private List<String> authKeys; | ||
|
||
/** | ||
* Default constructor. | ||
*/ | ||
public AuthBuilder() { | ||
authBuilders = new HashMap<String, List<AuthBuilder>>(); | ||
authBuilders.put(AND, new ArrayList<AuthBuilder>()); | ||
authBuilders.put(OR, new ArrayList<AuthBuilder>()); | ||
authKeys = new ArrayList<String>(); | ||
} | ||
|
||
/** | ||
* Registers the authentication key to the builder. | ||
* @param authKey A key pointing to some authentication in the provided auth managers. | ||
* @return {@link AuthBuilder} The instance of the current builder. | ||
*/ | ||
public AuthBuilder add(String authKey) { | ||
authKeys.add(authKey); | ||
return this; | ||
} | ||
|
||
/** | ||
* Registers the and group for authentication. | ||
* @param action A consumer for the nested builder. | ||
* @return {@link AuthBuilder} The instance of the current builder. | ||
*/ | ||
public AuthBuilder and(Consumer<AuthBuilder> action) { | ||
AuthBuilder authBuilder = new AuthBuilder(); | ||
action.accept(authBuilder); | ||
authBuilders.get(AND).add(authBuilder); | ||
return this; | ||
} | ||
|
||
/** | ||
* Registers the or group for authentication. | ||
* @param action A consumer for the nested builder. | ||
* @return {@link AuthBuilder} The instance of the current builder. | ||
*/ | ||
public AuthBuilder or(Consumer<AuthBuilder> action) { | ||
AuthBuilder authBuilder = new AuthBuilder(); | ||
action.accept(authBuilder); | ||
authBuilders.get(OR).add(authBuilder); | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds and validates the authentication using registered authentication keys. | ||
* @param authManagers The map of authentication managers. | ||
* @return {@link Authentication} The validated instance of authentication. | ||
*/ | ||
public Authentication build(Map<String, Authentication> authManagers) { | ||
if (authManagers == null || authManagers.isEmpty()) { | ||
return null; | ||
} | ||
|
||
if (authBuilders.get(AND).isEmpty() && authBuilders.get(OR).isEmpty() | ||
&& authKeys.isEmpty()) { | ||
return null; | ||
} | ||
|
||
Authentication mappedAuth = null; | ||
if (authBuilders.get(AND).isEmpty() && authBuilders.get(OR).isEmpty() | ||
&& !authKeys.isEmpty()) { | ||
mappedAuth = new Single(authManagers.get(authKeys.get(0))); | ||
return mappedAuth; | ||
} | ||
|
||
for (AuthBuilder authBuilder : authBuilders.get(AND)) { | ||
mappedAuth = new And(authBuilder.buildAuthGroup(authManagers)); | ||
} | ||
|
||
for (AuthBuilder authBuilder : authBuilders.get(OR)) { | ||
mappedAuth = new Or(authBuilder.buildAuthGroup(authManagers)); | ||
} | ||
|
||
return mappedAuth; | ||
} | ||
|
||
/** | ||
* Builds the nested authentication groups. | ||
* @param authManagers The map of authentication managers. | ||
* @return List<{@link Authentication}> The converted instance of nested authentications. | ||
*/ | ||
private List<Authentication> buildAuthGroup(Map<String, Authentication> authManagers) { | ||
List<Authentication> auths = new ArrayList<Authentication>(); | ||
|
||
authKeys.forEach(authKey -> { | ||
if (authManagers.containsKey(authKey)) { | ||
auths.add(new Single(authManagers.get(authKey))); | ||
} | ||
}); | ||
|
||
authBuilders.get(AND).forEach(authBuilder -> { | ||
auths.add(new And(authBuilder.buildAuthGroup(authManagers))); | ||
}); | ||
|
||
authBuilders.get(OR).forEach(authBuilder -> { | ||
auths.add(new Or(authBuilder.buildAuthGroup(authManagers))); | ||
}); | ||
|
||
return auths; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/io/apimatic/core/authentication/AuthCredential.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,52 @@ | ||
package io.apimatic.core.authentication; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import io.apimatic.coreinterfaces.authentication.Authentication; | ||
|
||
/** | ||
* Handles and validates the Authentication parameters. | ||
*/ | ||
public abstract class AuthCredential extends Authentication { | ||
|
||
/** | ||
* A map of authentication parameters. | ||
*/ | ||
private Map<String, String> authParams = new HashMap<>(); | ||
|
||
/** | ||
* @param authParams Map of authentication parameters. | ||
*/ | ||
public AuthCredential(final Map<String, String> authParams) { | ||
this.authParams = authParams; | ||
} | ||
|
||
/** | ||
* Getter for the map of authentication parameters. | ||
* @return Map<String, String> The map of authentication parameters. | ||
*/ | ||
public Map<String, String> getAuthParams() { | ||
return authParams; | ||
} | ||
|
||
/** | ||
* Validates the credentials for authentication. | ||
*/ | ||
public void validate() { | ||
// Check for null keys or values | ||
boolean hasNullKeyOrValue = authParams.entrySet().stream() | ||
.anyMatch(entry -> entry.getKey() == null | ||
|| entry.getKey() == "" | ||
|| entry.getValue() == null | ||
|| entry.getValue() == ""); | ||
|
||
if (hasNullKeyOrValue) { | ||
setErrorMessage("[Auth key and value cannot be null]"); | ||
setValidity(false); | ||
return; | ||
} | ||
|
||
setValidity(true); | ||
} | ||
|
||
} |
Oops, something went wrong.