|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +declare(strict_types=1); |
| 5 | +/* |
| 6 | + * @see https://github.com/zendframework/zend-expressive-authentication-oauth2 for the canonical source repository |
| 7 | + * |
| 8 | + * @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com) |
| 9 | + * @license https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/LICENSE.md |
| 10 | + * New BSD License |
| 11 | + */ |
| 12 | + |
| 13 | +/* |
| 14 | + * Script to generate public, private and encryption keys for thephpleague/oauth2-server. |
| 15 | + * |
| 16 | + * @see https://oauth2.thephpleague.com/installation/ |
| 17 | + */ |
| 18 | + |
| 19 | +echo "\n"; |
| 20 | +echo "This script is provided as a convenient way to generate keys for\n"; |
| 21 | +echo "the OAuth2 server provider. You may choose instead to use an\n"; |
| 22 | +echo "alternative method. For more information, see the install docs:\n"; |
| 23 | +echo "https://oauth2.thephpleague.com/installation/\n\n"; |
| 24 | + |
| 25 | +if (!extension_loaded('openssl')) { |
| 26 | + fwrite(STDERR, 'Extension \'openssl\' is not available' . PHP_EOL); |
| 27 | + exit(1); |
| 28 | +} |
| 29 | + |
| 30 | +// find the best dir |
| 31 | +if ( |
| 32 | + // see if there's a data dir of the parent application |
| 33 | + file_exists($dataDir = realpath(__DIR__ . '/../../../../data')) |
| 34 | +) { |
| 35 | + printf("Found a good location for keys:\n%s\n\n", $dataDir); |
| 36 | +} elseif ( |
| 37 | + // fallback to data dir of this package |
| 38 | + file_exists($dataDir = dirname(__DIR__) . '/data') |
| 39 | + // or, simply the parent directory |
| 40 | + || $dataDir = dirname(__DIR__) |
| 41 | +) { |
| 42 | + printf("Best available location for keys:\n%s\n", $dataDir); |
| 43 | + printf("You'll likely want to move them to a better location\n\n"); |
| 44 | +} else { |
| 45 | + fwrite(STDERR, 'Unable to find a location to write the keys' . PHP_EOL); |
| 46 | + exit(1); |
| 47 | +} |
| 48 | + |
| 49 | +if (!is_writable($dataDir)) { |
| 50 | + fwrite(STDERR, 'Directory ' . $dataDir . ' is not writable' . PHP_EOL); |
| 51 | + exit(1); |
| 52 | +} |
| 53 | + |
| 54 | +$dataDir = $dataDir . '/oauth'; |
| 55 | +printf("We'll put them in a subdirectory:\n%s\n\n", $dataDir); |
| 56 | + |
| 57 | +if (!file_exists($dataDir)) { |
| 58 | + mkdir($dataDir); |
| 59 | +} |
| 60 | + |
| 61 | +$filePrivateKey = $dataDir . '/private.key'; |
| 62 | +$filePublicKey = $dataDir . '/public.key'; |
| 63 | +$fileEncryptionKey = $dataDir . '/encryption.key'; |
| 64 | + |
| 65 | +// Generate public/private keys with OpenSSL |
| 66 | +$config = [ |
| 67 | + 'private_key_bits' => $bits = 2048, |
| 68 | + 'private_key_type' => OPENSSL_KEYTYPE_RSA, |
| 69 | +]; |
| 70 | + |
| 71 | +printf('Using %d bits to generate key of type RSA' . "\n\n", $bits); |
| 72 | + |
| 73 | +// Private key |
| 74 | +$res = openssl_pkey_new($config); |
| 75 | + |
| 76 | +if (!is_resource($res)) { |
| 77 | + fwrite(STDERR, 'Failed to create private key.' . PHP_EOL); |
| 78 | + fwrite(STDERR, 'Check your openssl extension settings.' . PHP_EOL); |
| 79 | + exit(1); |
| 80 | +} |
| 81 | + |
| 82 | +openssl_pkey_export($res, $privateKey); |
| 83 | +file_put_contents($filePrivateKey, $privateKey); |
| 84 | +printf("Private key stored in:\n%s\n", $filePrivateKey); |
| 85 | + |
| 86 | +// Public key |
| 87 | +$publicKey = openssl_pkey_get_details($res); |
| 88 | +file_put_contents($filePublicKey, $publicKey['key']); |
| 89 | +printf("Public key stored in:\n%s\n", $filePublicKey); |
| 90 | + |
| 91 | +// Encryption key |
| 92 | +$encKey = base64_encode(random_bytes(32)); |
| 93 | +file_put_contents($fileEncryptionKey, sprintf("<?php return '%s';", $encKey)); |
| 94 | +printf("Encryption key stored in:\n%s\n", $fileEncryptionKey); |
| 95 | + |
| 96 | +echo "\n"; |
0 commit comments