Skip to content

Commit 070037f

Browse files
authored
Merge pull request #1349 from nats-io/token-supplier-from-property
Options allow token supplier from property, not just api method
2 parents a2f5304 + 43a0dde commit 070037f

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/main/java/io/nats/client/Options.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ public class Options {
379379
* Property used to configure a builder from a Properties object. {@value}, see {@link Builder#token(String) token}.
380380
*/
381381
public static final String PROP_TOKEN = PFX + "token";
382+
/**
383+
* Property used to configure the token supplier from a Properties object. {@value}, see {@link Builder#tokenSupplier(Supplier) tokenSupplier}.
384+
*/
385+
public static final String PROP_TOKEN_SUPPLIER = PFX + "token.supplier";
382386
/**
383387
* Property used to configure a builder from a Properties object. {@value}, see {@link Builder#server(String) server}.
384388
*/
@@ -882,6 +886,8 @@ public Builder properties(Properties props) {
882886
charArrayProperty(props, PROP_USERNAME, ca -> this.username = ca);
883887
charArrayProperty(props, PROP_PASSWORD, ca -> this.password = ca);
884888
charArrayProperty(props, PROP_TOKEN, ca -> this.tokenSupplier = new DefaultTokenSupplier(ca));
889+
//noinspection unchecked
890+
classnameProperty(props, PROP_TOKEN_SUPPLIER, o -> this.tokenSupplier = (Supplier<char[]>) o);
885891

886892
booleanProperty(props, PROP_SECURE, b -> this.useDefaultTls = b);
887893
booleanProperty(props, PROP_OPENTLS, b -> this.useTrustAllTls = b);

src/test/java/io/nats/client/OptionsTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,16 @@ public void testTokenSupplier() {
892892

893893
connectString = o.buildProtocolConnectOptionsString(serverURI, true, null).toString();
894894
assertTrue(connectString.contains("\"auth_token\":\"short-lived-token-2\""));
895+
896+
Properties properties = new Properties();
897+
properties.setProperty(PROP_TOKEN_SUPPLIER, TestingDynamicTokenSupplier.class.getCanonicalName());
898+
o = new Options.Builder().properties(properties).build();
899+
900+
connectString = o.buildProtocolConnectOptionsString(serverURI, true, null).toString();
901+
assertTrue(connectString.contains("\"auth_token\":\"dynamic-token-1\""));
902+
903+
connectString = o.buildProtocolConnectOptionsString(serverURI, true, null).toString();
904+
assertTrue(connectString.contains("\"auth_token\":\"dynamic-token-2\""));
895905
}
896906

897907
@Test
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2025 The NATS Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at:
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package io.nats.client;
15+
16+
import java.util.concurrent.atomic.AtomicInteger;
17+
import java.util.function.Supplier;
18+
19+
public class TestingDynamicTokenSupplier implements Supplier<char[]> {
20+
AtomicInteger counter = new AtomicInteger(0);
21+
22+
@Override
23+
public char[] get() {
24+
return ("dynamic-token-" + counter.incrementAndGet()).toCharArray();
25+
}
26+
}

0 commit comments

Comments
 (0)