2222import java .util .List ;
2323import java .util .Map ;
2424import java .util .concurrent .TimeUnit ;
25+ import java .util .stream .StreamSupport ;
2526
2627import com .fasterxml .jackson .annotation .JsonProperty ;
2728
28- import java .util .stream .StreamSupport ;
2929import org .springframework .lang .Nullable ;
3030import org .springframework .util .Assert ;
3131
3232/**
3333 * Value object to bind Vault HTTP Token API requests.
3434 *
3535 * @author Mark Paluch
36+ * @author Nanne Baars
3637 */
3738public class VaultTokenRequest {
3839
40+ private static final VaultTokenRequest EMPTY = VaultTokenRequest .builder ().build ();
41+
3942 @ Nullable
4043 private final String id ;
4144
@@ -61,15 +64,15 @@ public class VaultTokenRequest {
6164 @ JsonProperty ("display_name" )
6265 private final String displayName ;
6366
64- @ JsonProperty ("num_uses" )
65- private final int numUses ;
66-
6767 @ JsonProperty ("entity_alias" )
6868 private final String entityAlias ;
6969
70+ @ JsonProperty ("num_uses" )
71+ private final int numUses ;
72+
7073 VaultTokenRequest (@ Nullable String id , List <String > policies , Map <String , String > meta , boolean noParent ,
7174 boolean noDefaultPolicy , boolean renewable , @ Nullable String ttl , @ Nullable String explicitMaxTtl ,
72- String displayName , int numUses , String entityAlias ) {
75+ String displayName , String entityAlias , int numUses ) {
7376
7477 this .id = id ;
7578 this .policies = policies ;
@@ -91,6 +94,14 @@ public static VaultTokenRequestBuilder builder() {
9194 return new VaultTokenRequestBuilder ();
9295 }
9396
97+ /**
98+ * @return an empty token request.
99+ * @since 3.1
100+ */
101+ public static VaultTokenRequest empty () {
102+ return EMPTY ;
103+ }
104+
94105 /**
95106 * @return Id of the client token.
96107 */
@@ -159,19 +170,19 @@ public String getDisplayName() {
159170 }
160171
161172 /**
162- * @return the number of allowed token uses.
173+ * @return then name of the entity alias to associate with during token creation. Only
174+ * works in combination with role name.
175+ * @since 3.1
163176 */
164- public int getNumUses () {
165- return this .numUses ;
177+ public String getEntityAlias () {
178+ return this .entityAlias ;
166179 }
167180
168181 /**
169- * @return then name of the entity alias to associate with during token creation. Only
170- * works in combination with role_name argument and used entity alias must be listed
171- * in allowed_entity_aliases
182+ * @return the number of allowed token uses.
172183 */
173- public String getEntityAlias () {
174- return this .entityAlias ;
184+ public int getNumUses () {
185+ return this .numUses ;
175186 }
176187
177188 /**
@@ -200,17 +211,18 @@ public static class VaultTokenRequestBuilder {
200211
201212 private String displayName = "" ;
202213
203- private int numUses ;
204-
214+ @ Nullable
205215 private String entityAlias ;
206216
217+ private int numUses ;
218+
207219 VaultTokenRequestBuilder () {
208220 }
209221
210222 /**
211- * Configure a the Id of the client token. Can only be specified by a root token.
212- * Otherwise, the token Id is a randomly generated UUID.
213- * @param id the token Id .
223+ * Configure the token identifier . Can only be specified by a root token.
224+ * Otherwise, the token identifier is a randomly generated UUID.
225+ * @param id the token identifier .
214226 * @return {@code this} {@link VaultTokenRequestBuilder}.
215227 */
216228 public VaultTokenRequestBuilder id (String id ) {
@@ -386,21 +398,6 @@ public VaultTokenRequestBuilder explicitMaxTtl(Duration explicitMaxTtl) {
386398 return this ;
387399 }
388400
389- /**
390- * Configure the maximum uses for the token. This can be used to create a
391- * one-time-token or limited use token. Defaults to {@literal 0}, which has no
392- * limit to the number of uses.
393- * @param numUses number of uses, must not be negative.
394- * @return {@code this} {@link VaultTokenRequestBuilder}.
395- */
396- public VaultTokenRequestBuilder numUses (int numUses ) {
397-
398- Assert .isTrue (numUses >= 0 , "Number of uses must not be negative" );
399-
400- this .numUses = numUses ;
401- return this ;
402- }
403-
404401 /**
405402 * Configure a display name for the token, defaults to "token".
406403 * @param displayName must not be empty or {@literal null}.
@@ -418,6 +415,7 @@ public VaultTokenRequestBuilder displayName(String displayName) {
418415 * Configure the entity alias for the token.
419416 * @param entityAlias must not be empty or {@literal null}.
420417 * @return {@code this} {@link VaultTokenRequestBuilder}.
418+ * @since 3.1
421419 */
422420 public VaultTokenRequestBuilder entityAlias (String entityAlias ) {
423421
@@ -428,9 +426,20 @@ public VaultTokenRequestBuilder entityAlias(String entityAlias) {
428426 }
429427
430428 /**
431- * Build a new {@link VaultTokenRequest} instance.
432- * @return a new {@link VaultCertificateRequest}.
429+ * Configure the maximum uses for the token. This can be used to create a
430+ * one-time-token or limited use token. Defaults to {@literal 0}, which has no
431+ * limit to the number of uses.
432+ * @param numUses number of uses, must not be negative.
433+ * @return {@code this} {@link VaultTokenRequestBuilder}.
433434 */
435+ public VaultTokenRequestBuilder numUses (int numUses ) {
436+
437+ Assert .isTrue (numUses >= 0 , "Number of uses must not be negative" );
438+
439+ this .numUses = numUses ;
440+ return this ;
441+ }
442+
434443 /**
435444 * Build a new {@link VaultTokenRequest} instance.
436445 * @return a new {@link VaultCertificateRequest}.
@@ -442,13 +451,12 @@ public VaultTokenRequest build() {
442451 case 1 -> List .of (this .policies .get (0 ));
443452 default -> List .copyOf (this .policies );
444453 };
445- Map <String , String > meta = switch (this .meta .size ()) {
446- case 0 -> Map .of ();
447- default -> Collections .unmodifiableMap (new LinkedHashMap <>(this .meta ));
448- };
454+
455+ Map <String , String > meta = this .meta .isEmpty () ? Map .of ()
456+ : Collections .unmodifiableMap (new LinkedHashMap <>(this .meta ));
449457
450458 return new VaultTokenRequest (this .id , policies , meta , this .noParent , this .noDefaultPolicy , this .renewable ,
451- this .ttl , this .explicitMaxTtl , this .displayName , this .numUses , this .entityAlias );
459+ this .ttl , this .explicitMaxTtl , this .displayName , this .entityAlias , this .numUses );
452460 }
453461
454462 private static <E > List <E > toList (Iterable <E > iter ) {
0 commit comments