streams/Zotlabs/Module/Jwks.php

61 lines
1.3 KiB
PHP
Raw Normal View History

2019-10-10 00:34:18 +00:00
<?php
namespace Zotlabs\Module;
use Zotlabs\Lib\Keyutils;
2019-10-10 03:18:27 +00:00
use Zotlabs\Web\Controller;
2019-10-10 00:34:18 +00:00
2019-10-10 03:18:27 +00:00
class Jwks extends Controller {
2019-10-10 00:34:18 +00:00
function init() {
2020-11-13 04:41:44 +00:00
Keyutils::pemtome(get_config('system','pubkey'),$m,$e);
/**
* RFC7518
*
6.3.1.1. "n" (Modulus) Parameter
The "n" (modulus) parameter contains the modulus value for the RSA
public key. It is represented as a Base64urlUInt-encoded value.
Note that implementers have found that some cryptographic libraries
prefix an extra zero-valued octet to the modulus representations they
return, for instance, returning 257 octets for a 2048-bit key, rather
than 256. Implementations using such libraries will need to take
care to omit the extra octet from the base64url-encoded
representation.
*
*/
$l = strlen((string) $m);
if ($l & 1) {
$m = substr((string) $m,1);
}
2019-10-10 00:34:18 +00:00
$keys = [
[
'e' => base64url_encode($e),
'n' => base64url_encode($m),
'kty' => 'RSA',
2020-11-12 05:37:13 +00:00
'kid' => '0',
2019-10-10 00:34:18 +00:00
]
];
$ret = [
2019-10-10 03:18:27 +00:00
'keys' => $keys
2019-10-10 00:34:18 +00:00
];
2019-10-10 03:18:27 +00:00
if (argc() > 1) {
$entry = intval(argv(1));
if ($keys[$entry]) {
unset($keys[$entry]['kid']);
json_return_and_die($keys[$entry],'application/jwk+json');
}
}
json_return_and_die($ret,'application/jwk-set+json');
2019-10-10 00:34:18 +00:00
}
}