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

MultipleSignaturesTest::testMultipleSignatures()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 104
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 104
rs 8.2857
cc 1
eloc 54
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
namespace Jose\Test\RFC7520;
13
14
use Jose\Factory\JWSFactory;
15
use Jose\Factory\SignerFactory;
16
use Jose\Factory\VerifierFactory;
17
use Jose\Loader;
18
use Jose\Object\JWK;
19
20
/**
21
 * @see https://tools.ietf.org/html/rfc7520#section-4.8
22
 *
23
 * @group RFC7520
24
 */
25
class MultipleSignaturesTest extends \PHPUnit_Framework_TestCase
26
{
27
    public function testMultipleSignatures()
28
    {
29
        /*
30
         * Payload,
31
         * @see https://tools.ietf.org/html/rfc7520#section-4.8.1
32
         */
33
        $payload = "It\xe2\x80\x99s a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there\xe2\x80\x99s no knowing where you might be swept off to.";
34
        $jws = JWSFactory::createJWS($payload);
35
36
        $rsa_private_key = new JWK([
37
            'kty' => 'RSA',
38
            'kid' => '[email protected]',
39
            'use' => 'sig',
40
            'n'   => 'n4EPtAOCc9AlkeQHPzHStgAbgs7bTZLwUBZdR8_KuKPEHLd4rHVTeT-O-XV2jRojdNhxJWTDvNd7nqQ0VEiZQHz_AJmSCpMaJMRBSFKrKb2wqVwGU_NsYOYL-QtiWN2lbzcEe6XC0dApr5ydQLrHqkHHig3RBordaZ6Aj-oBHqFEHYpPe7Tpe-OfVfHd1E6cS6M1FZcD1NNLYD5lFHpPI9bTwJlsde3uhGqC0ZCuEHg8lhzwOHrtIQbS0FVbb9k3-tVTU4fg_3L_vniUFAKwuCLqKnS2BYwdq_mzSnbLY7h_qixoR7jig3__kRhuaxwUkRz5iaiQkqgc5gHdrNP5zw',
41
            'e'   => 'AQAB',
42
            'd'   => 'bWUC9B-EFRIo8kpGfh0ZuyGPvMNKvYWNtB_ikiH9k20eT-O1q_I78eiZkpXxXQ0UTEs2LsNRS-8uJbvQ-A1irkwMSMkK1J3XTGgdrhCku9gRldY7sNA_AKZGh-Q661_42rINLRCe8W-nZ34ui_qOfkLnK9QWDDqpaIsA-bMwWWSDFu2MUBYwkHTMEzLYGqOe04noqeq1hExBTHBOBdkMXiuFhUq1BU6l-DqEiWxqg82sXt2h-LMnT3046AOYJoRioz75tSUQfGCshWTBnP5uDjd18kKhyv07lhfSJdrPdM5Plyl21hsFf4L_mHCuoFau7gdsPfHPxxjVOcOpBrQzwQ',
43
            'p'   => '3Slxg_DwTXJcb6095RoXygQCAZ5RnAvZlno1yhHtnUex_fp7AZ_9nRaO7HX_-SFfGQeutao2TDjDAWU4Vupk8rw9JR0AzZ0N2fvuIAmr_WCsmGpeNqQnev1T7IyEsnh8UMt-n5CafhkikzhEsrmndH6LxOrvRJlsPp6Zv8bUq0k',
44
            'q'   => 'uKE2dh-cTf6ERF4k4e_jy78GfPYUIaUyoSSJuBzp3Cubk3OCqs6grT8bR_cu0Dm1MZwWmtdqDyI95HrUeq3MP15vMMON8lHTeZu2lmKvwqW7anV5UzhM1iZ7z4yMkuUwFWoBvyY898EXvRD-hdqRxHlSqAZ192zB3pVFJ0s7pFc',
45
            'dp'  => 'B8PVvXkvJrj2L-GYQ7v3y9r6Kw5g9SahXBwsWUzp19TVlgI-YV85q1NIb1rxQtD-IsXXR3-TanevuRPRt5OBOdiMGQp8pbt26gljYfKU_E9xn-RULHz0-ed9E9gXLKD4VGngpz-PfQ_q29pk5xWHoJp009Qf1HvChixRX59ehik',
46
            'dq'  => 'CLDmDGduhylc9o7r84rEUVn7pzQ6PF83Y-iBZx5NT-TpnOZKF1pErAMVeKzFEl41DlHHqqBLSM0W1sOFbwTxYWZDm6sI6og5iTbwQGIC3gnJKbi_7k_vJgGHwHxgPaX2PnvP-zyEkDERuf-ry4c_Z11Cq9AqC2yeL6kdKT1cYF8',
47
            'qi'  => '3PiqvXQN0zwMeE-sBvZgi289XP9XCQF3VWqPzMKnIgQp7_Tugo6-NZBKCQsMf3HaEGBjTVJs_jcK8-TRXvaKe-7ZMaQj8VfBdYkssbu0NKDDhjJ-GtiseaDVWt7dcH0cfwxgFUHpQh7FoCrjFJ6h6ZEpMF6xmujs4qMpPz8aaI4',
48
        ]);
49
50
        /*
51
         * Header
52
         * @see https://tools.ietf.org/html/rfc7520#section-4.8.2
53
         */
54
        $jws = $jws->addSignature(
55
            $rsa_private_key,
56
            [
57
                'alg' => 'RS256',
58
            ],
59
            [
60
                'kid' => '[email protected]',
61
            ]
62
        );
63
64
        $ecdsa_private_key = new JWK([
65
            'kty' => 'EC',
66
            'kid' => '[email protected]',
67
            'use' => 'sig',
68
            'crv' => 'P-521',
69
            'x'   => 'AHKZLLOsCOzz5cY97ewNUajB957y-C-U88c3v13nmGZx6sYl_oJXu9A5RkTKqjqvjyekWF-7ytDyRXYgCF5cj0Kt',
70
            'y'   => 'AdymlHvOiLxXkEhayXQnNCvDX4h9htZaCJN34kfmC6pV5OhQHiraVySsUdaQkAgDPrwQrJmbnX9cwlGfP-HqHZR1',
71
            'd'   => 'AAhRON2r9cqXX1hg-RoI6R1tX5p2rUAYdmpHZoC1XNM56KtscrX6zbKipQrCW9CGZH3T4ubpnoTKLDYJ_fF3_rJt',
72
        ]);
73
74
        /*
75
         * Header
76
         * @see https://tools.ietf.org/html/rfc7520#section-4.8.3
77
         */
78
        $jws = $jws->addSignature(
79
            $ecdsa_private_key,
80
            [],
81
            [
82
                'alg' => 'ES512',
83
                'kid' => '[email protected]',
84
            ]
85
        );
86
87
        $symmetric_key = new JWK([
88
            'kty' => 'oct',
89
            'kid' => '018c0ae5-4d9b-471b-bfd6-eef314bc7037',
90
            'use' => 'sig',
91
            'alg' => 'HS256',
92
            'k'   => 'hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg',
93
        ]);
94
95
        /*
96
         * Header
97
         * @see https://tools.ietf.org/html/rfc7520#section-4.8.4
98
         */
99
        $jws = $jws->addSignature(
100
            $symmetric_key,
101
            [
102
                'alg' => 'HS256',
103
                'kid' => '018c0ae5-4d9b-471b-bfd6-eef314bc7037',
104
            ]
105
        );
106
107
        $signer = SignerFactory::createSigner(['RS256', 'ES512', 'HS256']);
108
        $signer->sign($jws);
109
110
        $this->assertEquals(3, $jws->countSignatures());
111
112
        $verifer = VerifierFactory::createVerifier(['RS256', 'ES512', 'HS256']);
113
114
        $verifer->verifyWithKey($jws, $rsa_private_key);
115
        $verifer->verifyWithKey($jws, $ecdsa_private_key);
116
        $verifer->verifyWithKey($jws, $symmetric_key);
117
118
        /*
119
         * @see https://tools.ietf.org/html/rfc7520#section-4.8.5
120
         */
121
        $expected_json = '{"payload":"SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmUgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4","signatures":[{"protected":"eyJhbGciOiJSUzI1NiJ9","header":{"kid":"[email protected]"},"signature":"MIsjqtVlOpa71KE-Mss8_Nq2YH4FGhiocsqrgi5NvyG53uoimic1tcMdSg-qptrzZc7CG6Svw2Y13TDIqHzTUrL_lR2ZFcryNFiHkSw129EghGpwkpxaTn_THJTCglNbADko1MZBCdwzJxwqZc-1RlpO2HibUYyXSwO97BSe0_evZKdjvvKSgsIqjytKSeAMbhMBdMma622_BG5t4sdbuCHtFjp9iJmkio47AIwqkZV1aIZsv33uPUqBBCXbYoQJwt7mxPftHmNlGoOSMxR_3thmXTCm4US-xiNOyhbm8afKK64jU6_TPtQHiJeQJxz9G3Tx-083B745_AfYOnlC9w"},{"header":{"alg":"ES512","kid":"[email protected]"},"signature":"ARcVLnaJJaUWG8fG-8t5BREVAuTY8n8YHjwDO1muhcdCoFZFFjfISu0Cdkn9Ybdlmi54ho0x924DUz8sK7ZXkhc7AFM8ObLfTvNCrqcI3Jkl2U5IX3utNhODH6v7xgy1Qahsn0fyb4zSAkje8bAWz4vIfj5pCMYxxm4fgV3q7ZYhm5eD"},{"protected":"eyJhbGciOiJIUzI1NiIsImtpZCI6IjAxOGMwYWU1LTRkOWItNDcxYi1iZmQ2LWVlZjMxNGJjNzAzNyJ9","signature":"s0h6KThzkfBBBkLspW1h84VsJZFTsPPqMDA7g1Md7p0"}]}';
122
123
        $loaded_json = Loader::load($expected_json);
124
125
        $this->assertEquals(3, $loaded_json->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...
126
127
        $verifer->verifyWithKey($loaded_json, $rsa_private_key);
0 ignored issues
show
Bug introduced by
It seems like $loaded_json defined by \Jose\Loader::load($expected_json) on line 123 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...
128
        $verifer->verifyWithKey($loaded_json, $ecdsa_private_key);
0 ignored issues
show
Bug introduced by
It seems like $loaded_json defined by \Jose\Loader::load($expected_json) on line 123 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...
129
        $verifer->verifyWithKey($loaded_json, $symmetric_key);
0 ignored issues
show
Bug introduced by
It seems like $loaded_json defined by \Jose\Loader::load($expected_json) on line 123 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...
130
    }
131
}
132