Skip to content

Commit

Permalink
Add tests for making crypto keys
Browse files Browse the repository at this point in the history
  • Loading branch information
reinink committed Apr 5, 2018
1 parent 01c1be3 commit bb211a9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/PassportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use Illuminate\Auth\Events\Logout;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\ServiceProvider;
use Laravel\Passport\Guards\TokenGuard;
use League\OAuth2\Server\CryptKey;
use League\OAuth2\Server\ResourceServer;
use Illuminate\Config\Repository as Config;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Grant\AuthCodeGrant;
use League\OAuth2\Server\Grant\ImplicitGrant;
Expand Down Expand Up @@ -231,8 +231,11 @@ protected function registerResourceServer()
*/
protected function makeCryptKey($type)
{
$key = Config::get('passport.'.$type.'_key') ?? 'file://'.Passport::keyPath('oauth-'.$type.'.key');
$key = str_replace('\\n', "\n", $key);
$key = str_replace('\\n', "\n", $this->app->make(Config::class)->get('passport.'.$type.'_key'));

if (!$key) {
$key = 'file://'.Passport::keyPath('oauth-'.$type.'.key');
}

return new CryptKey($key, null, false);
}
Expand Down
63 changes: 63 additions & 0 deletions tests/PassportServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

use Laravel\Passport\Passport;
use PHPUnit\Framework\TestCase;
use Illuminate\Config\Repository as Config;
use Laravel\Passport\PassportServiceProvider;
use Illuminate\Contracts\Foundation\Application as App;

class PassportServiceProviderTest extends TestCase
{
public function test_can_use_crypto_keys_from_config()
{
$config = Mockery::mock(Config::class, function ($config) {
$config->shouldReceive('get')
->with('passport.private_key')
->andReturn('-----BEGIN RSA PRIVATE KEY-----\nconfig\n-----END RSA PRIVATE KEY-----');
});

$provider = new PassportServiceProvider(
Mockery::mock(App::class, ['make' => $config])
);

// Call protected makeCryptKey method
$cryptKey = (function () {
return $this->makeCryptKey('private');
})->call($provider);

$this->assertSame(
"-----BEGIN RSA PRIVATE KEY-----\nconfig\n-----END RSA PRIVATE KEY-----",
file_get_contents($cryptKey->getKeyPath())
);
}

public function test_can_use_crypto_keys_from_local_disk()
{
Passport::loadKeysFrom(__DIR__.'/files');

file_put_contents(
__DIR__.'/files/oauth-private.key',
"-----BEGIN RSA PRIVATE KEY-----\ndisk\n-----END RSA PRIVATE KEY-----"
);

$config = Mockery::mock(Config::class, function ($config) {
$config->shouldReceive('get')->with('passport.private_key')->andReturn(null);
});

$provider = new PassportServiceProvider(
Mockery::mock(App::class, ['make' => $config])
);

// Call protected makeCryptKey method
$cryptKey = (function () {
return $this->makeCryptKey('private');
})->call($provider);

$this->assertSame(
"-----BEGIN RSA PRIVATE KEY-----\ndisk\n-----END RSA PRIVATE KEY-----",
file_get_contents($cryptKey->getKeyPath())
);

@unlink(__DIR__.'/files/oauth-private.key');
}
}

0 comments on commit bb211a9

Please sign in to comment.