Skip to content

Commit

Permalink
Validate used verification id (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner authored Sep 28, 2023
1 parent 6ec8e7e commit 45093db
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
'valid_verification_id' => 'The verification ID is not valid.',
'numeric' => 'The :attribute must be numeric.',
'collection_has_tokens' => "The collection doesn't have any tokens.",
'unused_verification_id' => 'The verification ID is already in use.',
];
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Enjin\Platform\GraphQL\Schemas\Primary\Traits\InPrimarySchema;
use Enjin\Platform\Interfaces\PlatformGraphQlMutation;
use Enjin\Platform\Interfaces\PlatformPublicGraphQlOperation;
use Enjin\Platform\Rules\UnusedVerificationId;
use Enjin\Platform\Rules\ValidHex;
use Enjin\Platform\Rules\ValidSubstrateAccount;
use Enjin\Platform\Services\Database\VerificationService;
Expand Down Expand Up @@ -49,7 +50,12 @@ public function args(): array
return [
'verificationId' => [
'type' => GraphQL::type('String!'),
'rules' => ['bail', 'filled', 'exists:verifications,verification_id'],
'rules' => [
'bail',
'filled',
'exists:verifications,verification_id',
new UnusedVerificationId(),
],
],
'signature' => [
'type' => GraphQL::type('String!'),
Expand Down
22 changes: 22 additions & 0 deletions src/Rules/UnusedVerificationId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Enjin\Platform\Rules;

use Closure;
use Enjin\Platform\Models\Wallet;
use Illuminate\Contracts\Validation\ValidationRule;

class UnusedVerificationId implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (Wallet::where('verification_id', $value)->exists()) {
$fail(__('enjin-platform::validation.unused_verification_id'));
}
}
}
18 changes: 18 additions & 0 deletions tests/Feature/GraphQL/Mutations/VerifyAccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Enjin\BlockchainTools\HexConverter;
use Enjin\Platform\Facades\Qr;
use Enjin\Platform\Models\Verification;
use Enjin\Platform\Models\Wallet;
use Enjin\Platform\Support\SS58Address;
use Enjin\Platform\Tests\Feature\GraphQL\TestCaseGraphQL;
use Enjin\Platform\Tests\Feature\GraphQL\Traits\HasHttp;
Expand Down Expand Up @@ -252,6 +253,23 @@ public function test_it_will_fail_with_wrong_ed25519_signature(): void
);
}

public function test_it_will_fail_with_used_verification_id(): void
{
Wallet::factory()->create(['verification_id' => $this->verification->verification_id]);
$data = app(Generator::class)->sr25519_signature($this->verification->code, isCode: true);

$response = $this->graphql($this->method, [
'verificationId' => $this->verification->verification_id,
'signature' => $data['signature'],
'account' => $data['address'],
], true);

$this->assertArraySubset(
['verificationId' => ['The verification ID is already in use.']],
$response['error']
);
}

public function test_it_will_fail_with_empty_verification_id(): void
{
$data = app(Generator::class)->sr25519_signature($this->verification->code, isCode: true);
Expand Down

0 comments on commit 45093db

Please sign in to comment.