Skip to content

Commit

Permalink
Fix sealbox binding use correct config key (#26)
Browse files Browse the repository at this point in the history
In the provider an incorrect config key was used for the recipientPubKey of the sealbox. Fixed it and added test.
  • Loading branch information
ricklambrechts authored Feb 16, 2023
1 parent 3ea975d commit 0da5533
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/CryptoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function register(): void
$this->app->singleton(SealboxCryptoInterface::class, function () {
return Factory::createSealboxCryptoService(
privKey: config('crypto.sealbox.private_key'),
recipientPubKey: config('crypto.sealbox.recipient_pub_key'),
recipientPubKey: config('crypto.sealbox.recipient_public_key'),
);
});

Expand Down
25 changes: 25 additions & 0 deletions tests/ProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace MinVWS\Crypto\Laravel\Tests;

use MinVWS\Crypto\Laravel\SealboxCryptoInterface;
use MinVWS\Crypto\Laravel\Service\Sealbox\SealboxService;

class ProviderTest extends TestCase
{
public function testSealboxIsRegisteredCorrectly(): void
{
$this->assertInstanceOf(
SealboxService::class,
$this->app->make(SealboxCryptoInterface::class)
);
}

public function testDefaultSealboxCanEncryptAndDecrypt(): void
{
$sealbox = $this->app->make(SealboxCryptoInterface::class);

$encrypted = $sealbox->encrypt('foobar');
$this->assertEquals('foobar', $sealbox->decrypt($encrypted));
}
}
40 changes: 40 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace MinVWS\Crypto\Laravel\Tests;

use MinVWS\Crypto\Laravel\CryptoServiceProvider;
use Orchestra\Testbench\TestCase as OrchestraTestCase;

abstract class TestCase extends OrchestraTestCase
{
public function setUp(): void
{
parent::setUp();
// additional setup
}

protected function getPackageProviders($app): array
{
return [
CryptoServiceProvider::class,
];
}

protected function getEnvironmentSetUp($app): void
{
// Set sealbox keys
[$publicKey, $secretKey] = $this->generateSodiumKeys();
config()->set('crypto.sealbox.recipient_public_key', $publicKey);
config()->set('crypto.sealbox.private_key', $secretKey);
}

protected function generateSodiumKeys(): array
{
$keypair = sodium_crypto_box_keypair();

return [
base64_encode(sodium_crypto_box_publickey($keypair)),
base64_encode(sodium_crypto_box_secretkey($keypair)),
];
}
}

0 comments on commit 0da5533

Please sign in to comment.