-
Notifications
You must be signed in to change notification settings - Fork 946
Expand file tree
/
Copy pathHttpBasic.php
More file actions
157 lines (133 loc) · 5.67 KB
/
Copy pathHttpBasic.php
File metadata and controls
157 lines (133 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace OAuth2\ClientAssertionType;
use OAuth2\ResponseException;
use OAuth2\Storage\ClientCredentialsInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Validate a client via Http Basic authentication
*
* @author Brent Shaffer <bshafs at gmail dot com>
*/
class HttpBasic implements ClientAssertionTypeInterface
{
private $clientData;
protected $storage;
protected $config;
/**
* @param OAuth2\Storage\ClientCredentialsInterface $clientStorage REQUIRED Storage class for retrieving client credentials information
* @param array $config OPTIONAL Configuration options for the server
* <code>
* $config = array(
* 'allow_credentials_in_request_body' => true, // whether to look for credentials in the POST body in addition to the Authorize HTTP Header
* 'allow_public_clients' => true // if true, "public clients" (clients without a secret) may be authenticated
* );
* </code>
*/
public function __construct(ClientCredentialsInterface $storage, array $config = array())
{
$this->storage = $storage;
$this->config = array_merge(array(
'allow_credentials_in_request_body' => true,
'allow_public_clients' => true,
), $config);
}
public function validateRequest(RequestInterface $request, &$errors = null)
{
if (!$clientData = $this->getClientCredentials($request, $errors)) {
return false;
}
if (!isset($clientData['client_id'])) {
throw new \LogicException('the clientData array must have "client_id" set');
}
if (!isset($clientData['client_secret']) || $clientData['client_secret'] == '') {
if (!$this->config['allow_public_clients']) {
$errors = array(
'error' => 'invalid_client',
'description' => 'client credentials are required'
);
return false;
}
if (!$this->storage->isPublicClient($clientData['client_id'])) {
$errors = array(
'error' => 'invalid_client',
'description' => 'This client is invalid or must authenticate using a client secret'
);
return false;
}
} elseif ($this->storage->checkClientCredentials($clientData['client_id'], $clientData['client_secret']) === false) {
$errors = array(
'error' => 'invalid_client',
'description' => 'The client credentials are invalid'
);
return false;
}
$this->clientData = $clientData;
return true;
}
public function getClientId()
{
return $this->clientData['client_id'];
}
/**
* Internal function used to get the client credentials from HTTP basic
* auth or POST data.
*
* According to the spec (draft 20), the client_id can be provided in
* the Basic Authorization header (recommended) or via GET/POST.
*
* @return
* A list containing the client identifier and password, for example
* @code
* return array(
* "client_id" => CLIENT_ID, // REQUIRED the client id
* "client_secret" => CLIENT_SECRET, // OPTIONAL the client secret (may be omitted for public clients)
* );
* @endcode
*
* @see http://tools.ietf.org/html/rfc6749#section-2.3.1
*
* @ingroup oauth2_section_2
*/
public function getClientCredentials(RequestInterface $request, &$errors = null)
{
if (
($clientId = $request->getHeaderLine('PHP_AUTH_USER'))
&& ($clientSecret = $request->getHeaderLine('PHP_AUTH_PW'))
) {
return array(
'client_id' => $clientId,
'client_secret' => $clientSecret
);
}
if ($authorizationHeader = $request->getServerParams()["HTTP_AUTHORIZATION"]) {
$exploded = explode(':', base64_decode(substr($authorizationHeader, 6)));
if (count($exploded) != 2) {
return null;
}
$result = [];
list($result['client_id'], $result['client_secret']) = $exploded;
return $result;
}
if ($this->config['allow_credentials_in_request_body']) {
$body = json_decode((string) $request->getBody(), true);
// Using POST for HttpBasic authorization is not recommended, but is supported by specification
if (!empty($body['client_id'])) {
/**
* client_secret can be null if the client's password is an empty string
* @see http://tools.ietf.org/html/rfc6749#section-2.3.1
*/
return array(
'client_id' => $body['client_id'],
'client_secret' => isset($body['client_secret']) ? $body['client_secret'] : '',
);
}
}
$message = $this->config['allow_credentials_in_request_body'] ? ' or body' : '';
$errors = array(
'error' => 'invalid_client',
'description' => 'Client credentials were not found in the headers' . $message,
);
return null;
}
}