@@ -97,30 +97,33 @@ public function getOauth2Info(): array
9797 }
9898
9999 /**
100- * Helper method to handle errors in json_decode
100+ * Create the account_token value.
101101 *
102- * @param string $json
103- * @param bool $assoc
104- * @param int $depth
105- * @param int $options
106- * @return mixed
107- * @throws ClientException
102+ * @param string $accountId
103+ * @param string $clientId
104+ * @param string $clientSecret
105+ * @return string
106+ * @throws \JsonException
108107 */
109- protected function json_decode (string $ json , bool $ assoc = true , int $ depth = 512 , int $ options = 0 )
108+ public function createAccountToken (string $ accountId , string $ clientId , string $ clientSecret ): string
110109 {
111- // Clear json_last_error()
112- \json_encode (null );
113-
114- $ data = @\json_decode ($ json , $ assoc , $ depth , $ options );
115-
116- if (\json_last_error () !== JSON_ERROR_NONE ) {
117- throw new \InvalidArgumentException (\sprintf (
118- 'Unable to decode JSON: %s ' ,
119- \json_last_error_msg ()
120- ));
121- }
122-
123- return $ data ;
110+ $ header = '{"alg":"HS256","typ":"JWT"} ' ;
111+ $ payload = \json_encode ([
112+ 'sub ' => $ accountId ,
113+ 'iat ' => \time (),
114+ 'jti ' => \hash ('sha256 ' , \uniqid ('' , true )),
115+ 'iss ' => 'SetaPDF-Signer CSC API ' ,
116+ "azp " => $ clientId
117+ ], \JSON_THROW_ON_ERROR );
118+
119+ $ b64urlEncode = static function (string $ data ): string {
120+ return \rtrim (\strtr (\base64_encode ($ data ), '+/ ' , '-_ ' ), '= ' );
121+ };
122+
123+ $ bytesToSign = $ b64urlEncode ($ header ) . '. ' . $ b64urlEncode ($ payload );
124+ $ signature = \hash_hmac ('sha256 ' , $ bytesToSign , \hash ('sha256 ' , $ clientSecret , true ), true );
125+
126+ return $ bytesToSign . '. ' . $ b64urlEncode ($ signature );
124127 }
125128
126129 /**
@@ -130,6 +133,7 @@ protected function json_decode(string $json, bool $assoc = true, int $depth = 51
130133 * @return array
131134 * @throws ClientExceptionInterface
132135 * @throws ClientException
136+ * @throws \JsonException
133137 */
134138 public function call (string $ path , ?string $ accessToken = null , array $ inputData = []): array
135139 {
@@ -154,7 +158,7 @@ public function call(string $path, ?string $accessToken = null, array $inputData
154158 throw new ClientException ('Error on ' . $ path . ': ' . $ response ->getBody (), $ response );
155159 }
156160
157- return $ this -> json_decode ((string ) $ response ->getBody ());
161+ return \ json_decode ((string ) $ response ->getBody (), true , 512 , \ JSON_THROW_ON_ERROR );
158162 }
159163
160164 /**
@@ -165,7 +169,9 @@ public function call(string $path, ?string $accessToken = null, array $inputData
165169 *
166170 * @param string|null $lang
167171 * @return array
172+ * @throws ClientException
168173 * @throws ClientExceptionInterface
174+ * @throws \JsonException
169175 * @see CSC API 11.1 /info
170176 */
171177 public function info (?string $ lang = null ): array
@@ -190,7 +196,9 @@ public function info(?string $lang = null): array
190196 * @param string|null $pageToken
191197 * @param string|null $clientData
192198 * @return array
193- * @throws ClientExceptionInterface|ClientException
199+ * @throws ClientException
200+ * @throws ClientExceptionInterface
201+ * @throws \JsonException
194202 * @see CSC API 11.4 /credentials/list
195203 */
196204 public function credentialsList (
@@ -229,7 +237,9 @@ public function credentialsList(
229237 * @param string|null $lang
230238 * @param string|null $clientData
231239 * @return array
232- * @throws ClientExceptionInterface|ClientException
240+ * @throws ClientException
241+ * @throws ClientExceptionInterface
242+ * @throws \JsonException
233243 * @see CSC API 11.5 /credentials/info
234244 */
235245 public function credentialsInfo (
@@ -275,7 +285,9 @@ public function credentialsInfo(
275285 * @param string $credentialID
276286 * @param string|null $clientData
277287 * @return array
278- * @throws ClientExceptionInterface|ClientException
288+ * @throws ClientException
289+ * @throws ClientExceptionInterface
290+ * @throws \JsonException
279291 * @see CSC API 11.8 /credentials/sendOTP
280292 */
281293 public function credentialsSendOTP (string $ accessToken , string $ credentialID , ?string $ clientData = null ): array
@@ -304,7 +316,9 @@ public function credentialsSendOTP(string $accessToken, string $credentialID, ?s
304316 * @param string|null $description
305317 * @param string|null $clientData
306318 * @return array
307- * @throws ClientExceptionInterface|ClientException
319+ * @throws ClientException
320+ * @throws ClientExceptionInterface
321+ * @throws \JsonException
308322 * @see CSC API 11.6 /credentials/authorize
309323 */
310324 public function credentialsAuthorize (
@@ -350,7 +364,9 @@ public function credentialsAuthorize(
350364 * @param string|null $signAlgoParams
351365 * @param string|null $clientData
352366 * @return array
353- * @throws ClientExceptionInterface|ClientException
367+ * @throws ClientException
368+ * @throws ClientExceptionInterface
369+ * @throws \JsonException
354370 * @see CSC API 11.9 /signatures/signHash
355371 */
356372 public function signaturesSignHash (
0 commit comments