Polishing.
Reformat code. Simplify flow. See gh-689 Original pull request: gh-802
This commit is contained in:
@@ -17,9 +17,11 @@ package org.springframework.vault.authentication;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.vault.VaultException;
|
||||
@@ -34,7 +36,7 @@ import org.springframework.web.client.RestOperations;
|
||||
* to obtain a {@link VaultToken}.
|
||||
*
|
||||
* @author Nanne Baars
|
||||
* @since 3.0.4
|
||||
* @since 3.1
|
||||
* @see JwtAuthenticationOptions
|
||||
* @see RestOperations
|
||||
* @see <a href="https://www.vaultproject.io/api-docs/auth/jwt">Vault Auth Backend:
|
||||
@@ -57,6 +59,7 @@ public class JwtAuthentication implements ClientAuthentication, AuthenticationSt
|
||||
* @param restOperations must not be {@literal null}.
|
||||
*/
|
||||
public JwtAuthentication(JwtAuthenticationOptions options, RestOperations restOperations) {
|
||||
|
||||
Assert.notNull(options, "JwtAuthenticationOptions must not be null");
|
||||
Assert.notNull(restOperations, "RestOperations must not be null");
|
||||
|
||||
@@ -64,31 +67,22 @@ public class JwtAuthentication implements ClientAuthentication, AuthenticationSt
|
||||
this.restOperations = restOperations;
|
||||
}
|
||||
|
||||
private static Map<String, String> getJwtLogin(String role, String jwt) {
|
||||
Map<String, String> login = new HashMap<>();
|
||||
|
||||
login.put("jwt", jwt);
|
||||
if (StringUtils.hasText(role)) {
|
||||
login.put("role", role);
|
||||
}
|
||||
|
||||
return login;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthenticationSteps getAuthenticationSteps() {
|
||||
return AuthenticationSteps.fromSupplier(options.getJwtSupplier())
|
||||
.map(token -> getJwtLogin(options.getRole(), token))
|
||||
.login(getLoginPath());
|
||||
.login(AuthenticationUtil.getLoginPath(this.options.getPath()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public VaultToken login() throws VaultException {
|
||||
|
||||
Map<String, String> login = getJwtLogin(this.options.getRole(), this.options.getJwtSupplier().get());
|
||||
|
||||
try {
|
||||
VaultResponse response = this.restOperations.postForObject(getLoginPath(), login, VaultResponse.class);
|
||||
|
||||
VaultResponse response = this.restOperations
|
||||
.postForObject(AuthenticationUtil.getLoginPath(this.options.getPath()), login, VaultResponse.class);
|
||||
Assert.state(response != null && response.getAuth() != null, "Auth field must not be null");
|
||||
|
||||
logger.debug("Login successful using JWT authentication");
|
||||
@@ -100,9 +94,17 @@ public class JwtAuthentication implements ClientAuthentication, AuthenticationSt
|
||||
}
|
||||
}
|
||||
|
||||
private String getLoginPath() {
|
||||
return AuthenticationUtil
|
||||
.getLoginPath(Optional.ofNullable(options.getPath()).orElse(DEFAULT_JWT_AUTHENTICATION_PATH));
|
||||
private static Map<String, String> getJwtLogin(@Nullable String role, String jwt) {
|
||||
|
||||
Map<String, String> login = new HashMap<>(2);
|
||||
|
||||
login.put("jwt", jwt);
|
||||
|
||||
if (StringUtils.hasText(role)) {
|
||||
login.put("role", role);
|
||||
}
|
||||
|
||||
return login;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.vault.authentication;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -25,20 +26,21 @@ import org.springframework.util.Assert;
|
||||
* Authentication options provide the role and the JWT. {@link JwtAuthenticationOptions}
|
||||
* can be constructed using {@link #builder()}. Instances of this class are immutable once
|
||||
* constructed.
|
||||
* <p>
|
||||
*
|
||||
* @author Nanne Baars
|
||||
* @since 3.0.4
|
||||
* @author Mark Paluch
|
||||
* @since 3.1
|
||||
* @see JwtAuthentication
|
||||
* @see #builder()
|
||||
*/
|
||||
public class JwtAuthenticationOptions {
|
||||
|
||||
public static final String DEFAULT_JWT_AUTHENTICATION_PATH = "jwt";
|
||||
|
||||
/**
|
||||
* Path of the JWT authentication backend mount. Optional and defaults to
|
||||
* {@literal jwt}.
|
||||
*/
|
||||
@Nullable
|
||||
private final String path;
|
||||
|
||||
/**
|
||||
@@ -55,7 +57,7 @@ public class JwtAuthenticationOptions {
|
||||
*/
|
||||
private final Supplier<String> jwtSupplier;
|
||||
|
||||
private JwtAuthenticationOptions(String role, Supplier<String> jwtSupplier, String path) {
|
||||
private JwtAuthenticationOptions(@Nullable String role, Supplier<String> jwtSupplier, String path) {
|
||||
|
||||
this.role = role;
|
||||
this.jwtSupplier = jwtSupplier;
|
||||
@@ -70,8 +72,10 @@ public class JwtAuthenticationOptions {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return name of the role against which the login is being attempted.
|
||||
* @return name of the role against which the login is being attempted. Can be
|
||||
* {@literal null} if not configured.
|
||||
*/
|
||||
@Nullable
|
||||
public String getRole() {
|
||||
return this.role;
|
||||
}
|
||||
@@ -84,7 +88,7 @@ public class JwtAuthenticationOptions {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the path of the kubernetes authentication backend mount.
|
||||
* @return the path of the JWT authentication backend mount.
|
||||
*/
|
||||
public String getPath() {
|
||||
return this.path;
|
||||
@@ -95,11 +99,26 @@ public class JwtAuthenticationOptions {
|
||||
*/
|
||||
public static class JwtAuthenticationOptionsBuilder {
|
||||
|
||||
private String path = DEFAULT_JWT_AUTHENTICATION_PATH;
|
||||
|
||||
@Nullable
|
||||
private String role;
|
||||
|
||||
@Nullable
|
||||
private Supplier<String> jwtSupplier;
|
||||
|
||||
private String path;
|
||||
/**
|
||||
* Configure the mount path.
|
||||
* @param path must not be {@literal null} or empty.
|
||||
* @return {@code this} {@link JwtAuthenticationOptionsBuilder}.
|
||||
*/
|
||||
public JwtAuthenticationOptionsBuilder path(String path) {
|
||||
|
||||
Assert.hasText(path, "Path must not be empty");
|
||||
|
||||
this.path = path;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the role.
|
||||
@@ -116,16 +135,17 @@ public class JwtAuthenticationOptions {
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the mount path.
|
||||
* @param path must not be {@literal null} or empty.
|
||||
* Configure the JWT authentication token. Vault authentication will use this
|
||||
* token as singleton. If you want to provide a dynamic token that can change over
|
||||
* time, see {@link #jwtSupplier(Supplier)}.
|
||||
* @param jwt must not be {@literal null}.
|
||||
* @return {@code this} {@link JwtAuthenticationOptionsBuilder}.
|
||||
*/
|
||||
public JwtAuthenticationOptionsBuilder path(String path) {
|
||||
public JwtAuthenticationOptionsBuilder jwt(String jwt) {
|
||||
|
||||
Assert.hasText(path, "Path must not be empty");
|
||||
Assert.hasText(jwt, "JWT must not be empty");
|
||||
|
||||
this.path = path;
|
||||
return this;
|
||||
return jwtSupplier(() -> jwt);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,9 +153,9 @@ public class JwtAuthenticationOptions {
|
||||
* @param jwtSupplier must not be {@literal null}.
|
||||
* @return {@code this} {@link JwtAuthenticationOptionsBuilder}.
|
||||
*/
|
||||
public JwtAuthenticationOptionsBuilder jwt(Supplier<String> jwtSupplier) {
|
||||
public JwtAuthenticationOptionsBuilder jwtSupplier(Supplier<String> jwtSupplier) {
|
||||
|
||||
Assert.notNull(jwtSupplier, "Jwt supplier must not be null");
|
||||
Assert.notNull(jwtSupplier, "JWT supplier must not be null");
|
||||
|
||||
this.jwtSupplier = jwtSupplier;
|
||||
return this;
|
||||
|
||||
Reference in New Issue
Block a user