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

FlattenedTest::testLoadFlattenedJWE()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 10
nc 1
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 Jose\Factory\DecrypterFactory;
13
use Jose\Loader;
14
use Jose\Object\JWEInterface;
15
use Jose\Object\JWSInterface;
16
use Jose\Test\TestCase;
17
18
/**
19
 * Class FlattenedTest.
20
 *
21
 * @group Functional
22
 */
23
class FlattenedTest extends TestCase
24
{
25
    /**
26
     * @see https://tools.ietf.org/html/rfc7516#appendix-A.5
27
     */
28
    public function testLoadFlattenedJWE()
29
    {
30
        $decrypter = DecrypterFactory::createDecrypter(['A128KW', 'A128CBC-HS256'], ['DEF']);
31
32
        $loaded = Loader::load('{"protected":"eyJlbmMiOiJBMTI4Q0JDLUhTMjU2In0","unprotected":{"jku":"https://server.example.com/keys.jwks"},"header":{"alg":"A128KW","kid":"7"},"encrypted_key":"6KB707dM9YTIgHtLvtgWQ8mKwboJW3of9locizkDTHzBC2IlrT1oOQ","iv":"AxY8DCtDaGlsbGljb3RoZQ","ciphertext":"KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY","tag":"Mz-VPPyU4RlcuYv1IwIvzw"}');
33
34
        $this->assertInstanceOf(JWEInterface::class, $loaded);
35
        $this->assertEquals('A128KW', $loaded->getRecipient(0)->getHeader('alg'));
0 ignored issues
show
Bug introduced by
The method getRecipient does only exist in Jose\Object\JWEInterface, but not in Jose\Object\JWSInterface.

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...
36
        $this->assertEquals('A128CBC-HS256', $loaded->getSharedProtectedHeader('enc'));
0 ignored issues
show
Bug introduced by
The method getSharedProtectedHeader does only exist in Jose\Object\JWEInterface, but not in Jose\Object\JWSInterface.

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...
37
        $this->assertNull($loaded->getPayload());
38
39
        $decrypter->decryptUsingKeySet($loaded, $this->getSymmetricKeySet(), $index);
0 ignored issues
show
Bug introduced by
It seems like $loaded defined by \Jose\Loader::load('{"pr...VPPyU4RlcuYv1IwIvzw"}') on line 32 can also be of type object<Jose\Object\JWSInterface>; however, Jose\Decrypter::decryptUsingKeySet() does only seem to accept object<Jose\Object\JWEInterface>, 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...
40
41
        $this->assertEquals(0, $index);
42
        $this->assertEquals('Live long and prosper.', $loaded->getPayload());
43
    }
44
45
    /**
46
     * @see https://tools.ietf.org/html/rfc7516#appendix-A.5
47
     */
48
    public function testLoadFlattenedJWS()
49
    {
50
        $loaded = Loader::load('{"payload":"eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ","protected":"eyJhbGciOiJFUzI1NiJ9","header":{"kid":"e9bc097a-ce51-4036-9562-d2ade882db0d"},"signature":"DtEhU3ljbEg8L38VWAfUAqOyKAM6-Xx-F4GawxaepmXFCgfTjDxw5djxLa8ISlSApmWQxfKTUJqPP3-Kg6NU1Q"}');
51
52
        $this->assertInstanceOf(JWSInterface::class, $loaded);
53
        $this->assertEquals('ES256', $loaded->getSignature(0)->getProtectedHeader('alg'));
0 ignored issues
show
Bug introduced by
The method getSignature 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...
54
        $this->assertEquals(['iss' => 'joe', 'exp' => 1300819380, 'http://example.com/is_root' => true], $loaded->getPayload());
55
    }
56
}
57