|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: GCC-MED |
|
5
|
|
|
* Date: 09/03/2018 |
|
6
|
|
|
* Time: 17:00 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace OAuth2\ClientAuthentication; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use OAuth2\Exceptions\OAuthException; |
|
13
|
|
|
use OAuth2\Roles\ClientInterface; |
|
14
|
|
|
use OAuth2\Storages\ClientStorageInterface; |
|
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
16
|
|
|
|
|
17
|
|
|
class ClientAuthenticationMethodManager |
|
18
|
|
|
{ |
|
19
|
|
|
protected $clientAuthenticationMethods = []; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ClientStorageInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $clientStorage; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(ClientStorageInterface $clientStorage) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->clientStorage = $clientStorage; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function addClientAuthenticationMethod(string $identifier, ClientAuthenticationMethodInterface $clientAuthenticationMethod) { |
|
31
|
|
|
$this->clientAuthenticationMethods[$identifier] = $clientAuthenticationMethod; |
|
32
|
|
|
} |
|
33
|
|
|
public function getClientAuthenticationMethod(string $identifier): ClientAuthenticationMethodInterface |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->clientAuthenticationMethods[$identifier] ?? null; |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* authenticate the client if client authentication is included |
|
40
|
|
|
* @param ServerRequestInterface $request |
|
41
|
|
|
* @param array $requestData |
|
42
|
|
|
* @return ClientInterface |
|
43
|
|
|
* @throws OAuthException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function authenticate(ServerRequestInterface $request, array $requestData): ClientInterface |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* require client authentication for confidential clients or for any |
|
49
|
|
|
* client that was issued client credentials (or with other |
|
50
|
|
|
* authentication requirements) |
|
51
|
|
|
*/ |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var ClientAuthenticationMethodInterface $clientAuthenticationMethod |
|
55
|
|
|
*/ |
|
56
|
|
|
$clientAuthenticationMethodUsedIdentifier = null; |
|
57
|
|
|
$clientAuthenticationMethodUsed = null; |
|
58
|
|
|
$authenticated = false; |
|
59
|
|
|
foreach ($this->clientAuthenticationMethods as $identifier => $clientAuthenticationMethod) { |
|
60
|
|
|
if($clientAuthenticationMethod->support($request, $requestData)) { |
|
61
|
|
|
if($clientAuthenticationMethodUsedIdentifier) { |
|
62
|
|
|
throw new OAuthException('invalid_request', |
|
63
|
|
|
'The request utilizes more than one mechanism for authenticating the client.', |
|
64
|
|
|
'https://tools.ietf.org/html/rfc6749#section-3.2.1'); |
|
65
|
|
|
} |
|
66
|
|
|
$clientAuthenticationMethodUsedIdentifier = $identifier; |
|
67
|
|
|
$clientAuthenticationMethodUsed = $clientAuthenticationMethod; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if($clientAuthenticationMethodUsed) { |
|
72
|
|
|
if(!$client = $clientAuthenticationMethod->authenticate($request, $requestData)) { |
|
|
|
|
|
|
73
|
|
|
throw new OAuthException('invalid_client', |
|
74
|
|
|
'Client authentication failed. Unknown client.', |
|
75
|
|
|
'https://tools.ietf.org/html/rfc6749#section-3.2.1'); |
|
76
|
|
|
} |
|
77
|
|
|
} else { |
|
78
|
|
|
if (empty($requestData['client_id'])) { |
|
79
|
|
|
throw new OAuthException('invalid_request', 'The request is missing the required parameter client_id.', |
|
80
|
|
|
'https://tools.ietf.org/html/rfc6749#section-4.1'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (!$client = $this->clientStorage->get($requestData['client_id'])) { |
|
84
|
|
|
throw new OAuthException('invalid_request', 'The request includes the invalid parameter client_id.', |
|
85
|
|
|
'https://tools.ietf.org/html/rfc6749#section-4.1'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if($client->hasCredentials()) { |
|
89
|
|
|
if (!$authenticated) { |
|
|
|
|
|
|
90
|
|
|
throw new OAuthException('invalid_client', 'Client authentication failed. No client authentication included', |
|
91
|
|
|
'https://tools.ietf.org/html/rfc6749#section-3.2.1'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$clientAuthenticationMethodUsedIdentifier = 'none'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$tokenEndpointAuthMethod = $client->getMetadata()->getTokenEndpointAuthMethod() ?: 'client_secret_basic'; |
|
99
|
|
|
if($tokenEndpointAuthMethod !== $clientAuthenticationMethodUsedIdentifier) { |
|
100
|
|
|
throw new OAuthException('invalid_client', |
|
101
|
|
|
'Client authentication failed. Unsupported authentication method.', |
|
102
|
|
|
'https://tools.ietf.org/html/rfc6749#section-3.2.1'); |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $client; |
|
107
|
|
|
} |
|
108
|
|
|
} |