11package io .emeraldpay .api ;
22
3+ import io .emeraldpay .api .proto .AuthGrpc ;
4+ import io .emeraldpay .impl .AuthHolder ;
5+ import io .emeraldpay .impl .AuthInterceptor ;
6+ import io .emeraldpay .impl .TokenCredentials ;
37import io .grpc .Channel ;
8+ import io .grpc .ClientInterceptor ;
49import io .grpc .ManagedChannelBuilder ;
510import io .grpc .netty .NettyChannelBuilder ;
611
712import java .net .InetAddress ;
13+ import java .net .URI ;
814import java .util .function .Function ;
915
1016/**
1319public class EmeraldConnection {
1420
1521 private final Channel channel ;
22+ private final ClientInterceptor credentials ;
1623
1724 public EmeraldConnection (Channel channel ) {
1825 this .channel = channel ;
26+ this .credentials = null ;
27+ }
28+
29+ public EmeraldConnection (Channel channel , ClientInterceptor credentials ) {
30+ this .channel = channel ;
31+ this .credentials = credentials ;
1932 }
2033
2134 /**
@@ -38,6 +51,22 @@ public Channel getChannel() {
3851 return channel ;
3952 }
4053
54+ /**
55+ * Credentials used to authenticate on Emerald API calls
56+ * @return credentials
57+ */
58+ public ClientInterceptor getCredentials () {
59+ return credentials ;
60+ }
61+
62+ /**
63+ * Check if the connection has credentials
64+ * @return true if credentials are set
65+ */
66+ public boolean hasCredentials () {
67+ return credentials != null ;
68+ }
69+
4170 public static class Builder {
4271 private String host ;
4372 private Integer port ;
@@ -51,6 +80,8 @@ public static class Builder {
5180
5281 private Function <NettyChannelBuilder , ManagedChannelBuilder <?>> customChannel = null ;
5382
83+ private String secretToken ;
84+
5485 /**
5586 * Set target address as a host and port pair
5687 *
@@ -80,6 +111,18 @@ public Builder connectTo(String host) {
80111 return this ;
81112 }
82113
114+ public Builder connectTo (URI url ) {
115+ boolean isSecure = "https" .equals (url .getScheme ());
116+ if (isSecure ) {
117+ this .usePlaintext = false ;
118+ }
119+ int port = url .getPort ();
120+ if (port == -1 ) {
121+ port = isSecure ? 443 : 80 ;
122+ }
123+ return this .connectTo (url .getHost (), port );
124+ }
125+
83126 public Builder connectTo (InetAddress host , int port ) {
84127 this .port = port ;
85128 return this .connectTo (host );
@@ -111,6 +154,17 @@ public Builder maxMessageSize(Integer value) {
111154 return this ;
112155 }
113156
157+ /**
158+ * Authenticate on Emerald API using the provided secret token
159+ *
160+ * @param secret a token like `emrld_y40SYbbZclSZPX4r6nL9hNKUGaknAwqyv2qslI`
161+ * @return builder
162+ */
163+ public Builder withAuthToken (String secret ) {
164+ this .secretToken = secret ;
165+ return this ;
166+ }
167+
114168 /**
115169 * Customize Channel Builder by applying any custom options not covered by this Builder
116170 *
@@ -162,7 +216,18 @@ public EmeraldConnection build() {
162216 channelBuilder .defaultLoadBalancingPolicy ("round_robin" );
163217 }
164218
165- return new EmeraldConnection (channelBuilder .build ());
219+ Channel channel = channelBuilder .build ();
220+
221+ AuthInterceptor authInterceptor = null ;
222+ if (secretToken != null ) {
223+ if (usePlaintext ) {
224+ System .err .println ("WARNING: Authentication with a secret token over an unsecure plaintext connection." );
225+ }
226+ AuthHolder holder = new AuthHolder (new TokenCredentials (secretToken , AuthGrpc .newBlockingStub (channel )));
227+ authInterceptor = new AuthInterceptor (holder );
228+ }
229+
230+ return new EmeraldConnection (channel , authInterceptor );
166231 }
167232
168233
0 commit comments