Skip to content

Commit efe03a2

Browse files
committed
Use phpseclib for generating keys
1 parent fb5a3c9 commit efe03a2

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
"illuminate/http": "~5.3",
2323
"illuminate/support": "~5.3",
2424
"league/oauth2-server": "~5.0",
25-
"symfony/process": "~3.1",
2625
"symfony/psr-http-message-bridge": "^0.3.0",
27-
"zendframework/zend-diactoros": "~1.0"
26+
"zendframework/zend-diactoros": "~1.0",
27+
"phpseclib/phpseclib": "^2.0"
2828
},
2929
"require-dev": {
3030
"mockery/mockery": "~0.9",

src/Console/KeysCommand.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Laravel\Passport\Console;
44

5+
use phpseclib\Crypt\RSA;
56
use Illuminate\Console\Command;
6-
use Symfony\Component\Process\Process;
77

88
class KeysCommand extends Command
99
{
@@ -24,16 +24,15 @@ class KeysCommand extends Command
2424
/**
2525
* Execute the console command.
2626
*
27+
* @param RSA $rsa
2728
* @return mixed
2829
*/
29-
public function handle()
30+
public function handle(RSA $rsa)
3031
{
31-
$callback = function ($type, $line) {
32-
$this->output->write($line);
33-
};
32+
$keys = $rsa->createKey(4096);
3433

35-
(new Process('openssl genrsa -out oauth-private.key 4096', storage_path()))->run($callback);
36-
(new Process('openssl rsa -in oauth-private.key -pubout -out oauth-public.key', storage_path()))->run($callback);
34+
file_put_contents(storage_path('oauth-private.key'), array_get($keys, 'privatekey'));
35+
file_put_contents(storage_path('oauth-public.key'), array_get($keys, 'publickey'));
3736

3837
$this->info('Encryption keys generated successfully.');
3938
}

tests/KeysCommandTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
function storage_path($file = null)
4+
{
5+
return __DIR__ . DIRECTORY_SEPARATOR . $file;
6+
}
7+
8+
class KeysCommandTest extends PHPUnit_Framework_TestCase
9+
{
10+
public function tearDown()
11+
{
12+
Mockery::close();
13+
14+
@unlink(storage_path('oauth-private.key'));
15+
@unlink(storage_path('oauth-public.key'));
16+
}
17+
18+
public function testPrivateAndPublicKeysAreGenerated()
19+
{
20+
$command = Mockery::mock(\Laravel\Passport\Console\KeysCommand::class)
21+
->makePartial()
22+
->shouldReceive('info')
23+
->with('Encryption keys generated successfully.')
24+
->getMock();
25+
26+
$rsa = new \phpseclib\Crypt\RSA();
27+
28+
$command->handle($rsa);
29+
30+
$this->assertFileExists(storage_path('oauth-private.key'));
31+
$this->assertFileExists(storage_path('oauth-public.key'));
32+
}
33+
}

0 commit comments

Comments
 (0)