-
Notifications
You must be signed in to change notification settings - Fork 0
/
JWTProvider.php
241 lines (182 loc) · 7.15 KB
/
JWTProvider.php
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* User: Deesingh
* Date: 03/12/19
* Time: 16:46
*/
class JWTProvider
{
private const SIGN_ALGORITHM = 'RS256';
/**
* Converts and signs array into a JWT string.
*
* @param array $payload
* @param string $key
*
* @return string
* @throws \InvalidArgumentException
*/
public function encode(array $payload, string $key)
{
$header = ['typ' => 'JWT', 'alg' => self::SIGN_ALGORITHM];
$headerJson = json_encode($header);
$segments[] = $this->urlSafeB64Encode($headerJson);
$payloadJson = json_encode($payload);
$segments[] = $this->urlSafeB64Encode($payloadJson);
//now going to use openssl_sign()
$result = openssl_sign(implode('.', $segments),
$signature,
$key,
'sha256');
if (false === $result) {
throw new \RuntimeException('Failed to encrypt value. ' . implode("\n", $this->getSslErrors()));
}
$segments[] = $this->urlSafeB64Encode($signature);
return implode('.', $segments); //PACK THE ARRAY CONTAINING JWT
}
/**
* Encode a string with URL-safe Base64.
*
* @param string $input The string you want encoded
*
* @return string
*/
private function urlSafeB64Encode(string $input): string
{
return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
}
/**
* Builds request JWT.
*
* @param string $formattableTimeString
* @param string $issuer
* @param string $subject
* @param string $audience
* @param string $services
* @return string
*/
public function buildJWTPayload($formattableTimeString, $issuer, $subject, $audience, $services)
{
$data = [
"exp" => strtotime($formattableTimeString),
"iss" => $issuer,
"sub" => $subject,
"aud" => $audience
];
if (is_array($services)) {
foreach ($services as &$aService) {
$data[$aService] = true;
}
} else {
//assuming a single service
$data[$services] = true;
}
return $data;
}
}
function psvm()
{
$cl_options = ""
. "i:" // Client Id
. "s:" // Client Secret
. "k:" // Key File path
. "u:" // Issuer
. "b:" // Subject
. "a:" // Audience
. "c:" // services
. "e::"; // expires
$longopts = array( //just for reference, not used actually
"client-id:",
"client-secret:",
"key-file:",
"iss:",
"sub:",
"aud:",
"services:",
"exp::"
);
$show_usage = false;
$cmdLineOpts = getopt($cl_options);
if (isset($cmdLineOpts["i"])) {
$client_id = $cmdLineOpts["i"];
if (isset($cmdLineOpts["s"])) {
$client_secret = $cmdLineOpts["s"];
if (isset($cmdLineOpts["k"])) {
$key_file = $cmdLineOpts["k"];
echo "Key file is " . $key_file . "\n";
if (is_file($key_file) && is_readable($key_file)) {
$fHandle = fopen($key_file, "r") or die("Unable to read the key file " . $key_file);
$private_key = fread($fHandle, filesize($key_file)) or die("Unable to read the key file " . $key_file);
fclose($fHandle);
if (isset($cmdLineOpts["u"])) { //Checking issuer
$issuer = $cmdLineOpts["u"];
if (isset($cmdLineOpts["b"])) { //checking subject
$subject = $cmdLineOpts["b"];
if (isset($cmdLineOpts["a"])) { //checking audience
$audience = $cmdLineOpts["a"];
if (isset($cmdLineOpts["c"])) { //checking services
$services = preg_split("/,/", $cmdLineOpts["c"]);
if (isset($cmdLineOpts["e"])) {
$exp_time = $cmdLineOpts["e"];
} else {
echo "exp_time not provided assuming 1 day\n";
$exp_time = '1 Day';
}
$jwtInstance = new JWTProvider();
$curl = curl_init();
$payload = $jwtInstance->buildJWTPayload($exp_time,
$issuer,
$subject,
$audience,
$services);
$jwt = $jwtInstance->encode($payload, $private_key);
echo "JWT Prepared:" . $jwt . "\n\n";
$url = "https://ims-na1.adobelogin.com/ims/exchange/jwt/"; //token auth Endpoint
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
"client_id" => $client_id,
"client_secret" => $client_secret,
"jwt_token" => $jwt,
));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
echo "Result of JWT Call\n\n" . $result . "\n";
} else {
echo "Services not provided\n";
$show_usage = true;
}
} else {
echo "Audience not provided\n";
$show_usage = true;
}
} else {
echo "Subject not provided\n";
$show_usage = true;
}
} else {
echo "Issuer not provided\n";
$show_usage = true;
}
} else {
echo "Unable to read the key file " . $key_file . '\n';
}
} else {
echo "Key file location not provided\n";
$show_usage = true;
}
} else {
echo "Client-secret not provided\n";
$show_usage = true;
}
} else {
echo "Client ID not provided\n";
$show_usage = true;
}
if ($show_usage) {
echo "Usage:\n JWTProvider.php -i <client-id> -s <client-secret> -k <key-file> -u <issuer> -b <subject> -a <audience> -c <services comma separated> -e <exp time, default 1 Day>";
}
}
psvm();
exit(1);