|
| 1 | +/* |
| 2 | + * Copyright 2017-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.vault.authentication; |
| 17 | + |
| 18 | +import java.util.function.Supplier; |
| 19 | +import org.springframework.lang.Nullable; |
| 20 | +import org.springframework.util.Assert; |
| 21 | + |
| 22 | +/** |
| 23 | + * Authentication options for {@link JwtAuthentication}. |
| 24 | + * <p> |
| 25 | + * Authentication options provide the role and the JWT. {@link JwtAuthenticationOptions} |
| 26 | + * can be constructed using {@link #builder()}. Instances of this class are immutable once |
| 27 | + * constructed. |
| 28 | + * <p> |
| 29 | + * |
| 30 | + * @author Nanne Baars |
| 31 | + * @since 3.0.4 |
| 32 | + * @see JwtAuthentication |
| 33 | + * @see #builder() |
| 34 | + */ |
| 35 | +public class JwtAuthenticationOptions { |
| 36 | + |
| 37 | + /** |
| 38 | + * Path of the JWT authentication backend mount. Optional and defaults to |
| 39 | + * {@literal jwt}. |
| 40 | + */ |
| 41 | + @Nullable |
| 42 | + private final String path; |
| 43 | + |
| 44 | + /** |
| 45 | + * Name of the role against which the login is being attempted. Defaults to configured |
| 46 | + * default_role if not provided. See |
| 47 | + * <a href="https://developer.hashicorp.com/vault/api-docs/auth/jwt#configure">Vault |
| 48 | + * JWT configuration</a> |
| 49 | + */ |
| 50 | + @Nullable |
| 51 | + private final String role; |
| 52 | + |
| 53 | + /** |
| 54 | + * Supplier instance to obtain a service account JSON Web Tokens. |
| 55 | + */ |
| 56 | + private final Supplier<String> jwtSupplier; |
| 57 | + |
| 58 | + private JwtAuthenticationOptions(String role, Supplier<String> jwtSupplier, String path) { |
| 59 | + |
| 60 | + this.role = role; |
| 61 | + this.jwtSupplier = jwtSupplier; |
| 62 | + this.path = path; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return a new {@link JwtAuthenticationOptionsBuilder}. |
| 67 | + */ |
| 68 | + public static JwtAuthenticationOptionsBuilder builder() { |
| 69 | + return new JwtAuthenticationOptionsBuilder(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return name of the role against which the login is being attempted. |
| 74 | + */ |
| 75 | + public String getRole() { |
| 76 | + return this.role; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @return JSON Web Token. |
| 81 | + */ |
| 82 | + public Supplier<String> getJwtSupplier() { |
| 83 | + return this.jwtSupplier; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @return the path of the kubernetes authentication backend mount. |
| 88 | + */ |
| 89 | + public String getPath() { |
| 90 | + return this.path; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Builder for {@link JwtAuthenticationOptions}. |
| 95 | + */ |
| 96 | + public static class JwtAuthenticationOptionsBuilder { |
| 97 | + |
| 98 | + private String role; |
| 99 | + |
| 100 | + private Supplier<String> jwtSupplier; |
| 101 | + |
| 102 | + private String path; |
| 103 | + |
| 104 | + /** |
| 105 | + * Configure the role. |
| 106 | + * @param role name of the role against which the login is being attempted, must |
| 107 | + * not be {@literal null} or empty. |
| 108 | + * @return {@code this} {@link JwtAuthenticationOptionsBuilder}. |
| 109 | + */ |
| 110 | + public JwtAuthenticationOptionsBuilder role(String role) { |
| 111 | + |
| 112 | + Assert.hasText(role, "Role must not be empty"); |
| 113 | + |
| 114 | + this.role = role; |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Configure the mount path. |
| 120 | + * @param path must not be {@literal null} or empty. |
| 121 | + * @return {@code this} {@link JwtAuthenticationOptionsBuilder}. |
| 122 | + */ |
| 123 | + public JwtAuthenticationOptionsBuilder path(String path) { |
| 124 | + |
| 125 | + Assert.hasText(path, "Path must not be empty"); |
| 126 | + |
| 127 | + this.path = path; |
| 128 | + return this; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Configure the {@link Supplier} to obtain a JWT authentication token. |
| 133 | + * @param jwtSupplier must not be {@literal null}. |
| 134 | + * @return {@code this} {@link JwtAuthenticationOptionsBuilder}. |
| 135 | + */ |
| 136 | + public JwtAuthenticationOptionsBuilder jwt(Supplier<String> jwtSupplier) { |
| 137 | + |
| 138 | + Assert.notNull(jwtSupplier, "Jwt supplier must not be null"); |
| 139 | + |
| 140 | + this.jwtSupplier = jwtSupplier; |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Build a new {@link JwtAuthenticationOptions} instance. |
| 146 | + * @return a new {@link JwtAuthenticationOptions}. |
| 147 | + */ |
| 148 | + public JwtAuthenticationOptions build() { |
| 149 | + |
| 150 | + Assert.notNull(this.jwtSupplier, "JWT must not be null"); |
| 151 | + |
| 152 | + return new JwtAuthenticationOptions(this.role, this.jwtSupplier, this.path); |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments