Completed
Push — develop ( 686594...b5844e )
by Florent
03:11
created

testEd25519SignAndVerifyAlgorithm()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 26
rs 8.8571
cc 2
eloc 17
nc 2
nop 0
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
use Base64Url\Base64Url;
13
use Jose\Algorithm\Signature\Ed25519;
14
use Jose\Factory\JWSFactory;
15
use Jose\Factory\VerifierFactory;
16
use Jose\Loader;
17
use Jose\Object\JWK;
18
use Jose\Object\JWSInterface;
19
use Jose\Test\TestCase;
20
21
/**
22
 * Class Ed25519SignatureTest.
23
 *
24
 * @group Ed25519
25
 * @group Unit
26
 */
27
class Ed25519SignatureTest extends TestCase
28
{
29
    /**
30
     * @see https://tools.ietf.org/html/draft-ietf-jose-cfrg-curves-00#appendix-A.5
31
     */
32
    public function testEd25519VerifyAlgorithm()
33
    {
34
        if (!function_exists('ed25519_sign')) {
35
            $this->markTestSkipped('Ed25519 extension not available');
36
        }
37
38
        $key = new JWK([
39
            'kty' => 'OKP',
40
            'crv' => 'Ed25519',
41
            'd'   => 'nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A',
42
            'x'   => '11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo',
43
        ]);
44
45
        $ed25519 = new Ed25519();
46
        $input = 'eyJhbGciOiJFZDI1NTE5In0.RXhhbXBsZSBvZiBFZDI1NTE5IHNpZ25pbmc';
47
        $signature = Base64Url::decode('UxhIYLHGg39NVCLpQAVD_UcfOmnGSCzLFZoXYkLiIbFccmOb_qObsgjzLKsfJw-4NlccUgvYrEHrRbNV0HcZAQ');
48
49
        $ed25519->verify($key, $input, $signature);
50
    }
51
52
    /**
53
     * @see https://tools.ietf.org/html/draft-ietf-jose-cfrg-curves-00#appendix-A.5
54
     */
55
    public function testEd25519SignAndVerifyAlgorithm()
56
    {
57
        if (!function_exists('ed25519_sign')) {
58
            $this->markTestSkipped('Ed25519 extension not available');
59
        }
60
61
        $key = new JWK([
62
            'kty' => 'OKP',
63
            'crv' => 'Ed25519',
64
            'd'   => 'nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A',
65
            'x'   => '11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo',
66
        ]);
67
68
        $header = ['alg' => 'Ed25519'];
69
        $input = Base64Url::decode('RXhhbXBsZSBvZiBFZDI1NTE5IHNpZ25pbmc');
70
71
        $jws = JWSFactory::createJWSToCompactJSON($input, $key, $header);
72
73
        $this->assertEquals('eyJhbGciOiJFZDI1NTE5In0.RXhhbXBsZSBvZiBFZDI1NTE5IHNpZ25pbmc.UxhIYLHGg39NVCLpQAVD_UcfOmnGSCzLFZoXYkLiIbFccmOb_qObsgjzLKsfJw-4NlccUgvYrEHrRbNV0HcZAQ', $jws);
74
        $loaded = Loader::load($jws);
75
        $verifier = VerifierFactory::createVerifier(['Ed25519']);
76
77
        $this->assertInstanceOf(JWSInterface::class, $loaded);
78
        $this->assertEquals(1, $loaded->countSignatures());
0 ignored issues
show
Bug introduced by
The method countSignatures does only exist in Jose\Object\JWSInterface, but not in Jose\Object\JWEInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
79
        $verifier->verifyWithKey($loaded, $key);
0 ignored issues
show
Bug introduced by
It seems like $loaded defined by \Jose\Loader::load($jws) on line 74 can also be of type object<Jose\Object\JWEInterface>; however, Jose\Verifier::verifyWithKey() does only seem to accept object<Jose\Object\JWSInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
80
    }
81
}
82