11package com .twilio ;
22
3+ import com .twilio .annotations .Beta ;
4+ import com .twilio .auth_strategy .AuthStrategy ;
35import com .twilio .exception .ApiException ;
46import com .twilio .exception .AuthenticationException ;
57import com .twilio .exception .CertificateValidationException ;
8+ import com .twilio .credential .CredentialProvider ;
69import com .twilio .http .HttpMethod ;
710import com .twilio .http .NetworkHttpClient ;
811import com .twilio .http .Request ;
@@ -34,6 +37,8 @@ public class Twilio {
3437 private static String edge = System .getenv ("TWILIO_EDGE" );
3538 private static volatile TwilioRestClient restClient ;
3639 private static volatile ExecutorService executorService ;
40+
41+ private static CredentialProvider credentialProvider ;
3742
3843 private Twilio () {
3944 }
@@ -64,6 +69,31 @@ public static synchronized void init(final String username, final String passwor
6469 Twilio .setAccountSid (null );
6570 }
6671
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+
6797 /**
6898 * Initialize the Twilio environment.
6999 *
@@ -91,6 +121,7 @@ public static synchronized void setUsername(final String username) {
91121 if (!username .equals (Twilio .username )) {
92122 Twilio .invalidate ();
93123 }
124+ Twilio .invalidateOAuthCreds ();
94125
95126 Twilio .username = username ;
96127 }
@@ -109,6 +140,7 @@ public static synchronized void setPassword(final String password) {
109140 if (!password .equals (Twilio .password )) {
110141 Twilio .invalidate ();
111142 }
143+ Twilio .invalidateOAuthCreds ();
112144
113145 Twilio .password = password ;
114146 }
@@ -181,12 +213,19 @@ public static TwilioRestClient getRestClient() {
181213
182214 private static TwilioRestClient buildRestClient () {
183215 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 );
187228 }
188-
189- TwilioRestClient .Builder builder = new TwilioRestClient .Builder (Twilio .username , Twilio .password );
190229
191230 if (Twilio .accountSid != null ) {
192231 builder .accountSid (Twilio .accountSid );
@@ -273,6 +312,15 @@ private static void invalidate() {
273312 Twilio .restClient = null ;
274313 }
275314
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+
276324 /**
277325 * Attempts to gracefully shutdown the ExecutorService if it is present.
278326 */
0 commit comments