1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Alexandre |
5
|
|
|
* Date: 07/01/2018 |
6
|
|
|
* Time: 14:19 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OAuth2OLD\ClientAuthentication; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use OAuth2OLD\ClientAuthentication\Authenticators\ClientAuthenticatorInterface; |
13
|
|
|
use OAuth2OLD\Config; |
14
|
|
|
use OAuth2OLD\Exceptions\OAuthException; |
15
|
|
|
use OAuth2OLD\Roles\ClientInterface; |
16
|
|
|
use OAuth2OLD\Server; |
17
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
18
|
|
|
|
19
|
|
|
class Guard |
20
|
|
|
{ |
21
|
|
|
private $client; |
22
|
|
|
private $error; |
23
|
|
|
/** |
24
|
|
|
* @var Server |
25
|
|
|
*/ |
26
|
|
|
private $server; |
27
|
|
|
private $enforceTLS; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Guard constructor. |
31
|
|
|
* @param Server $server |
32
|
|
|
* @throws \Exception |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Server $server) |
35
|
|
|
{ |
36
|
|
|
$this->client = null; |
37
|
|
|
$this->error = null; |
38
|
|
|
$this->server = $server; |
39
|
|
|
$this->enforceTLS = $server->getConfigurationRepository()->getConfig(Config::ENFORCE_TLS); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ServerRequestInterface $request |
44
|
|
|
* @return bool |
45
|
|
|
* @throws OAuthException |
46
|
|
|
* @throws \Exception |
47
|
|
|
* @implementation if you use other authenticators, override this method to decide |
48
|
|
|
* what authenticator the guard should use for current client |
49
|
|
|
*/ |
50
|
|
|
public function authenticate(ServerRequestInterface $request): bool |
51
|
|
|
{ |
52
|
|
|
$clientAuthenticatorSupported = null; |
53
|
|
|
/** |
54
|
|
|
* @var ClientAuthenticatorInterface $clientAuthenticator |
55
|
|
|
*/ |
56
|
|
|
foreach ($this->server->getClientAuthenticatorRepository()->getClientAuthenticators() as $clientAuthenticator) { |
57
|
|
|
if ($clientAuthenticator->support($request)) { |
58
|
|
|
if ($clientAuthenticatorSupported) { |
59
|
|
|
throw new OAuthException('invalid_client', 'Multiple authentication methods used', |
60
|
|
|
'https://tools.ietf.org/html/rfc6749#section-5.2'); |
61
|
|
|
} |
62
|
|
|
$clientAuthenticatorSupported = $clientAuthenticator; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!$clientAuthenticatorSupported) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
$this->client = $clientAuthenticatorSupported->authenticate($request); |
72
|
|
|
} catch (\Exception $e) { |
73
|
|
|
throw new OAuthException('invalid_client', $e->getMessage(), |
74
|
|
|
'https://tools.ietf.org/html/rfc6749#section-5.2'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (($this->enforceTLS === true && !$this->server->isSecure()) || |
78
|
|
|
(is_null($this->enforceTLS) && $clientAuthenticatorSupported->isPasswordAuthentication() && |
79
|
|
|
!$this->server->isSecure() && $this->client->isTLSSupported())) { |
80
|
|
|
throw new OAuthException('access_denied', 'Require the use of TLS', |
81
|
|
|
'https://tools.ietf.org/html/rfc6749#section-3.1.2.1'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return null|ClientInterface |
89
|
|
|
*/ |
90
|
|
|
public function getClient(): ?ClientInterface |
91
|
|
|
{ |
92
|
|
|
return $this->client; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return null|string |
97
|
|
|
*/ |
98
|
|
|
public function getError(): ?string |
99
|
|
|
{ |
100
|
|
|
return $this->error; |
101
|
|
|
} |
102
|
|
|
} |