Failed Conditions
Pull Request — master (#63)
by Florent
07:25 queued 04:17
created

findClientInformationInTheRequest()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 6
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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\TokenEndpointAuthMethod;
15
16
use Assert\Assertion;
17
use OAuth2Framework\Component\Server\Model\Client\Client;
18
use OAuth2Framework\Component\Server\Model\Client\ClientId;
19
use OAuth2Framework\Component\Server\Response\OAuth2Exception;
20
use OAuth2Framework\Component\Server\Response\OAuth2ResponseFactoryManager;
21
use Psr\Http\Message\ServerRequestInterface;
22
23
final class TokenEndpointAuthMethodManager
24
{
25
    /**
26
     * @var TokenEndpointAuthMethodInterface[]
27
     */
28
    private $tokenEndpointAuthMethodNames = [];
29
30
    /**
31
     * @var TokenEndpointAuthMethodInterface[]
32
     */
33
    private $tokenEndpointAuthMethods = [];
34
35
    /**
36
     * @param TokenEndpointAuthMethodInterface $tokenEndpointAuthMethod
37
     *
38
     * @return TokenEndpointAuthMethodManager
39
     */
40
    public function add(TokenEndpointAuthMethodInterface $tokenEndpointAuthMethod): TokenEndpointAuthMethodManager
41
    {
42
        $this->tokenEndpointAuthMethods[] = $tokenEndpointAuthMethod;
43
        foreach ($tokenEndpointAuthMethod->getSupportedAuthenticationMethods() as $method_name) {
44
            $this->tokenEndpointAuthMethodNames[$method_name] = $tokenEndpointAuthMethod;
45
        }
46
47
        return $this;
48
    }
49
50
    /**
51
     * @return string[]
52
     */
53
    public function all(): array
54
    {
55
        return array_keys($this->tokenEndpointAuthMethodNames);
56
    }
57
58
    /**
59
     * @param string $tokenEndpointAuthMethod
60
     *
61
     * @return bool
62
     */
63
    public function has(string $tokenEndpointAuthMethod): bool
64
    {
65
        return array_key_exists($tokenEndpointAuthMethod, $this->tokenEndpointAuthMethodNames);
66
    }
67
68
    /**
69
     * @param string $tokenEndpointAuthMethod
70
     *
71
     * @throws \InvalidArgumentException
72
     *
73
     * @return TokenEndpointAuthMethodInterface
74
     */
75
    public function get(string $tokenEndpointAuthMethod): TokenEndpointAuthMethodInterface
76
    {
77
        Assertion::true($this->has($tokenEndpointAuthMethod), sprintf('The token endpoint authentication method \'%s\' is not supported. Please use one of the following values: %s', $tokenEndpointAuthMethod, implode(', ', $this->all())));
78
79
        return $this->tokenEndpointAuthMethodNames[$tokenEndpointAuthMethod];
80
    }
81
82
    /**
83
     * @return TokenEndpointAuthMethodInterface[]
84
     */
85
    public function getTokenEndpointAuthMethods(): array
86
    {
87
        return array_values($this->tokenEndpointAuthMethods);
88
    }
89
90
    /**
91
     * @param ServerRequestInterface           $request
92
     * @param TokenEndpointAuthMethodInterface $authenticationMethod
0 ignored issues
show
Documentation introduced by
Should the type for parameter $authenticationMethod not be null|TokenEndpointAuthMethodInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
93
     * @param mixed                            $clientCredentials    The client credentials found in the request
94
     *
95
     * @throws OAuth2Exception
96
     *
97
     * @return null|ClientId
98
     */
99
    public function findClientInformationInTheRequest(ServerRequestInterface $request, TokenEndpointAuthMethodInterface &$authenticationMethod = null, &$clientCredentials = null)
100
    {
101
        $clientId = null;
102
        $clientCredentials = null;
103
        foreach ($this->getTokenEndpointAuthMethods() as $method) {
104
            $temp = $method->findClientId($request, $clientCredentials);
105
            if (null !== $temp) {
106
                if (null !== $clientId) {
107
                    if (!$method instanceof None && !$authenticationMethod instanceof None) {
108
                        $authenticationMethod = null;
109
110
                        throw new OAuth2Exception(
111
                            400, ['error' => OAuth2ResponseFactoryManager::ERROR_INVALID_REQUEST, 'error_description' => 'Only one authentication method may be used to authenticate the client.']);
112
                    } else {
113
                        if (!$method instanceof None) {
114
                            $authenticationMethod = $method;
115
                        }
116
                    }
117
                } else {
118
                    $clientId = $temp;
119
                    $authenticationMethod = $method;
120
                }
121
            }
122
        }
123
124
        return $clientId;
125
    }
126
127
    /**
128
     * @param ServerRequestInterface           $request
129
     * @param Client                           $client
130
     * @param TokenEndpointAuthMethodInterface $authenticationMethod
131
     * @param mixed                            $clientCredentials
132
     *
133
     * @return bool
134
     */
135
    public function isClientAuthenticated(ServerRequestInterface $request, Client $client, TokenEndpointAuthMethodInterface $authenticationMethod, $clientCredentials): bool
136
    {
137
        if (true === $client->isDeleted()) {
138
            return false;
139
        }
140
        if (in_array($client->get('token_endpoint_auth_method'), $authenticationMethod->getSupportedAuthenticationMethods())) {
141
            if (false === $client->areClientCredentialsExpired()) {
142
                return $authenticationMethod->isClientAuthenticated($client, $clientCredentials, $request);
143
            }
144
        }
145
146
        return false;
147
    }
148
}
149