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

Commit e242e89

Browse files
committed
Merge branch 'master' of github.com:beyondcode/laravel-credentials
2 parents aa3f3c7 + a72fd42 commit e242e89

File tree

8 files changed

+86
-21
lines changed

8 files changed

+86
-21
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ Here's how you can access your stored credentials. In this example we're retriev
2121
$credential = credentials('api-password');
2222
```
2323

24+
You can also specify a fallback value to be used if the credential for the specified key cannot be decrypted:
25+
26+
```php
27+
$credential = credentials('my-production-token', 'my-fallback-value');
28+
```
29+
2430
With the built-in edit command, you can easily edit your existing credentials. They will be automatically encrypted after saving your changes.
2531

2632
```bash
2733
php artisan credentials:edit
2834
```
2935
![Credentials Demo](https://beyondco.de/github/credentials.gif)
3036

31-
3237
## Installation
3338

3439
You can install the package via composer:

config/.gitkeep

Whitespace-only changes.

src/Credentials.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,42 @@ class Credentials
1010
{
1111
const CONFIG_PREFIX = '___credentials_';
1212

13-
/** @var Encrypter */
13+
/**
14+
* The encrypter.
15+
*
16+
* @var \Illuminate\Contracts\Encryption\Encrypter
17+
*/
1418
private $encrypter;
1519

16-
/** @var array */
20+
/**
21+
* The decrypted values array.
22+
*
23+
* @var array
24+
*/
1725
private $decrypted;
1826

1927
/**
2028
* Create a new Credentials Instance.
21-
* @param Encrypter $encrypter
29+
*
30+
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
31+
* @return void
2232
*/
2333
public function __construct(Encrypter $encrypter)
2434
{
2535
$this->encrypter = $encrypter;
2636
}
2737

2838
/**
39+
* Load the file.
40+
*
2941
* @param string $filename
3042
* @return array
3143
*/
3244
public function load(string $filename)
3345
{
3446
if (!file_exists($filename)) {
3547
$this->decrypted = [];
48+
3649
return $this->decrypted;
3750
}
3851

@@ -48,6 +61,7 @@ public function load(string $filename)
4861
*
4962
* @param array $data
5063
* @param string $filename
64+
* @return void
5165
*/
5266
public function store(array $data, string $filename)
5367
{
@@ -59,6 +73,8 @@ public function store(array $data, string $filename)
5973
}
6074

6175
/**
76+
* Get an encrypter value.
77+
*
6278
* @param string $key
6379
* @param null $default
6480
* @return mixed

src/CredentialsServiceProvider.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
class CredentialsServiceProvider extends ServiceProvider
1111
{
12-
1312
/**
1413
* Bootstrap the application services.
14+
*
15+
* @return void
1516
*/
1617
public function boot()
1718
{
@@ -27,6 +28,11 @@ public function boot()
2728
}
2829
}
2930

31+
/**
32+
* Fix the configuration.
33+
*
34+
* @return void
35+
*/
3036
protected function fixConfig()
3137
{
3238
collect(Arr::dot(config()->all()))->filter(function ($item) {
@@ -40,6 +46,8 @@ protected function fixConfig()
4046

4147
/**
4248
* Register the application services.
49+
*
50+
* @return void
4351
*/
4452
public function register()
4553
{
@@ -53,6 +61,7 @@ public function register()
5361
}
5462

5563
$encrypter = new Encrypter($key, config('credentials.cipher'));
64+
5665
return new Credentials($encrypter);
5766
});
5867

src/EditCredentialsCommand.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Console\Command;
66
use Symfony\Component\Process\Process;
77
use BeyondCode\Credentials\Exceptions\InvalidJSON;
8-
use Symfony\Component\Process\Exception\ProcessFailedException;
98

109
class EditCredentialsCommand extends Command
1110
{
@@ -23,6 +22,12 @@ class EditCredentialsCommand extends Command
2322
*/
2423
protected $description = 'Encrypt and edit existing credentials. They will be decrypted after saving.';
2524

25+
/**
26+
* The command handler.
27+
*
28+
* @param \BeyondCode\Credentials\Credentials $credentials
29+
* @return void
30+
*/
2631
public function handle(Credentials $credentials)
2732
{
2833
$filename = config('credentials.file');
@@ -51,4 +56,4 @@ public function handle(Credentials $credentials)
5156

5257
$this->info('Successfully updated credentials.');
5358
}
54-
}
59+
}

src/Exceptions/InvalidJSON.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,38 @@
66

77
class InvalidJSON extends Exception
88
{
9+
/**
10+
* Create a new InvalidJson exception instance.
11+
*
12+
* @param int $error
13+
* @return $this
14+
*/
915
public static function create($error)
1016
{
1117
return new static("Unable to parse credential JSON ".self::getErrorMessage($error));
1218
}
1319

20+
/**
21+
* Get the error message.
22+
*
23+
* @param int $error
24+
* @return string
25+
*/
1426
private static function getErrorMessage($error)
1527
{
1628
switch ($error) {
1729
case JSON_ERROR_DEPTH:
1830
return ' - Maximum stack depth exceeded';
19-
break;
2031
case JSON_ERROR_STATE_MISMATCH:
2132
return ' - Underflow or the modes mismatch';
22-
break;
2333
case JSON_ERROR_CTRL_CHAR:
2434
return ' - Unexpected control character found';
25-
break;
2635
case JSON_ERROR_SYNTAX:
2736
return ' - Syntax error, malformed JSON';
28-
break;
2937
case JSON_ERROR_UTF8:
3038
return ' - Malformed UTF-8 characters, possibly incorrectly encoded';
31-
break;
3239
default:
3340
return ' - Unknown error';
34-
break;
3541
}
3642
}
37-
}
43+
}

src/helpers.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@
22

33
use BeyondCode\Credentials\Credentials;
44

5-
if (!function_exists('credentials')) {
6-
function credentials(string $key, $default = null) {
5+
if (! function_exists('credentials')) {
6+
/**
7+
* Get a an encrypted value.
8+
*
9+
* @param string $key
10+
* @param null $default
11+
* @return mixed
12+
*/
13+
function credentials(string $key, $default = null)
14+
{
715
$filename = config('credentials.file');
816

917
try {
10-
$credentials = app(Credentials::class);
11-
$credentials->load($filename);
18+
$credentials = app(Credentials::class);
19+
$credentials->load($filename);
1220

13-
return $credentials->get($key);
21+
return $credentials->get($key, $default);
1422
} catch (ReflectionException $e) {
15-
return Credentials::CONFIG_PREFIX.$key;
23+
return Credentials::CONFIG_PREFIX.$key;
1624
}
1725
}
18-
}
26+
}

tests/CredentialTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ public function it_can_use_the_helper_function()
125125
$this->assertSame('my-secret-value', credentials('key'));
126126
}
127127

128+
/** @test */
129+
public function it_can_give_a_default_to_the_helper_function()
130+
{
131+
$this->app['config']->set('credentials.file', __DIR__ . '/temp/credentials.php.enc');
132+
133+
$data = [
134+
'key' => 'my-secret-value'
135+
];
136+
137+
$credentials = app(Credentials::class);
138+
139+
$credentials->store($data, __DIR__ . '/temp/credentials.php.enc');
140+
141+
$this->assertSame('my-fallback-value', credentials('wrong-key', 'my-fallback-value'));
142+
}
143+
128144
/** @test */
129145
public function it_replaces_credential_strings_in_the_configuration_files()
130146
{

0 commit comments

Comments
 (0)