1
1
package com .twilio ;
2
2
3
+ import com .twilio .annotations .Beta ;
4
+ import com .twilio .auth_strategy .AuthStrategy ;
3
5
import com .twilio .exception .ApiException ;
4
6
import com .twilio .exception .AuthenticationException ;
5
7
import com .twilio .exception .CertificateValidationException ;
8
+ import com .twilio .credential .CredentialProvider ;
6
9
import com .twilio .http .HttpMethod ;
7
10
import com .twilio .http .NetworkHttpClient ;
8
11
import com .twilio .http .Request ;
@@ -34,6 +37,8 @@ public class Twilio {
34
37
private static String edge = System .getenv ("TWILIO_EDGE" );
35
38
private static volatile TwilioRestClient restClient ;
36
39
private static volatile ExecutorService executorService ;
40
+
41
+ private static CredentialProvider credentialProvider ;
37
42
38
43
private Twilio () {
39
44
}
@@ -64,6 +69,31 @@ public static synchronized void init(final String username, final String passwor
64
69
Twilio .setAccountSid (null );
65
70
}
66
71
72
+ @ Beta
73
+ public static synchronized void init (final CredentialProvider credentialProvider ) {
74
+ Twilio .setCredentialProvider (credentialProvider );
75
+ Twilio .setAccountSid (null );
76
+ }
77
+
78
+ @ Beta
79
+ public static synchronized void init (final CredentialProvider credentialProvider , String accountSid ) {
80
+ Twilio .setCredentialProvider (credentialProvider );
81
+ Twilio .setAccountSid (accountSid );
82
+ }
83
+
84
+ private static void setCredentialProvider (final CredentialProvider credentialProvider ) {
85
+ if (credentialProvider == null ) {
86
+ throw new AuthenticationException ("Credential Provider can not be null" );
87
+ }
88
+
89
+ if (!credentialProvider .equals (Twilio .credentialProvider )) {
90
+ Twilio .invalidate ();
91
+ }
92
+ // Invalidate Basic Creds as they might be initialized via environment variables.
93
+ invalidateBasicCreds ();
94
+ Twilio .credentialProvider = credentialProvider ;
95
+ }
96
+
67
97
/**
68
98
* Initialize the Twilio environment.
69
99
*
@@ -91,6 +121,7 @@ public static synchronized void setUsername(final String username) {
91
121
if (!username .equals (Twilio .username )) {
92
122
Twilio .invalidate ();
93
123
}
124
+ Twilio .invalidateOAuthCreds ();
94
125
95
126
Twilio .username = username ;
96
127
}
@@ -109,6 +140,7 @@ public static synchronized void setPassword(final String password) {
109
140
if (!password .equals (Twilio .password )) {
110
141
Twilio .invalidate ();
111
142
}
143
+ Twilio .invalidateOAuthCreds ();
112
144
113
145
Twilio .password = password ;
114
146
}
@@ -181,12 +213,19 @@ public static TwilioRestClient getRestClient() {
181
213
182
214
private static TwilioRestClient buildRestClient () {
183
215
if (Twilio .username == null || Twilio .password == null ) {
184
- throw new AuthenticationException (
185
- "TwilioRestClient was used before AccountSid and AuthToken were set, please call Twilio.init()"
186
- );
216
+ if (credentialProvider == null ) {
217
+ throw new AuthenticationException (
218
+ "Credentials have not been initialized or changed, please call Twilio.init()"
219
+ );
220
+ }
221
+ }
222
+ TwilioRestClient .Builder builder ;
223
+ if (credentialProvider != null ) {
224
+ AuthStrategy authStrategy = credentialProvider .toAuthStrategy ();
225
+ builder = new TwilioRestClient .Builder (authStrategy );
226
+ } else {
227
+ builder = new TwilioRestClient .Builder (Twilio .username , Twilio .password );
187
228
}
188
-
189
- TwilioRestClient .Builder builder = new TwilioRestClient .Builder (Twilio .username , Twilio .password );
190
229
191
230
if (Twilio .accountSid != null ) {
192
231
builder .accountSid (Twilio .accountSid );
@@ -273,6 +312,15 @@ private static void invalidate() {
273
312
Twilio .restClient = null ;
274
313
}
275
314
315
+ private static void invalidateOAuthCreds () {
316
+ Twilio .credentialProvider = null ;
317
+ }
318
+
319
+ private static void invalidateBasicCreds () {
320
+ Twilio .username = null ;
321
+ Twilio .password = null ;
322
+ }
323
+
276
324
/**
277
325
* Attempts to gracefully shutdown the ExecutorService if it is present.
278
326
*/
0 commit comments