-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.php
executable file
·59 lines (42 loc) · 1.19 KB
/
generate.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
<?php
// Copyright (c) 2019 Yodlee, Inc. All Rights Reserved.
// Licensed under the MIT License. See `LICENSE` for details.
error_reporting(E_STRICT);
require __DIR__.'/vendor/autoload.php';
use Firebase\JWT\JWT;
function generate()
{
$options = getopt('k:i:u:');
$requiredOptions = ['k', 'i'];
$errors = [];
foreach ($requiredOptions as $opt) {
if (! isset($options[$opt])) {
$errors[] = "Option $opt is required";
}
}
if (count($errors)) {
throw new InvalidArgumentException(implode(".\n", $errors));
}
$time = time();
$keyfile = $options['k'];
$key = file_get_contents($keyfile);
if ($key === false) {
throw new Exception("Private key file ($keyfile): no such file or directory");
}
$payload = [
'iss' => $options['i'],
'iat' => $time,
'exp' => $time + 1800,
];
if (isset($options['u'])) {
$payload['sub'] = $options['u'];
}
return $jwt = JWT::encode($payload, $key, 'RS512');
}
if (basename(__FILE__) == $_SERVER['SCRIPT_FILENAME']) {
try {
echo generate()."\n";
} catch (Exception $e) {
echo $e->getMessage().".\n";
}
}