|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MinVWS\OpenIDConnectLaravel\Tests; |
| 6 | + |
| 7 | +use Jose\Component\Core\AlgorithmManager; |
| 8 | +use Jose\Component\Core\JWK; |
| 9 | +use Jose\Component\Encryption\Algorithm\ContentEncryption\A128CBCHS256; |
| 10 | +use Jose\Component\Encryption\Algorithm\KeyEncryption\RSAOAEP; |
| 11 | +use Jose\Component\Encryption\Compression\CompressionMethodManager; |
| 12 | +use Jose\Component\Encryption\Compression\Deflate; |
| 13 | +use Jose\Component\Encryption\JWEBuilder; |
| 14 | +use Jose\Component\Encryption\Serializer\CompactSerializer; |
| 15 | +use Jose\Component\KeyManagement\JWKFactory; |
| 16 | +use JsonException; |
| 17 | +use OpenSSLAsymmetricKey; |
| 18 | +use OpenSSLCertificate; |
| 19 | +use OpenSSLCertificateSigningRequest; |
| 20 | +use RuntimeException; |
| 21 | + |
| 22 | +function buildJweString(string $payload, JWK $recipient): string |
| 23 | +{ |
| 24 | + // Create the JWE builder object |
| 25 | + $jweBuilder = new JWEBuilder( |
| 26 | + new AlgorithmManager([new RSAOAEP()]), |
| 27 | + new AlgorithmManager([new A128CBCHS256()]), |
| 28 | + new CompressionMethodManager([new Deflate()]) |
| 29 | + ); |
| 30 | + |
| 31 | + // Build the JWE |
| 32 | + $jwe = $jweBuilder |
| 33 | + ->create() |
| 34 | + ->withPayload($payload) |
| 35 | + ->withSharedProtectedHeader([ |
| 36 | + 'alg' => 'RSA-OAEP', |
| 37 | + 'enc' => 'A128CBC-HS256', |
| 38 | + 'zip' => 'DEF', |
| 39 | + ]) |
| 40 | + ->addRecipient($recipient) |
| 41 | + ->build(); |
| 42 | + |
| 43 | + // Get the compact serialization of the JWE |
| 44 | + return (new CompactSerializer())->serialize($jwe, 0); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * @throws JsonException |
| 49 | + */ |
| 50 | +function buildExamplePayload(): string |
| 51 | +{ |
| 52 | + return json_encode([ |
| 53 | + 'iat' => time(), |
| 54 | + 'nbf' => time(), |
| 55 | + 'exp' => time() + 3600, |
| 56 | + 'iss' => 'My service', |
| 57 | + 'aud' => 'Your application', |
| 58 | + ], JSON_THROW_ON_ERROR); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Generate OpenSSL Key and return the tempfile resource |
| 63 | + * @return array{OpenSSLAsymmetricKey, resource} |
| 64 | + */ |
| 65 | +function generateOpenSSLKey(): array |
| 66 | +{ |
| 67 | + $file = tmpfile(); |
| 68 | + if (!is_resource($file)) { |
| 69 | + throw new RuntimeException('Could not create temporary file'); |
| 70 | + } |
| 71 | + |
| 72 | + $key = openssl_pkey_new([ |
| 73 | + 'private_key_bits' => 512, |
| 74 | + 'private_key_type' => OPENSSL_KEYTYPE_RSA, |
| 75 | + ]); |
| 76 | + if (!$key instanceof OpenSSLAsymmetricKey) { |
| 77 | + throw new RuntimeException('Could not generate private key'); |
| 78 | + } |
| 79 | + |
| 80 | + openssl_pkey_export($key, $privateKey); |
| 81 | + fwrite($file, $privateKey); |
| 82 | + |
| 83 | + return [$key, $file]; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Generate X509 certificate |
| 88 | + * @param OpenSSLAsymmetricKey $key |
| 89 | + * @return OpenSSLCertificate |
| 90 | + */ |
| 91 | +function generateX509Certificate(OpenSSLAsymmetricKey $key): OpenSSLCertificate |
| 92 | +{ |
| 93 | + $csr = openssl_csr_new([], $key); |
| 94 | + if (!$csr instanceof OpenSSLCertificateSigningRequest) { |
| 95 | + throw new RuntimeException('Could not generate CSR'); |
| 96 | + } |
| 97 | + |
| 98 | + $certificate = openssl_csr_sign($csr, null, $key, 365); |
| 99 | + if (!$certificate instanceof OpenSSLCertificate) { |
| 100 | + throw new RuntimeException('Could not generate X509 certificate'); |
| 101 | + } |
| 102 | + |
| 103 | + return $certificate; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Get JWK from resource |
| 108 | + * @param $resource resource |
| 109 | + * @return JWK |
| 110 | + */ |
| 111 | +function getJwkFromResource($resource): JWK |
| 112 | +{ |
| 113 | + if (!is_resource($resource)) { |
| 114 | + throw new RuntimeException('Could not create temporary file'); |
| 115 | + } |
| 116 | + |
| 117 | + return JWKFactory::createFromKeyFile(stream_get_meta_data($resource)['uri']); |
| 118 | +} |
0 commit comments