Failed Conditions
Push — ng ( 68a719...06acb0 )
by Florent
23:02
created

AuthenticationMethodManagerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 0
loc 71
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A genericCalls() 0 13 1
B theClientCannotUseSeveralAuthenticationMethods() 0 25 2
A theClientCanUseSeveralAuthenticationMethodsWhenOneIsNone() 0 19 1
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\Server\TokenEndpoint\Tests;
15
16
use OAuth2Framework\Component\Server\Core\Client\ClientId;
17
use OAuth2Framework\Component\Server\Core\Response\OAuth2Exception;
18
use OAuth2Framework\Component\Server\TokenEndpoint\AuthenticationMethod\AuthenticationMethod;
19
use OAuth2Framework\Component\Server\TokenEndpoint\AuthenticationMethod\AuthenticationMethodManager;
20
use OAuth2Framework\Component\Server\TokenEndpoint\AuthenticationMethod\ClientSecretBasic;
21
use OAuth2Framework\Component\Server\TokenEndpoint\AuthenticationMethod\ClientSecretPost;
22
use OAuth2Framework\Component\Server\TokenEndpoint\AuthenticationMethod\None;
23
use PHPUnit\Framework\TestCase;
24
use Psr\Http\Message\ServerRequestInterface;
25
26
/**
27
 * @group TokenEndpoint
28
 * @group AuthenticationMethod
29
 */
30
final class AuthenticationMethodManagerTest extends TestCase
31
{
32
    /**
33
     * @test
34
     */
35
    public function genericCalls()
36
    {
37
        $manager = new AuthenticationMethodManager();
38
        $manager
39
            ->add(new None())
40
            ->add(new ClientSecretBasic('Realm'))
41
        ;
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
56
            ->add(new ClientSecretBasic('My Service'))
57
            ->add(new ClientSecretPost())
58
        ;
59
        $request = $this->prophesize(ServerRequestInterface::class);
60
        $request->getHeader('Authorization')->willReturn(['Basic '.base64_encode('CLIENT_ID:CLIENT_SECRET')]);
61
        $request->getParsedBody()->willReturn([
62
            'client_id' => 'CLIENT_ID',
63
            'client_secret' => 'CLIENT_SECRET',
64
        ]);
65
66
        try {
67
            $manager->findClientIdAndCredentials($request->reveal(), $method, $credentials);
68
            $this->fail('An OAuth2 exception should be thrown.');
69
        } catch (OAuth2Exception $e) {
70
            self::assertEquals(400, $e->getCode());
71
            self::assertEquals([
72
                'error' => 'invalid_request',
73
                'error_description' => 'Only one authentication method may be used to authenticate the client.',
74
            ], $e->getData());
75
        }
76
    }
77
78
    /**
79
     * @test
80
     */
81
    public function theClientCanUseSeveralAuthenticationMethodsWhenOneIsNone()
82
    {
83
        $manager = new AuthenticationMethodManager();
84
        $manager
85
            ->add(new None())
86
            ->add(new ClientSecretPost())
87
        ;
88
        $request = $this->prophesize(ServerRequestInterface::class);
89
        $request->getParsedBody()->willReturn([
90
            'client_id' => 'CLIENT_ID',
91
            'client_secret' => 'CLIENT_SECRET',
92
        ]);
93
94
        $clientId = $manager->findClientIdAndCredentials($request->reveal(), $method, $credentials);
95
        self::assertInstanceOf(ClientSecretPost::class, $method);
96
        self::assertInstanceOf(ClientId::class, $clientId);
97
        self::assertEquals('CLIENT_SECRET', $credentials);
98
99
    }
100
}
101