-
Notifications
You must be signed in to change notification settings - Fork 1
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 #16 from IlicMiljan/abstract-encoding-logic
Abstract Encoding Logic
- Loading branch information
Showing
11 changed files
with
364 additions
and
7 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace IlicMiljan\SecureProps\Encoder; | ||
|
||
use SensitiveParameter; | ||
|
||
class Base64Encoder implements Encoder | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function encode(#[SensitiveParameter] string $value): string | ||
{ | ||
return base64_encode($value); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function decode(#[SensitiveParameter] string $value): string | ||
{ | ||
return base64_decode($value); | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace IlicMiljan\SecureProps\Encoder; | ||
|
||
use SensitiveParameter; | ||
|
||
/** | ||
* Encodes and decodes string values. | ||
* | ||
* This interface should be implemented by classes that provide mechanisms | ||
* for encoding and decoding string values. | ||
* | ||
* Implementations could, for example, apply base64 encoding/decoding, or any | ||
* other form of transformation that maintains the reversibility of the value. | ||
*/ | ||
interface Encoder | ||
{ | ||
/** | ||
* Encodes the provided string value. | ||
* | ||
* Takes a string as input and returns an encoded version of the string.f | ||
* | ||
* @param string $value The string value to encode. | ||
* @return string The encoded string. | ||
*/ | ||
public function encode(#[SensitiveParameter] string $value): string; | ||
|
||
/** | ||
* Decodes the provided encoded string value. | ||
* | ||
* Takes an encoded string as input and returns the original unencoded | ||
* version of the string. | ||
* | ||
* @param string $value The encoded string to decode. | ||
* @return string The original, unencoded string. | ||
*/ | ||
public function decode(#[SensitiveParameter] string $value): string; | ||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace IlicMiljan\SecureProps\Encoder; | ||
|
||
use SensitiveParameter; | ||
|
||
class NullEncoder implements Encoder | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function encode(#[SensitiveParameter] string $value): string | ||
{ | ||
return $value; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function decode(#[SensitiveParameter] string $value): string | ||
{ | ||
return $value; | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace IlicMiljan\SecureProps\Tests\Attribute; | ||
|
||
use IlicMiljan\SecureProps\Attribute\Encrypted; | ||
use PHPUnit\Framework\TestCase; | ||
use SensitiveParameter; | ||
|
||
class SensitiveParameterTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
if (PHP_VERSION_ID >= 80200) { | ||
$this->markTestSkipped( | ||
'All tests in this file have been skipped because they are do not apply to PHP 8.2 or higher.' | ||
); | ||
} | ||
} | ||
|
||
public function testCanBeCreated(): void | ||
{ | ||
$encrypted = new SensitiveParameter(); | ||
|
||
$this->assertInstanceOf(SensitiveParameter::class, $encrypted); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace IlicMiljan\SecureProps\Tests\Encoder; | ||
|
||
use IlicMiljan\SecureProps\Encoder\Base64Encoder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class Base64EncoderTest extends TestCase | ||
{ | ||
private Base64Encoder $encoder; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->encoder = new Base64Encoder(); | ||
} | ||
|
||
public function testEncode(): void | ||
{ | ||
$string = "Hello, World!"; | ||
$expectedEncodedString = base64_encode($string); | ||
|
||
$this->assertEquals( | ||
$expectedEncodedString, | ||
$this->encoder->encode($string), | ||
"The encoded string does not match the expected output." | ||
); | ||
} | ||
|
||
public function testDecode(): void | ||
{ | ||
$encodedString = "SGVsbG8sIFdvcmxkIQ=="; | ||
$expectedDecodedString = base64_decode($encodedString); | ||
|
||
$this->assertEquals( | ||
$expectedDecodedString, | ||
$this->encoder->decode($encodedString) | ||
); | ||
} | ||
|
||
public function testEncodeDecode(): void | ||
{ | ||
$originalString = "Test encode and decode!"; | ||
|
||
$encodedString = $this->encoder->encode($originalString); | ||
$decodedString = $this->encoder->decode($encodedString); | ||
|
||
$this->assertEquals( | ||
$originalString, | ||
$decodedString | ||
); | ||
} | ||
} |
Oops, something went wrong.