Completed
Pull Request — master (#7)
by John
07:24
created

AuthenticatorTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\JwtBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\JwtBundle\Tests\Authenticator;
9
10
use KleijnWeb\JwtBundle\Authenticator\Authenticator;
11
use KleijnWeb\JwtBundle\Authenticator\JwtKey;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
14
use Symfony\Component\Security\Core\User\User;
15
16
/**
17
 * @author John Kleijn <[email protected]>
18
 */
19
class AuthenticatorTest extends \PHPUnit_Framework_TestCase
20
{
21
    // @codingStandardsIgnoreStart
22
23
    /**
24
     * Created using jwt.io
25
     */
26
    const TEST_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtleU9uZSJ9.eyJwcm4iOiJqb2huIiwiaXNzIjoiaHR0cDovL2FwaS5zZXJ2ZXIxLmNvbS9vYXV0aDIvdG9rZW4ifQ._jXjAWMzwwG1v5N3ZOEUoLGSINtmwLsvQdfYkYAcWiY';
27
28
    const JKEY_CLASS = 'KleijnWeb\JwtBundle\Authenticator\JwtKey';
29
30
    /**
31
     * @var array
32
     */
33
    private static $keyConfig = [
34
        'keyOne' =>
35
            [
36
                'issuer' => 'http://api.server1.com/oauth2/token',
37
                'secret' => 'A Pre-Shared Key',
38
                'type'   => 'HS256',
39
            ],
40
        'keyTwo' =>
41
            [
42
                'issuer' => 'http://api.server2.com/oauth2/token',
43
                'type'   => 'RS256',
44
                'secret' => 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0F',
45
            ],
46
    ];
47
48
    // @codingStandardsIgnoreEnd
49
50
    /**
51
     * @var JwtKey[]
52
     */
53
    private $keys;
54
55
    protected function setUp()
56
    {
57
        $this->keys = new \ArrayObject;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ArrayObject() of type object<ArrayObject> is incompatible with the declared type array<integer,object<Kle...\Authenticator\JwtKey>> of property $keys.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
        foreach (self::$keyConfig as $keyId => $config) {
59
            $config['kid']      = $keyId;
60
            $this->keys[$keyId] = new JwtKey($config);
61
        }
62
    }
63
64
    /**
65
     * @test
66
     */
67
    public function getGetKeysUsingIndexesInConfig()
68
    {
69
        $authenticator = new Authenticator($this->keys);
0 ignored issues
show
Documentation introduced by
$this->keys is of type array<integer,object<Kle...\Authenticator\JwtKey>>, but the function expects a object<ArrayObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
71
        $this->assertInstanceOf(self::JKEY_CLASS, $authenticator->getKeyById('keyOne'));
72
        $this->assertInstanceOf(self::JKEY_CLASS, $authenticator->getKeyById('keyTwo'));
73
    }
74
75
    /**
76
     * @test
77
     */
78
    public function willGetSingleKeyWhenKeyIdIsNull()
79
    {
80
        $config = $this->keys;
81
        unset($config['keyTwo']);
82
83
        $authenticator = new Authenticator($config);
0 ignored issues
show
Documentation introduced by
$config is of type array<integer,object<Kle...\Authenticator\JwtKey>>, but the function expects a object<ArrayObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
85
        $this->assertInstanceOf(self::JKEY_CLASS, $authenticator->getKeyById(null));
86
    }
87
88
    /**
89
     * @test
90
     * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
91
     */
92
    public function willFailWhenTryingToGetKeyWithoutIdWhenThereAreMoreThanOne()
93
    {
94
        $authenticator = new Authenticator($this->keys);
0 ignored issues
show
Documentation introduced by
$this->keys is of type array<integer,object<Kle...\Authenticator\JwtKey>>, but the function expects a object<ArrayObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
96
        $this->assertInstanceOf(self::JKEY_CLASS, $authenticator->getKeyById(null));
97
    }
98
99
    /**
100
     * @test
101
     * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
102
     */
103
    public function willFailWhenTryingToGetUnknownKey()
104
    {
105
        $authenticator = new Authenticator($this->keys);
0 ignored issues
show
Documentation introduced by
$this->keys is of type array<integer,object<Kle...\Authenticator\JwtKey>>, but the function expects a object<ArrayObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
107
        $this->assertInstanceOf(self::JKEY_CLASS, $authenticator->getKeyById('blah'));
108
    }
109
110
    /**
111
     * @test
112
     * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
113
     */
114
    public function willFailWhenTryingToGetUserNameFromClaimsWithoutPrn()
115
    {
116
        $authenticator = new Authenticator($this->keys);
0 ignored issues
show
Documentation introduced by
$this->keys is of type array<integer,object<Kle...\Authenticator\JwtKey>>, but the function expects a object<ArrayObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
118
        $authenticator->getUsername([]);
119
    }
120
121
    /**
122
     * @test
123
     */
124
    public function canGetUserNameFromClaims()
125
    {
126
        $authenticator = new Authenticator($this->keys);
0 ignored issues
show
Documentation introduced by
$this->keys is of type array<integer,object<Kle...\Authenticator\JwtKey>>, but the function expects a object<ArrayObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
128
        $authenticator->getUsername(['prn' => 'johndoe']);
129
    }
130
131
    /**
132
     * @test
133
     */
134
    public function authenticateTokenWillSetUserFetchedFromUserProviderOnToken()
135
    {
136
        $claims        = ['prn' => 'john'];
137
        $authenticator = new Authenticator($this->keys);
0 ignored issues
show
Documentation introduced by
$this->keys is of type array<integer,object<Kle...\Authenticator\JwtKey>>, but the function expects a object<ArrayObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
        $anonToken     = new PreAuthenticatedToken('foo', $claims, 'myprovider');
139
        $userProvider  = $this->getMockBuilder(
140
            'Symfony\Component\Security\Core\User\UserProviderInterface'
141
        )->getMockForAbstractClass();
142
143
        $userProvider->expects($this->once())
144
            ->method('loadUserByUsername')
145
            ->with('john')
146
            ->willReturn(new User('john', 'hi there'));
147
        $authenticator->authenticateToken($anonToken, $userProvider, 'myprovider');
148
    }
149
150
    /**
151
     * @test
152
     */
153
    public function supportsPreAuthToken()
154
    {
155
        $authenticator = new Authenticator($this->keys);
0 ignored issues
show
Documentation introduced by
$this->keys is of type array<integer,object<Kle...\Authenticator\JwtKey>>, but the function expects a object<ArrayObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
156
157
        $securityToken = new PreAuthenticatedToken('foo', 'bar', 'myprovider');
158
        $actual        = $authenticator->supportsToken($securityToken, 'myprovider');
159
        $this->assertTrue($actual);
160
    }
161
162
    /**
163
     * @test
164
     * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
165
     */
166
    public function willFailWhenApiKeyNotFoundInHeader()
167
    {
168
        $authenticator = new Authenticator($this->keys);
0 ignored issues
show
Documentation introduced by
$this->keys is of type array<integer,object<Kle...\Authenticator\JwtKey>>, but the function expects a object<ArrayObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
169
        $request       = new Request();
170
        $authenticator->createToken($request, 'myprovider');
171
    }
172
173
    /**
174
     * @test
175
     */
176
    public function canGetAnonTokenWithClaims()
177
    {
178
        $authenticator = new Authenticator($this->keys);
0 ignored issues
show
Documentation introduced by
$this->keys is of type array<integer,object<Kle...\Authenticator\JwtKey>>, but the function expects a object<ArrayObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
179
        $request       = new Request();
180
        $request->headers->set('Authorization', 'Bearer ' . self::TEST_TOKEN);
181
        $token = $authenticator->createToken($request, 'myprovider');
182
183
        $expected = ["prn" => "john", 'iss' => 'http://api.server1.com/oauth2/token'];
184
        $this->assertSame($expected, $token->getCredentials());
185
    }
186
}
187