Failed Conditions
Push — master ( 6a154b...66d9db )
by Florent
16:43 queued 07:43
created

theClientCannotUseSeveralAuthenticationMethods()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 2
eloc 17
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\ClientAuthentication\Tests;
15
16
use OAuth2Framework\Component\Core\Client\ClientId;
17
use OAuth2Framework\Component\Core\Message\OAuth2Message;
18
use OAuth2Framework\Component\ClientAuthentication\AuthenticationMethod;
19
use OAuth2Framework\Component\ClientAuthentication\AuthenticationMethodManager;
20
use OAuth2Framework\Component\ClientAuthentication\ClientSecretBasic;
21
use OAuth2Framework\Component\ClientAuthentication\ClientSecretPost;
22
use OAuth2Framework\Component\ClientAuthentication\None;
23
use PHPUnit\Framework\TestCase;
24
use Prophecy\Prophecy\ObjectProphecy;
25
use Psr\Http\Message\ServerRequestInterface;
26
use Psr\Http\Message\StreamInterface;
27
28
/**
29
 * @group TokenEndpoint
30
 * @group ClientAuthentication
31
 */
32
final class AuthenticationMethodManagerTest extends TestCase
33
{
34
    /**
35
     * @test
36
     */
37
    public function genericCalls()
38
    {
39
        $manager = new AuthenticationMethodManager();
40
        $manager->add(new None());
41
        $manager->add(new ClientSecretBasic('Realm'));
42
        self::assertTrue($manager->has('none'));
43
        self::assertEquals(['none', 'client_secret_basic'], $manager->list());
44
        self::assertInstanceOf(AuthenticationMethod::class, $manager->get('none'));
45
        self::assertEquals(2, count($manager->all()));
46
        self::assertEquals(['Basic realm="Realm",charset="UTF-8"'], $manager->getSchemesParameters());
47
    }
48
49
    /**
50
     * @test
51
     */
52
    public function theClientCannotUseSeveralAuthenticationMethods()
53
    {
54
        $manager = new AuthenticationMethodManager();
55
        $manager->add(new ClientSecretBasic('My Service'));
56
        $manager->add(new ClientSecretPost());
57
        $request = $this->buildRequest([
58
            'client_id' => 'CLIENT_ID',
59
            'client_secret' => 'CLIENT_SECRET',
60
        ]);
61
        $request->getHeader('Authorization')->willReturn(['Basic '.base64_encode('CLIENT_ID:CLIENT_SECRET')]);
62
63
        try {
64
            $manager->findClientIdAndCredentials($request->reveal(), $method, $credentials);
65
            $this->fail('An OAuth2 exception should be thrown.');
66
        } catch (OAuth2Message $e) {
67
            self::assertEquals(400, $e->getCode());
68
            self::assertEquals([
69
                'error' => 'invalid_request',
70
                'error_description' => 'Only one authentication method may be used to authenticate the client.',
71
            ], $e->getData());
72
        }
73
    }
74
75
    /**
76
     * @test
77
     */
78
    public function theClientCanUseSeveralAuthenticationMethodsWhenOneIsNone()
79
    {
80
        $manager = new AuthenticationMethodManager();
81
        $manager->add(new None());
82
        $manager->add(new ClientSecretPost());
83
        $request = $this->buildRequest([
84
            'client_id' => 'CLIENT_ID',
85
            'client_secret' => 'CLIENT_SECRET',
86
        ]);
87
88
        $clientId = $manager->findClientIdAndCredentials($request->reveal(), $method, $credentials);
89
        self::assertInstanceOf(ClientSecretPost::class, $method);
90
        self::assertInstanceOf(ClientId::class, $clientId);
91
        self::assertEquals('CLIENT_SECRET', $credentials);
92
    }
93
94
    private function buildRequest(array $data): ObjectProphecy
95
    {
96
        $body = $this->prophesize(StreamInterface::class);
97
        $body->getContents()->willReturn(http_build_query($data));
98
        $request = $this->prophesize(ServerRequestInterface::class);
99
        $request->hasHeader('Content-Type')->willReturn(true);
100
        $request->getHeader('Content-Type')->willReturn(['application/x-www-form-urlencoded']);
101
        $request->getBody()->willReturn($body->reveal());
102
        $request->getParsedBody()->willReturn([]);
103
104
        return $request;
105
    }
106
}
107