From e1ce653ad9a37046275c67475912131078b5081c Mon Sep 17 00:00:00 2001 From: Sernin van de Krol Date: Fri, 18 May 2018 10:11:22 +0200 Subject: [PATCH 1/8] Pass $default to underlying function --- src/helpers.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index 5cd481d..2cbe265 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -10,9 +10,9 @@ function credentials(string $key, $default = null) { $credentials = app(Credentials::class); $credentials->load($filename); - return $credentials->get($key); + return $credentials->get($key, $default); } catch (ReflectionException $e) { return Credentials::CONFIG_PREFIX.$key; } } -} \ No newline at end of file +} From 38afaa8adb481584364d899797060897c06c8ee3 Mon Sep 17 00:00:00 2001 From: Sernin van de Krol Date: Fri, 18 May 2018 10:12:31 +0200 Subject: [PATCH 2/8] Remove useless break statements --- src/Exceptions/InvalidJSON.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Exceptions/InvalidJSON.php b/src/Exceptions/InvalidJSON.php index 56e2311..5018714 100644 --- a/src/Exceptions/InvalidJSON.php +++ b/src/Exceptions/InvalidJSON.php @@ -16,22 +16,16 @@ private static function getErrorMessage($error) switch ($error) { case JSON_ERROR_DEPTH: return ' - Maximum stack depth exceeded'; - break; case JSON_ERROR_STATE_MISMATCH: return ' - Underflow or the modes mismatch'; - break; case JSON_ERROR_CTRL_CHAR: return ' - Unexpected control character found'; - break; case JSON_ERROR_SYNTAX: return ' - Syntax error, malformed JSON'; - break; case JSON_ERROR_UTF8: return ' - Malformed UTF-8 characters, possibly incorrectly encoded'; - break; default: return ' - Unknown error'; - break; } } -} \ No newline at end of file +} From fb418b98eeb7e7d5ba0cd8394263c9497d977357 Mon Sep 17 00:00:00 2001 From: Sernin van de Krol Date: Fri, 18 May 2018 10:17:31 +0200 Subject: [PATCH 3/8] Add test for default parameter of helper function --- tests/CredentialTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/CredentialTest.php b/tests/CredentialTest.php index c963b09..e4d0d1a 100644 --- a/tests/CredentialTest.php +++ b/tests/CredentialTest.php @@ -124,6 +124,22 @@ public function it_can_use_the_helper_function() $this->assertSame('my-secret-value', credentials('key')); } + /** @test */ + public function it_can_give_a_default_to_the_helper_function() + { + $this->app['config']->set('credentials.file', __DIR__ . '/temp/credentials.php.enc'); + + $data = [ + 'key' => 'my-secret-value' + ]; + + $credentials = app(Credentials::class); + + $credentials->store($data, __DIR__ . '/temp/credentials.php.enc'); + + $this->assertSame('my-fallback-value', credentials('wrong-key', 'my-fallback-value')); + } + /** @test */ public function it_replaces_credential_strings_in_the_configuration_files() { From b7419b6b86fb4664c29e1d61f3d50a9e62dfee39 Mon Sep 17 00:00:00 2001 From: Bertel Torp Date: Tue, 29 May 2018 15:34:57 +0200 Subject: [PATCH 4/8] Introduce the optional fallback parameter --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b980339..2e9505d 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,12 @@ Here's how you can access your stored credentials. In this example we're retriev $credential = credentials('api-password'); ``` +You can also specify a fallback value to be used if the credential for the specified key cannot be decrypted: + +```php +$credential = credentials('my-production-token', 'my-fallback-value'); +``` + With the built-in edit command, you can easily edit your existing credentials. They will be automatically encrypted after saving your changes. ```bash @@ -28,7 +34,6 @@ php artisan credentials:edit ``` ![Credentials Demo](https://beyondco.de/github/credentials.gif) - ## Installation You can install the package via composer: From 3a2204bc68619076e128e66a388bc8dbeac8719c Mon Sep 17 00:00:00 2001 From: Lucas Michot Date: Sun, 19 Aug 2018 18:02:44 +0200 Subject: [PATCH 5/8] Remove useless .gitkeep file. --- config/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 config/.gitkeep diff --git a/config/.gitkeep b/config/.gitkeep deleted file mode 100644 index e69de29..0000000 From e6d7d1d9795b299051fb0c4b154f6a13a206464b Mon Sep 17 00:00:00 2001 From: Lucas Michot Date: Sun, 19 Aug 2018 18:10:51 +0200 Subject: [PATCH 6/8] Set all docblocks. --- src/Credentials.php | 22 +++++++++++++++++++--- src/CredentialsServiceProvider.php | 11 ++++++++++- src/EditCredentialsCommand.php | 9 +++++++-- src/Exceptions/InvalidJSON.php | 12 ++++++++++++ src/helpers.php | 20 ++++++++++++++------ 5 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/Credentials.php b/src/Credentials.php index 7338bba..5de1c27 100755 --- a/src/Credentials.php +++ b/src/Credentials.php @@ -10,15 +10,25 @@ class Credentials { const CONFIG_PREFIX = '___credentials_'; - /** @var Encrypter */ + /** + * The encrypter. + * + * @var \Illuminate\Contracts\Encryption\Encrypter + */ private $encrypter; - /** @var array */ + /** + * The decrpyted values array. + * + * @var array + */ private $decrypted; /** * Create a new Credentials Instance. - * @param Encrypter $encrypter + * + * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter + * @return void */ public function __construct(Encrypter $encrypter) { @@ -26,6 +36,8 @@ public function __construct(Encrypter $encrypter) } /** + * Load the file. + * * @param string $filename * @return array */ @@ -33,6 +45,7 @@ public function load(string $filename) { if (!file_exists($filename)) { $this->decrypted = []; + return $this->decrypted; } @@ -48,6 +61,7 @@ public function load(string $filename) * * @param array $data * @param string $filename + * @return void */ public function store(array $data, string $filename) { @@ -59,6 +73,8 @@ public function store(array $data, string $filename) } /** + * Get an encrypter value. + * * @param string $key * @param null $default * @return mixed diff --git a/src/CredentialsServiceProvider.php b/src/CredentialsServiceProvider.php index f065cf7..3053ee8 100644 --- a/src/CredentialsServiceProvider.php +++ b/src/CredentialsServiceProvider.php @@ -8,9 +8,10 @@ class CredentialsServiceProvider extends ServiceProvider { - /** * Bootstrap the application services. + * + * @return void */ public function boot() { @@ -26,6 +27,11 @@ public function boot() } } + /** + * Fix the configuration. + * + * @return void + */ protected function fixConfig() { collect(array_dot(config()->all()))->filter(function ($item) { @@ -39,6 +45,8 @@ protected function fixConfig() /** * Register the application services. + * + * @return void */ public function register() { @@ -52,6 +60,7 @@ public function register() } $encrypter = new Encrypter($key, config('credentials.cipher')); + return new Credentials($encrypter); }); diff --git a/src/EditCredentialsCommand.php b/src/EditCredentialsCommand.php index a0a31ab..a603716 100644 --- a/src/EditCredentialsCommand.php +++ b/src/EditCredentialsCommand.php @@ -5,7 +5,6 @@ use Illuminate\Console\Command; use Symfony\Component\Process\Process; use BeyondCode\Credentials\Exceptions\InvalidJSON; -use Symfony\Component\Process\Exception\ProcessFailedException; class EditCredentialsCommand extends Command { @@ -23,6 +22,12 @@ class EditCredentialsCommand extends Command */ protected $description = 'Encrypt and edit existing credentials. They will be decrypted after saving.'; + /** + * The command handler. + * + * @param \BeyondCode\Credentials\Credentials $credentials + * @return void + */ public function handle(Credentials $credentials) { $filename = config('credentials.file'); @@ -51,4 +56,4 @@ public function handle(Credentials $credentials) $this->info('Successfully updated credentials.'); } -} \ No newline at end of file +} diff --git a/src/Exceptions/InvalidJSON.php b/src/Exceptions/InvalidJSON.php index 5018714..e90f826 100644 --- a/src/Exceptions/InvalidJSON.php +++ b/src/Exceptions/InvalidJSON.php @@ -6,11 +6,23 @@ class InvalidJSON extends Exception { + /** + * Create a new InvalidJson exception instance. + * + * @param int $error + * @return $this + */ public static function create($error) { return new static("Unable to parse credential JSON ".self::getErrorMessage($error)); } + /** + * Get the error message. + * + * @param int $error + * @return string + */ private static function getErrorMessage($error) { switch ($error) { diff --git a/src/helpers.php b/src/helpers.php index 2cbe265..6348a6b 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -2,17 +2,25 @@ use BeyondCode\Credentials\Credentials; -if (!function_exists('credentials')) { - function credentials(string $key, $default = null) { +if (! function_exists('credentials')) { + /** + * Get a an encrypted value. + * + * @param string $key + * @param null $default + * @return mixed + */ + function credentials(string $key, $default = null) + { $filename = config('credentials.file'); try { - $credentials = app(Credentials::class); - $credentials->load($filename); + $credentials = app(Credentials::class); + $credentials->load($filename); - return $credentials->get($key, $default); + return $credentials->get($key, $default); } catch (ReflectionException $e) { - return Credentials::CONFIG_PREFIX.$key; + return Credentials::CONFIG_PREFIX.$key; } } } From c34aa5ffcc17cea13c44595c5043f8b372df4312 Mon Sep 17 00:00:00 2001 From: Jason Maners Date: Fri, 14 Sep 2018 11:49:33 -0400 Subject: [PATCH 7/8] Update dependency for Laravel 5.7 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 355fcf6..cdc888e 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": "^7.1", - "illuminate/encryption": "5.6.*" + "illuminate/encryption": ">=5.6" }, "require-dev": { "phpunit/phpunit": "^7.0", From 86c95e0c0abba0ee84b7be042621756ded809c9d Mon Sep 17 00:00:00 2001 From: Benjamin Stout Date: Mon, 12 Aug 2019 14:45:40 -0700 Subject: [PATCH 8/8] Small typo fix --- src/Credentials.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Credentials.php b/src/Credentials.php index 5de1c27..02dc9db 100755 --- a/src/Credentials.php +++ b/src/Credentials.php @@ -18,7 +18,7 @@ class Credentials private $encrypter; /** - * The decrpyted values array. + * The decrypted values array. * * @var array */