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: diff --git a/config/.gitkeep b/config/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/Credentials.php b/src/Credentials.php index 7338bba..02dc9db 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 decrypted 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 919d049..2473f28 100644 --- a/src/CredentialsServiceProvider.php +++ b/src/CredentialsServiceProvider.php @@ -9,9 +9,10 @@ class CredentialsServiceProvider extends ServiceProvider { - /** * Bootstrap the application services. + * + * @return void */ public function boot() { @@ -27,6 +28,11 @@ public function boot() } } + /** + * Fix the configuration. + * + * @return void + */ protected function fixConfig() { collect(Arr::dot(config()->all()))->filter(function ($item) { @@ -40,6 +46,8 @@ protected function fixConfig() /** * Register the application services. + * + * @return void */ public function register() { @@ -53,6 +61,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 56e2311..e90f826 100644 --- a/src/Exceptions/InvalidJSON.php +++ b/src/Exceptions/InvalidJSON.php @@ -6,32 +6,38 @@ 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) { 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 +} diff --git a/src/helpers.php b/src/helpers.php index 5cd481d..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); + return $credentials->get($key, $default); } catch (ReflectionException $e) { - return Credentials::CONFIG_PREFIX.$key; + return Credentials::CONFIG_PREFIX.$key; } } -} \ No newline at end of file +} diff --git a/tests/CredentialTest.php b/tests/CredentialTest.php index 529bce7..69d8707 100644 --- a/tests/CredentialTest.php +++ b/tests/CredentialTest.php @@ -125,6 +125,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() {