|
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 |
|
|
|
|
|
|
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
|
|
|
$authenticationMethod = null; |
|
108
|
|
|
throw new OAuth2Exception( |
|
109
|
|
|
400, ['error' => OAuth2ResponseFactoryManager::ERROR_INVALID_REQUEST, 'error_description' => 'Only one authentication method may be used to authenticate the client.']); |
|
110
|
|
|
} else { |
|
111
|
|
|
$clientId = $temp; |
|
112
|
|
|
$authenticationMethod = $method; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $clientId; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param ServerRequestInterface $request |
|
122
|
|
|
* @param Client $client |
|
123
|
|
|
* @param TokenEndpointAuthMethodInterface $authenticationMethod |
|
124
|
|
|
* @param mixed $clientCredentials |
|
125
|
|
|
* |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
public function isClientAuthenticated(ServerRequestInterface $request, Client $client, TokenEndpointAuthMethodInterface $authenticationMethod, $clientCredentials): bool |
|
129
|
|
|
{ |
|
130
|
|
|
if (true === $client->isDeleted()) { |
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
if (in_array($client->get('token_endpoint_auth_method'), $authenticationMethod->getSupportedAuthenticationMethods())) { |
|
134
|
|
|
if (false === $client->areClientCredentialsExpired()) { |
|
135
|
|
|
return $authenticationMethod->isClientAuthenticated($client, $clientCredentials, $request); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
This check looks for
@paramannotations 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.