Skip to content

Commit bb211a9

Browse files
committed
Add tests for making crypto keys
1 parent 01c1be3 commit bb211a9

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

src/PassportServiceProvider.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
use Illuminate\Auth\Events\Logout;
88
use Illuminate\Support\Facades\Auth;
99
use Illuminate\Support\Facades\Event;
10-
use Illuminate\Support\Facades\Config;
1110
use Illuminate\Support\Facades\Cookie;
1211
use Illuminate\Support\Facades\Request;
1312
use Illuminate\Support\ServiceProvider;
1413
use Laravel\Passport\Guards\TokenGuard;
1514
use League\OAuth2\Server\CryptKey;
1615
use League\OAuth2\Server\ResourceServer;
16+
use Illuminate\Config\Repository as Config;
1717
use League\OAuth2\Server\AuthorizationServer;
1818
use League\OAuth2\Server\Grant\AuthCodeGrant;
1919
use League\OAuth2\Server\Grant\ImplicitGrant;
@@ -231,8 +231,11 @@ protected function registerResourceServer()
231231
*/
232232
protected function makeCryptKey($type)
233233
{
234-
$key = Config::get('passport.'.$type.'_key') ?? 'file://'.Passport::keyPath('oauth-'.$type.'.key');
235-
$key = str_replace('\\n', "\n", $key);
234+
$key = str_replace('\\n', "\n", $this->app->make(Config::class)->get('passport.'.$type.'_key'));
235+
236+
if (!$key) {
237+
$key = 'file://'.Passport::keyPath('oauth-'.$type.'.key');
238+
}
236239

237240
return new CryptKey($key, null, false);
238241
}

tests/PassportServiceProviderTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
use Laravel\Passport\Passport;
4+
use PHPUnit\Framework\TestCase;
5+
use Illuminate\Config\Repository as Config;
6+
use Laravel\Passport\PassportServiceProvider;
7+
use Illuminate\Contracts\Foundation\Application as App;
8+
9+
class PassportServiceProviderTest extends TestCase
10+
{
11+
public function test_can_use_crypto_keys_from_config()
12+
{
13+
$config = Mockery::mock(Config::class, function ($config) {
14+
$config->shouldReceive('get')
15+
->with('passport.private_key')
16+
->andReturn('-----BEGIN RSA PRIVATE KEY-----\nconfig\n-----END RSA PRIVATE KEY-----');
17+
});
18+
19+
$provider = new PassportServiceProvider(
20+
Mockery::mock(App::class, ['make' => $config])
21+
);
22+
23+
// Call protected makeCryptKey method
24+
$cryptKey = (function () {
25+
return $this->makeCryptKey('private');
26+
})->call($provider);
27+
28+
$this->assertSame(
29+
"-----BEGIN RSA PRIVATE KEY-----\nconfig\n-----END RSA PRIVATE KEY-----",
30+
file_get_contents($cryptKey->getKeyPath())
31+
);
32+
}
33+
34+
public function test_can_use_crypto_keys_from_local_disk()
35+
{
36+
Passport::loadKeysFrom(__DIR__.'/files');
37+
38+
file_put_contents(
39+
__DIR__.'/files/oauth-private.key',
40+
"-----BEGIN RSA PRIVATE KEY-----\ndisk\n-----END RSA PRIVATE KEY-----"
41+
);
42+
43+
$config = Mockery::mock(Config::class, function ($config) {
44+
$config->shouldReceive('get')->with('passport.private_key')->andReturn(null);
45+
});
46+
47+
$provider = new PassportServiceProvider(
48+
Mockery::mock(App::class, ['make' => $config])
49+
);
50+
51+
// Call protected makeCryptKey method
52+
$cryptKey = (function () {
53+
return $this->makeCryptKey('private');
54+
})->call($provider);
55+
56+
$this->assertSame(
57+
"-----BEGIN RSA PRIVATE KEY-----\ndisk\n-----END RSA PRIVATE KEY-----",
58+
file_get_contents($cryptKey->getKeyPath())
59+
);
60+
61+
@unlink(__DIR__.'/files/oauth-private.key');
62+
}
63+
}

0 commit comments

Comments
 (0)