Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set encryption keys from config #683

Merged
merged 5 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"illuminate/encryption": "~5.6",
"illuminate/http": "~5.6",
"illuminate/support": "~5.6",
"league/oauth2-server": "^6.0",
"league/oauth2-server": "^7.0",
"phpseclib/phpseclib": "^2.0",
"symfony/psr-http-message-bridge": "~1.0",
"zendframework/zend-diactoros": "~1.0"
Expand Down
20 changes: 20 additions & 0 deletions config/passport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Encryption Keys
|--------------------------------------------------------------------------
|
| Passport uses encryption keys when generating secure access tokens.
| By default these keys are stored as local files, but can also be
| set using the following environment variables.
|
*/

'private_key' => env('PASSPORT_PRIVATE_KEY'),

'public_key' => env('PASSPORT_PUBLIC_KEY'),

];
2 changes: 1 addition & 1 deletion src/Bridge/ClientRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(ClientModelRepository $clients)
/**
* {@inheritdoc}
*/
public function getClientEntity($clientIdentifier, $grantType,
public function getClientEntity($clientIdentifier, $grantType = null,
$clientSecret = null, $mustValidateSecret = true)
{
// First, we will verify that the client exists and is authorized to create personal
Expand Down
35 changes: 27 additions & 8 deletions src/PassportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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 @@ -82,6 +83,8 @@ public function register()
$this->registerResourceServer();

$this->registerGuard();

$this->offerPublishing();
}

/**
Expand Down Expand Up @@ -200,7 +203,7 @@ public function makeAuthorizationServer()
$this->app->make(Bridge\ClientRepository::class),
$this->app->make(Bridge\AccessTokenRepository::class),
$this->app->make(Bridge\ScopeRepository::class),
$this->makeCryptKey('oauth-private.key'),
$this->makeCryptKey('private'),
app('encrypter')->getKey()
);
}
Expand All @@ -215,7 +218,7 @@ protected function registerResourceServer()
$this->app->singleton(ResourceServer::class, function () {
return new ResourceServer(
$this->app->make(Bridge\AccessTokenRepository::class),
$this->makeCryptKey('oauth-public.key')
$this->makeCryptKey('public')
);
});
}
Expand All @@ -226,13 +229,15 @@ protected function registerResourceServer()
* @param string $key
* @return \League\OAuth2\Server\CryptKey
*/
protected function makeCryptKey($key)
protected function makeCryptKey($type)
{
return new CryptKey(
'file://'.Passport::keyPath($key),
null,
false
);
$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 Expand Up @@ -281,4 +286,18 @@ protected function deleteCookieOnLogout()
}
});
}

/**
* Setup the resource publishing groups for Passport.
*
* @return void
*/
protected function offerPublishing()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/passport.php' => config_path('passport.php'),
], 'passport-config');
}
}
}
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');
}
}