-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjws.php
More file actions
52 lines (42 loc) · 1.38 KB
/
jws.php
File metadata and controls
52 lines (42 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
*
* @Author: Max Base
* @Date: 05/25/2025
* @Repository: https://github.com/BaseMax/moadian-api-example
*
**/
require_once "nonce.php"; // for $nonce
// Config
$clientId = 'xxxxxxxxx';
$privateKeyPath = __DIR__ . '/private_key.txt';
$certificatePath = __DIR__ . '/cert.cer';
function base64url_encode($data)
{
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
$sigT = gmdate("Y-m-d\TH:i:s\Z");
$cert = file_get_contents($certificatePath);
$certClean = trim(str_replace(["-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----", "\n", "\r"], "", $cert));
$header = [
"alg" => "RS256",
"x5c" => [$certClean],
"sigT" => $sigT,
"crit" => ["sigT"]
];
$payload = [
"nonce" => $nonce,
"clientId" => $clientId
];
$encodedHeader = base64url_encode(json_encode($header, JSON_UNESCAPED_SLASHES));
$encodedPayload = base64url_encode(json_encode($payload, JSON_UNESCAPED_SLASHES));
$dataToSign = $encodedHeader . '.' . $encodedPayload;
$privateKeyPem = file_get_contents($privateKeyPath);
$privateKey = openssl_pkey_get_private($privateKeyPem);
if (!$privateKey) {
die("کلید خصوصی بارگذاری نشد.");
}
openssl_sign($dataToSign, $signature, $privateKey, OPENSSL_ALGO_SHA256);
$encodedSignature = base64url_encode($signature);
$jwsToken = $encodedHeader . '.' . $encodedPayload . '.' . $encodedSignature;
print "JWS Token: $jwsToken\n";