Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:beyondcode/laravel-credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Sep 4, 2019
2 parents aa3f3c7 + a72fd42 commit e242e89
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 21 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ 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
php artisan credentials:edit
```
![Credentials Demo](https://beyondco.de/github/credentials.gif)


## Installation

You can install the package via composer:
Expand Down
Empty file removed config/.gitkeep
Empty file.
22 changes: 19 additions & 3 deletions src/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,42 @@ 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)
{
$this->encrypter = $encrypter;
}

/**
* Load the file.
*
* @param string $filename
* @return array
*/
public function load(string $filename)
{
if (!file_exists($filename)) {
$this->decrypted = [];

return $this->decrypted;
}

Expand All @@ -48,6 +61,7 @@ public function load(string $filename)
*
* @param array $data
* @param string $filename
* @return void
*/
public function store(array $data, string $filename)
{
Expand All @@ -59,6 +73,8 @@ public function store(array $data, string $filename)
}

/**
* Get an encrypter value.
*
* @param string $key
* @param null $default
* @return mixed
Expand Down
11 changes: 10 additions & 1 deletion src/CredentialsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

class CredentialsServiceProvider extends ServiceProvider
{

/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
Expand All @@ -27,6 +28,11 @@ public function boot()
}
}

/**
* Fix the configuration.
*
* @return void
*/
protected function fixConfig()
{
collect(Arr::dot(config()->all()))->filter(function ($item) {
Expand All @@ -40,6 +46,8 @@ protected function fixConfig()

/**
* Register the application services.
*
* @return void
*/
public function register()
{
Expand All @@ -53,6 +61,7 @@ public function register()
}

$encrypter = new Encrypter($key, config('credentials.cipher'));

return new Credentials($encrypter);
});

Expand Down
9 changes: 7 additions & 2 deletions src/EditCredentialsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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');
Expand Down Expand Up @@ -51,4 +56,4 @@ public function handle(Credentials $credentials)

$this->info('Successfully updated credentials.');
}
}
}
20 changes: 13 additions & 7 deletions src/Exceptions/InvalidJSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
22 changes: 15 additions & 7 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
16 changes: 16 additions & 0 deletions tests/CredentialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit e242e89

Please sign in to comment.