4848 */
4949class Authenticator extends NullAuthenticator implements \Stringable
5050{
51+ private DataCenter $ dataCenter ;
5152 private CacheInterface $ cache ;
5253 private string $ cacheKey ;
5354 private Client $ client ;
5455
56+ /**
57+ * @noinspection PhpDocSignatureInspection
58+ *
59+ * @param null|key-of<\Guanguans\Notify\ZohoCliq\DataCenter::BASE_URI_MAP> $dataCenter
60+ */
5561 public function __construct (
5662 private string $ clientId ,
5763 #[\SensitiveParameter]
5864 private string $ clientSecret ,
65+ ?string $ dataCenter = null ,
5966 ?CacheInterface $ cache = null ,
6067 ?string $ cacheKey = null ,
6168 ?Client $ client = null ,
6269 ) {
70+ $ this ->dataCenter = new DataCenter ($ dataCenter );
6371 $ this ->cache = $ cache ?? new FileCache ;
64- $ this ->cacheKey = $ cacheKey ?? "zoho_cliq.access_token. $ clientId. $ clientSecret " ;
65- $ this ->client = $ client ?? new Client ;
72+ $ this ->cacheKey = $ cacheKey ?? "zoho_cliq.access_token. $ this -> dataCenter . $ clientId. $ clientSecret " ;
73+ $ this ->client = ( $ client ?? new Client )-> push ( $ this -> baseUriMiddleware ( $ this -> dataCenter -> toOauthBaseUri ())) ;
6674 }
6775
6876 /**
@@ -80,35 +88,20 @@ public function applyToMiddleware(callable $handler): callable
8088 {
8189 return array_reduce (
8290 [
83- [ $ this , ' dataCenter ' ] ,
84- [ $ this , ' retry ' ] ,
85- [ $ this , ' authenticate ' ] ,
91+ $ this -> retryMiddleware () ,
92+ $ this -> authMiddleware () ,
93+ $ this -> baseUriMiddleware ( $ this -> dataCenter -> toBaseUri ()) ,
8694 ],
8795 static fn (callable $ handler , callable $ next ): callable => $ next ($ handler ),
8896 $ handler ,
8997 );
9098 }
9199
92- /**
93- * @todo
94- */
95- private function dataCenter (callable $ handler ): callable
96- {
97- return $ handler ;
98- }
99-
100- private function authenticate (callable $ handler ): callable
101- {
102- return Middleware::mapRequest (
103- fn (RequestInterface $ request ): RequestInterface => $ request ->withHeader ('Authorization ' , "Bearer $ this " ),
104- )($ handler );
105- }
106-
107100 /**
108101 * @see \GuzzleHttp\RetryMiddleware::onFulfilled()
109102 * @see \GuzzleHttp\RetryMiddleware::onRejected()
110103 */
111- private function retry ( callable $ handler ): callable
104+ private function retryMiddleware ( ): callable
112105 {
113106 return Middleware::retry (
114107 function (int $ retries , RequestInterface &$ request , ?ResponseInterface $ response = null ): bool {
@@ -124,7 +117,31 @@ function (int $retries, RequestInterface &$request, ?ResponseInterface $response
124117
125118 return false ;
126119 }
127- )($ handler );
120+ );
121+ }
122+
123+ private function authMiddleware (): callable
124+ {
125+ return Middleware::mapRequest (
126+ fn (RequestInterface $ request ): RequestInterface => $ request ->withHeader ('Authorization ' , "Bearer $ this " ),
127+ );
128+ }
129+
130+ /**
131+ * @see \GuzzleHttp\Client::buildUri()
132+ * @see \GuzzleHttp\Client::requestAsync()
133+ * @see \GuzzleHttp\Client::sendAsync()
134+ */
135+ private function baseUriMiddleware (string $ baseUri ): callable
136+ {
137+ $ parsedBaseUri = parse_url ($ baseUri );
138+
139+ return Middleware::mapRequest (
140+ static fn (RequestInterface $ request ): RequestInterface => $ request ->withUri (
141+ $ request ->getUri ()->withScheme ($ parsedBaseUri ['scheme ' ])->withHost ($ parsedBaseUri ['host ' ]),
142+ $ request ->hasHeader ('Host ' )
143+ ),
144+ );
128145 }
129146
130147 /**
@@ -157,6 +174,10 @@ private function getToken(): string
157174 * "expires_in": 3600
158175 * }
159176 * ```
177+ *
178+ * ```json
179+ * {"error":"invalid_client_secret"}
180+ * ```
160181 */
161182 private function refreshToken (): string
162183 {
0 commit comments