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\AuthenticationMethod; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Server\Core\Client\Client; |
17
|
|
|
use OAuth2Framework\Component\Server\Core\Client\ClientId; |
18
|
|
|
use OAuth2Framework\Component\Server\Core\Response\OAuth2Exception; |
19
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
20
|
|
|
|
21
|
|
|
final class TokenEndpointAuthenticationMethodManager |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var TokenEndpointAuthenticationMethod[] |
25
|
|
|
*/ |
26
|
|
|
private $tokenEndpointAuthenticationMethodNames = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TokenEndpointAuthenticationMethod[] |
30
|
|
|
*/ |
31
|
|
|
private $tokenEndpointAuthenticationMethods = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param TokenEndpointAuthenticationMethod $tokenEndpointAuthenticationMethod |
35
|
|
|
* |
36
|
|
|
* @return TokenEndpointAuthenticationMethodManager |
37
|
|
|
*/ |
38
|
|
|
public function add(TokenEndpointAuthenticationMethod $tokenEndpointAuthenticationMethod): self |
39
|
|
|
{ |
40
|
|
|
$this->tokenEndpointAuthenticationMethods[] = $tokenEndpointAuthenticationMethod; |
41
|
|
|
foreach ($tokenEndpointAuthenticationMethod->getSupportedAuthenticationMethods() as $method_name) { |
42
|
|
|
$this->tokenEndpointAuthenticationMethodNames[$method_name] = $tokenEndpointAuthenticationMethod; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string[] |
50
|
|
|
*/ |
51
|
|
|
public function all(): array |
52
|
|
|
{ |
53
|
|
|
return array_keys($this->tokenEndpointAuthenticationMethodNames); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $tokenEndpointAuthenticationMethod |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function has(string $tokenEndpointAuthenticationMethod): bool |
62
|
|
|
{ |
63
|
|
|
return array_key_exists($tokenEndpointAuthenticationMethod, $this->tokenEndpointAuthenticationMethodNames); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $tokenEndpointAuthenticationMethod |
68
|
|
|
* |
69
|
|
|
* @throws \InvalidArgumentException |
70
|
|
|
* |
71
|
|
|
* @return TokenEndpointAuthenticationMethod |
72
|
|
|
*/ |
73
|
|
|
public function get(string $tokenEndpointAuthenticationMethod): TokenEndpointAuthenticationMethod |
74
|
|
|
{ |
75
|
|
|
Assertion::true($this->has($tokenEndpointAuthenticationMethod), sprintf('The token endpoint authentication method "%s" is not supported. Please use one of the following values: %s', $tokenEndpointAuthenticationMethod, implode(', ', $this->all()))); |
76
|
|
|
|
77
|
|
|
return $this->tokenEndpointAuthenticationMethodNames[$tokenEndpointAuthenticationMethod]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return TokenEndpointAuthenticationMethod[] |
82
|
|
|
*/ |
83
|
|
|
public function getTokenEndpointAuthenticationMethods(): array |
84
|
|
|
{ |
85
|
|
|
return array_values($this->tokenEndpointAuthenticationMethods); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param ServerRequestInterface $request |
90
|
|
|
* @param TokenEndpointAuthenticationMethod $authenticationMethod |
|
|
|
|
91
|
|
|
* @param mixed $clientCredentials The client credentials found in the request |
92
|
|
|
* |
93
|
|
|
* @throws OAuth2Exception |
94
|
|
|
* |
95
|
|
|
* @return null|ClientId |
96
|
|
|
*/ |
97
|
|
|
public function findClientInformationInTheRequest(ServerRequestInterface $request, TokenEndpointAuthenticationMethod &$authenticationMethod = null, &$clientCredentials = null) |
98
|
|
|
{ |
99
|
|
|
$clientId = null; |
100
|
|
|
$clientCredentials = null; |
101
|
|
|
foreach ($this->getTokenEndpointAuthenticationMethods() as $method) { |
102
|
|
|
$temp = $method->findClientId($request, $clientCredentials); |
103
|
|
|
if (null !== $temp) { |
104
|
|
|
if (null !== $clientId) { |
105
|
|
|
if (!$method instanceof None && !$authenticationMethod instanceof None) { |
106
|
|
|
$authenticationMethod = null; |
107
|
|
|
|
108
|
|
|
throw new OAuth2Exception( |
|
|
|
|
109
|
|
|
400, ['error' => OAuth2Exception::ERROR_INVALID_REQUEST, 'error_description' => 'Only one authentication method may be used to authenticate the client.']); |
|
|
|
|
110
|
|
|
} else { |
111
|
|
|
if (!$method instanceof None) { |
112
|
|
|
$authenticationMethod = $method; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} else { |
116
|
|
|
$clientId = $temp; |
117
|
|
|
$authenticationMethod = $method; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $clientId; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param ServerRequestInterface $request |
127
|
|
|
* @param Client $client |
128
|
|
|
* @param TokenEndpointAuthenticationMethod $authenticationMethod |
129
|
|
|
* @param mixed $clientCredentials |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function isClientAuthenticated(ServerRequestInterface $request, Client $client, TokenEndpointAuthenticationMethod $authenticationMethod, $clientCredentials): bool |
134
|
|
|
{ |
135
|
|
|
if (true === $client->isDeleted()) { |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
if (in_array($client->get('token_endpoint_auth_method'), $authenticationMethod->getSupportedAuthenticationMethods())) { |
139
|
|
|
if (false === $client->areClientCredentialsExpired()) { |
140
|
|
|
return $authenticationMethod->isClientAuthenticated($client, $clientCredentials, $request); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
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.