1
1
<?php
2
2
namespace Twilio \Base ;
3
3
4
+ use Twilio \AuthStrategy \AuthStrategy ;
5
+ use Twilio \CredentialProvider \CredentialProvider ;
4
6
use Twilio \Exceptions \ConfigurationException ;
5
7
use Twilio \Exceptions \TwilioException ;
6
8
use Twilio \Http \Client as HttpClient ;
@@ -24,6 +26,7 @@ class BaseClient
24
26
25
27
protected $ username ;
26
28
protected $ password ;
29
+ protected $ credentialProvider ;
27
30
protected $ accountSid ;
28
31
protected $ region ;
29
32
protected $ edge ;
@@ -46,7 +49,6 @@ class BaseClient
46
49
* @param mixed[] $environment Environment to look for auth details, defaults
47
50
* to $_ENV
48
51
* @param string[] $userAgentExtensions Additions to the user agent string
49
- * @throws ConfigurationException If valid authentication is not present
50
52
*/
51
53
public function __construct (
52
54
?string $ username = null ,
@@ -59,18 +61,15 @@ public function __construct(
59
61
) {
60
62
$ this ->environment = $ environment ?: \getenv ();
61
63
62
- $ this ->username = $ this ->getArg ($ username , self ::ENV_ACCOUNT_SID );
63
- $ this ->password = $ this ->getArg ($ password , self ::ENV_AUTH_TOKEN );
64
+ $ this ->setUsername ( $ this ->getArg ($ username , self ::ENV_ACCOUNT_SID ) );
65
+ $ this ->setPassword ( $ this ->getArg ($ password , self ::ENV_AUTH_TOKEN ) );
64
66
$ this ->region = $ this ->getArg ($ region , self ::ENV_REGION );
65
67
$ this ->edge = $ this ->getArg (null , self ::ENV_EDGE );
66
68
$ this ->logLevel = $ this ->getArg (null , self ::ENV_LOG );
67
69
$ this ->userAgentExtensions = $ userAgentExtensions ?: [];
68
70
69
- if (!$ this ->username || !$ this ->password ) {
70
- throw new ConfigurationException ('Credentials are required to create a Client ' );
71
- }
72
-
73
- $ this ->accountSid = $ accountSid ?: $ this ->username ;
71
+ $ this ->invalidateOAuth ();
72
+ $ this ->setAccountSid ($ accountSid ?: $ this ->username );
74
73
75
74
if ($ httpClient ) {
76
75
$ this ->httpClient = $ httpClient ;
@@ -79,6 +78,36 @@ public function __construct(
79
78
}
80
79
}
81
80
81
+ public function setUsername (?string $ username ): void {
82
+ $ this ->username = $ username ;
83
+ }
84
+
85
+ public function setPassword (?string $ password ): void {
86
+ $ this ->password = $ password ;
87
+ }
88
+
89
+ public function setAccountSid (?string $ accountSid ): void {
90
+ $ this ->accountSid = $ accountSid ;
91
+ }
92
+
93
+ private function _setCredentialProvider ($ credentialProvider ): void {
94
+ $ this ->credentialProvider = $ credentialProvider ;
95
+ }
96
+
97
+ public function setCredentialProvider (CredentialProvider $ credentialProvider ): void {
98
+ $ this ->_setCredentialProvider ($ credentialProvider );
99
+ $ this ->invalidateBasicAuth ();
100
+ }
101
+
102
+ public function invalidateBasicAuth (): void {
103
+ $ this ->setUsername ("" );
104
+ $ this ->setPassword ("" );
105
+ }
106
+
107
+ public function invalidateOAuth (): void {
108
+ $ this ->_setCredentialProvider (null );
109
+ }
110
+
82
111
/**
83
112
* Determines argument value accounting for environment variables.
84
113
*
@@ -111,7 +140,9 @@ public function getArg(?string $arg, string $envVar): ?string
111
140
* @param string $username User for Authentication
112
141
* @param string $password Password for Authentication
113
142
* @param int $timeout Timeout in seconds
143
+ * @param AuthStrategy $authStrategy AuthStrategy for Authentication
114
144
* @return \Twilio\Http\Response Response from the Twilio API
145
+ * @throws TwilioException
115
146
*/
116
147
public function request (
117
148
string $ method ,
@@ -121,10 +152,26 @@ public function request(
121
152
array $ headers = [],
122
153
?string $ username = null ,
123
154
?string $ password = null ,
124
- ?int $ timeout = null
155
+ ?int $ timeout = null ,
156
+ ?AuthStrategy $ authStrategy = null
125
157
): \Twilio \Http \Response {
126
158
$ username = $ username ?: $ this ->username ;
127
159
$ password = $ password ?: $ this ->password ;
160
+ $ authStrategy = $ authStrategy ?: null ;
161
+ if ($ this ->credentialProvider ) {
162
+ $ authStrategy = $ this ->credentialProvider ->toAuthStrategy ();
163
+ }
164
+
165
+ if (!$ authStrategy ) {
166
+ if (!$ username ) {
167
+ throw new ConfigurationException ('username is required ' );
168
+ }
169
+
170
+ if (!$ password ) {
171
+ throw new ConfigurationException ("password is required " );
172
+ }
173
+ }
174
+
128
175
$ logLevel = (getenv ('DEBUG_HTTP_TRAFFIC ' ) === 'true ' ? 'debug ' : $ this ->getLogLevel ());
129
176
130
177
$ headers ['User-Agent ' ] = 'twilio-php/ ' . VersionInfo::string () .
@@ -136,7 +183,8 @@ public function request(
136
183
$ headers ['User-Agent ' ] .= ' ' . implode (' ' , $ this ->userAgentExtensions );
137
184
}
138
185
139
- if (!\array_key_exists ('Accept ' , $ headers )) {
186
+ // skip adding Accept header in case of delete operation
187
+ if ($ method !== "DELETE " && !\array_key_exists ('Accept ' , $ headers )) {
140
188
$ headers ['Accept ' ] = 'application/json ' ;
141
189
}
142
190
@@ -169,7 +217,8 @@ public function request(
169
217
$ headers ,
170
218
$ username ,
171
219
$ password ,
172
- $ timeout
220
+ $ timeout ,
221
+ $ authStrategy
173
222
);
174
223
175
224
if ($ logLevel === 'debug ' ) {
0 commit comments