Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 5f0e718

Browse files
authored
Merge pull request #37 from chdr/gen-keys/35
Write keys to parent app data dir if available
2 parents 8ff0006 + f49c796 commit 5f0e718

File tree

4 files changed

+110
-50
lines changed

4 files changed

+110
-50
lines changed

bin/generate-keys.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

bin/generate-oauth2-keys

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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";

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
"dev-master": "0.4.x-dev"
5656
}
5757
},
58+
"bin": [
59+
"bin/generate-oauth2-keys"
60+
],
5861
"scripts": {
5962
"check": [
6063
"@cs-check",
@@ -63,7 +66,6 @@
6366
"cs-check": "phpcs",
6467
"cs-fix": "phpcbf",
6568
"test": "phpunit --colors=always",
66-
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
67-
"generate-keys": "php bin/generate-keys.php"
69+
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
6870
}
6971
}

docs/book/intro.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ If you need an introduction to OAuth2, you can read the following references:
2222

2323
In order to implement the OAuth2 server, we first need to configure it. The
2424
first step is to generate new cryptographic keys. We need to execute the script
25-
`bin/generate-keys.php` in order to generate these keys.
25+
`bin/generate-oauth2-keys` in order to generate these keys.
2626

2727
```bash
28-
$ php vendor/bin/generate-keys.php
28+
$ ./vendor/bin/generate-oauth2-keys
2929
```
3030

31-
This script will store the keys in the `data` folder:
31+
This script will store the keys in the parent application `data` folder if found:
3232

3333
```
3434
Private key stored in:
35-
./data/private.key
35+
./data/oauth/private.key
3636
Public key stored in:
37-
./data/public.key
37+
./data/oauth/public.key
3838
Encryption key stored in:
39-
./data/encryption.key
39+
./data/oauth/encryption.key
4040
```
4141

4242
The script will generate public and private keys, and an encryption key.
@@ -52,9 +52,9 @@ The default values are:
5252

5353
```php
5454
return [
55-
'private_key' => __DIR__ . '/../data/private.key',
56-
'public_key' => __DIR__ . '/../data/public.key',
57-
'encryption_key' => require __DIR__ . '/../data/encryption.key',
55+
'private_key' => __DIR__ . '/../data/oauth/private.key',
56+
'public_key' => __DIR__ . '/../data/oauth/public.key',
57+
'encryption_key' => require __DIR__ . '/../data/oauth/encryption.key',
5858
'access_token_expire' => 'P1D',
5959
'refresh_token_expire' => 'P1M',
6060
'auth_code_expire' => 'PT10M',
@@ -68,7 +68,7 @@ return [
6868

6969
The `private_key` and `public_key` values contains the paths to the previous
7070
generated pair of keys. The `encryption_key` contains the encryption key value
71-
as a string, as stored in the `data/encryption.key` file.
71+
as a string, as stored in the `data/oauth/encryption.key` file.
7272

7373
The `access_token_expire` value is the time-to-live (TTL) value of the access
7474
token. The time period is represented using the [DateInterval](http://php.net/manual/en/class.dateinterval.php)

0 commit comments

Comments
 (0)