-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from dotenv-org/add-tests
DotenvVault loads .env.vault files
- Loading branch information
Showing
13 changed files
with
478 additions
and
107 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" bootstrap="vendor/autoload.php" colors="true" failOnRisky="true" failOnWarning="false" processIsolation="false" stopOnError="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false"> | ||
<coverage/> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" bootstrap="vendor/autoload.php" colors="true" convertDeprecationsToExceptions="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" failOnRisky="true" failOnWarning="false" processIsolation="false" stopOnError="false" stopOnFailure="false" verbose="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="PHP DotenvVault Test Suite"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<source> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DotenvVault\Decrypter; | ||
|
||
use Exception; | ||
|
||
final class Decrypter implements DecrypterInterface | ||
{ | ||
/** | ||
* Decrypt encrypted content into a string | ||
* | ||
* @param string $content | ||
* @param string $keyStr | ||
* | ||
* @throws \Exception | ||
* | ||
* @return string | ||
*/ | ||
public function decrypt(string $encrypted, string $keyStr) | ||
{ | ||
if ($encrypted === null || !is_string($encrypted) || strlen($encrypted) < 1) { | ||
$msg = 'MISSING_CIPHERTEXT: It must be a non-empty string'; | ||
throw new Exception($msg); | ||
} | ||
|
||
// grab last 64 to permit keys like vlt_64 or custom_64 | ||
$last64 = substr($keyStr, -64); | ||
|
||
// must be 64 characters long | ||
if (strlen($last64) !== 64) { | ||
$msg = 'INVALID_DOTENV_KEY: It must be 64 characters long (or more)'; | ||
throw new Exception($msg); | ||
} | ||
|
||
// check key length is good INVALID_DOTENV_KEY: It must be 64 characters long (or more) | ||
$key = hex2bin($last64); | ||
|
||
// base64 decode | ||
$decoded = base64_decode($encrypted, true); | ||
|
||
// determine cipher and pull out nonce and tag | ||
$ciphertext = substr($decoded, 12, -16); | ||
$nonce = substr($decoded, 0, 12); | ||
$tag = substr($decoded, -16); | ||
|
||
try { | ||
$plaintext = openssl_decrypt($ciphertext, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $nonce, $tag); | ||
|
||
if ($plaintext === false) { | ||
$msg = 'DECRYPTION_FAILED: Please check your DOTENV_KEY'; | ||
throw new Exception($msg); | ||
} else { | ||
return $plaintext; | ||
} | ||
} catch (ExceptionType $e) { | ||
$msg = 'DECRYPTION_FAILED: Please check your DOTENV_KEY'; | ||
throw new Exception($msg); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DotenvVault\Decrypter; | ||
|
||
interface DecrypterInterface | ||
{ | ||
/** | ||
* Decrypt encrypted content into a string | ||
* | ||
* @param string $content | ||
* @param string $keyStr | ||
* | ||
* @throws \Exception | ||
* | ||
* | ||
* @return string | ||
*/ | ||
public function decrypt(string $encrypted, string $keyStr); | ||
} |
Oops, something went wrong.