Skip to content

Commit

Permalink
Merge pull request #8 from RichardStyles/feature/Laravel_9_support
Browse files Browse the repository at this point in the history
Add Laravel 9 encrypter contract function
  • Loading branch information
RichardStyles authored Jun 4, 2022
2 parents 82b1cbd + 294c2c0 commit 349b24d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
}
],
"require": {
"php": "^7.3|^8.0",
"illuminate/support": "^8.12",
"illuminate/database": "^8.40",
"php": "^7.3|^8.0|^8.1",
"illuminate/support": "^8.12||^9",
"illuminate/database": "^8.40||^9",
"phpseclib/phpseclib": "~2.0"
},
"require-dev": {
Expand Down
20 changes: 15 additions & 5 deletions src/EloquentEncryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct()
Config::get('eloquent_encryption.handler', RsaKeyStorageHandler::class)
);

if(!$this->handler instanceof RsaKeyHandler){
if (!$this->handler instanceof RsaKeyHandler) {
throw new InvalidRsaKeyHandler;
}
}
Expand Down Expand Up @@ -98,7 +98,7 @@ public function encrypt($value, $serialize = true)
/**
* Encrypt a string without serialization.
*
* @param string $value
* @param string $value
* @return string
*
* @throws RSAKeyFileMissing
Expand All @@ -112,7 +112,7 @@ public function encryptString($value)
* Decrypt a value using the RSA key
*
* @param $value
* @param bool $unserialize
* @param bool $unserialize
* @return false|string|null
* @throws RSAKeyFileMissing
*/
Expand All @@ -131,7 +131,7 @@ public function decrypt($value, $unserialize = true)
/**
* Decrypt the given string without unserialization.
*
* @param string $payload
* @param string $payload
* @return string
*
* @throws RSAKeyFileMissing
Expand All @@ -143,8 +143,18 @@ public function decryptString($payload)

public function __call($name, $arguments)
{
if(method_exists($this->handler, $name)){
if (method_exists($this->handler, $name)) {
return $this->handler->{$name}($arguments);
}
}

/**
* Get the encryption key that the encrypter is currently using.
*
* @return string
*/
public function getKey()
{
return $this->handler->getPrivateKey();
}
}
6 changes: 6 additions & 0 deletions tests/Traits/WithRSAHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ private function makeKey($path, $key)

return $key;
}

protected function makeRawKey($path, $key, $contents)
{
Storage::put($path.$key, $contents);
return $key;
}
}
9 changes: 9 additions & 0 deletions tests/Unit/EloquentEncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ function an_invalid_rsa_key_handler_throws_exception()

$eloquent_encryption = new EloquentEncryption();
}

/** @test */
function get_key_returns_the_private_key()
{
$this->makePublicKey();
$this->makeRawKey('','eloquent_encryption','Super secret key');

$this->assertEquals('Super secret key', $this->eloquent_encryption->getKey());
}
}

class BadRsaKeyHandler
Expand Down

0 comments on commit 349b24d

Please sign in to comment.