Cheetah
CouchbaseDB.php
Go to the documentation of this file.
1 <?php
2 
3 namespace OAuth2\Storage;
4 
5 use OAuth2\OpenID\Storage\AuthorizationCodeInterface as OpenIDAuthorizationCodeInterface;
6 
23  OpenIDAuthorizationCodeInterface
24 {
25  protected $db;
26  protected $config;
27 
28  public function __construct($connection, $config = array())
29  {
30  if ($connection instanceof \Couchbase) {
31  $this->db = $connection;
32  } else {
33  if (!is_array($connection) || !is_array($connection['servers'])) {
34  throw new \InvalidArgumentException('First argument to OAuth2\Storage\CouchbaseDB must be an instance of Couchbase or a configuration array containing a server array');
35  }
36 
37  $this->db = new \Couchbase($connection['servers'], (!isset($connection['username'])) ? '' : $connection['username'], (!isset($connection['password'])) ? '' : $connection['password'], $connection['bucket'], false);
38  }
39 
40  $this->config = array_merge(array(
41  'client_table' => 'oauth_clients',
42  'access_token_table' => 'oauth_access_tokens',
43  'refresh_token_table' => 'oauth_refresh_tokens',
44  'code_table' => 'oauth_authorization_codes',
45  'user_table' => 'oauth_users',
46  'jwt_table' => 'oauth_jwt',
47  ), $config);
48  }
49 
50  // Helper function to access couchbase item by type:
51  protected function getObjectByType($name,$id)
52  {
53  return json_decode($this->db->get($this->config[$name].'-'.$id),true);
54  }
55 
56  // Helper function to set couchbase item by type:
57  protected function setObjectByType($name,$id,$array)
58  {
59  $array['type'] = $name;
60 
61  return $this->db->set($this->config[$name].'-'.$id,json_encode($array));
62  }
63 
64  // Helper function to delete couchbase item by type, wait for persist to at least 1 node
65  protected function deleteObjectByType($name,$id)
66  {
67  $this->db->delete($this->config[$name].'-'.$id,"",1);
68  }
69 
70  /* ClientCredentialsInterface */
71  public function checkClientCredentials($client_id, $client_secret = null)
72  {
73  if ($result = $this->getObjectByType('client_table',$client_id)) {
74  return $result['client_secret'] == $client_secret;
75  }
76 
77  return false;
78  }
79 
80  public function isPublicClient($client_id)
81  {
82  if (!$result = $this->getObjectByType('client_table',$client_id)) {
83  return false;
84  }
85 
86  return empty($result['client_secret']);
87  }
88 
89  /* ClientInterface */
90  public function getClientDetails($client_id)
91  {
92  $result = $this->getObjectByType('client_table',$client_id);
93 
94  return is_null($result) ? false : $result;
95  }
96 
97  public function setClientDetails($client_id, $client_secret = null, $redirect_uri = null, $grant_types = null, $scope = null, $user_id = null)
98  {
99  if ($this->getClientDetails($client_id)) {
100 
101  $this->setObjectByType('client_table',$client_id, array(
102  'client_id' => $client_id,
103  'client_secret' => $client_secret,
104  'redirect_uri' => $redirect_uri,
105  'grant_types' => $grant_types,
106  'scope' => $scope,
107  'user_id' => $user_id,
108  ));
109  } else {
110  $this->setObjectByType('client_table',$client_id, array(
111  'client_id' => $client_id,
112  'client_secret' => $client_secret,
113  'redirect_uri' => $redirect_uri,
114  'grant_types' => $grant_types,
115  'scope' => $scope,
116  'user_id' => $user_id,
117  ));
118  }
119 
120  return true;
121  }
122 
123  public function checkRestrictedGrantType($client_id, $grant_type)
124  {
125  $details = $this->getClientDetails($client_id);
126  if (isset($details['grant_types'])) {
127  $grant_types = explode(' ', $details['grant_types']);
128 
129  return in_array($grant_type, $grant_types);
130  }
131 
132  // if grant_types are not defined, then none are restricted
133  return true;
134  }
135 
136  /* AccessTokenInterface */
137  public function getAccessToken($access_token)
138  {
139  $token = $this->getObjectByType('access_token_table',$access_token);
140 
141  return is_null($token) ? false : $token;
142  }
143 
144  public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope = null)
145  {
146  // if it exists, update it.
147  if ($this->getAccessToken($access_token)) {
148  $this->setObjectByType('access_token_table',$access_token, array(
149  'access_token' => $access_token,
150  'client_id' => $client_id,
151  'expires' => $expires,
152  'user_id' => $user_id,
153  'scope' => $scope
154  ));
155  } else {
156  $this->setObjectByType('access_token_table',$access_token, array(
157  'access_token' => $access_token,
158  'client_id' => $client_id,
159  'expires' => $expires,
160  'user_id' => $user_id,
161  'scope' => $scope
162  ));
163  }
164 
165  return true;
166  }
167 
168  /* AuthorizationCodeInterface */
169  public function getAuthorizationCode($code)
170  {
171  $code = $this->getObjectByType('code_table',$code);
172 
173  return is_null($code) ? false : $code;
174  }
175 
176  public function setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = null, $id_token = null)
177  {
178  // if it exists, update it.
179  if ($this->getAuthorizationCode($code)) {
180  $this->setObjectByType('code_table',$code, array(
181  'authorization_code' => $code,
182  'client_id' => $client_id,
183  'user_id' => $user_id,
184  'redirect_uri' => $redirect_uri,
185  'expires' => $expires,
186  'scope' => $scope,
187  'id_token' => $id_token,
188  ));
189  } else {
190  $this->setObjectByType('code_table',$code,array(
191  'authorization_code' => $code,
192  'client_id' => $client_id,
193  'user_id' => $user_id,
194  'redirect_uri' => $redirect_uri,
195  'expires' => $expires,
196  'scope' => $scope,
197  'id_token' => $id_token,
198  ));
199  }
200 
201  return true;
202  }
203 
204  public function expireAuthorizationCode($code)
205  {
206  $this->deleteObjectByType('code_table',$code);
207 
208  return true;
209  }
210 
211  /* UserCredentialsInterface */
212  public function checkUserCredentials($username, $password)
213  {
214  if ($user = $this->getUser($username)) {
215  return $this->checkPassword($user, $password);
216  }
217 
218  return false;
219  }
220 
221  public function getUserDetails($username)
222  {
223  if ($user = $this->getUser($username)) {
224  $user['user_id'] = $user['username'];
225  }
226 
227  return $user;
228  }
229 
230  /* RefreshTokenInterface */
231  public function getRefreshToken($refresh_token)
232  {
233  $token = $this->getObjectByType('refresh_token_table',$refresh_token);
234 
235  return is_null($token) ? false : $token;
236  }
237 
238  public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope = null)
239  {
240  $this->setObjectByType('refresh_token_table',$refresh_token, array(
241  'refresh_token' => $refresh_token,
242  'client_id' => $client_id,
243  'user_id' => $user_id,
244  'expires' => $expires,
245  'scope' => $scope
246  ));
247 
248  return true;
249  }
250 
251  public function unsetRefreshToken($refresh_token)
252  {
253  $this->deleteObjectByType('refresh_token_table',$refresh_token);
254 
255  return true;
256  }
257 
258  // plaintext passwords are bad! Override this for your application
259  protected function checkPassword($user, $password)
260  {
261  return $user['password'] == $password;
262  }
263 
264  public function getUser($username)
265  {
266  $result = $this->getObjectByType('user_table',$username);
267 
268  return is_null($result) ? false : $result;
269  }
270 
271  public function setUser($username, $password, $firstName = null, $lastName = null)
272  {
273  if ($this->getUser($username)) {
274  $this->setObjectByType('user_table',$username, array(
275  'username' => $username,
276  'password' => $password,
277  'first_name' => $firstName,
278  'last_name' => $lastName
279  ));
280 
281  } else {
282  $this->setObjectByType('user_table',$username, array(
283  'username' => $username,
284  'password' => $password,
285  'first_name' => $firstName,
286  'last_name' => $lastName
287  ));
288 
289  }
290 
291  return true;
292  }
293 
294  public function getClientKey($client_id, $subject)
295  {
296  if (!$jwt = $this->getObjectByType('jwt_table',$client_id)) {
297  return false;
298  }
299 
300  if (isset($jwt['subject']) && $jwt['subject'] == $subject) {
301  return $jwt['key'];
302  }
303 
304  return false;
305  }
306 
307  public function getClientScope($client_id)
308  {
309  if (!$clientDetails = $this->getClientDetails($client_id)) {
310  return false;
311  }
312 
313  if (isset($clientDetails['scope'])) {
314  return $clientDetails['scope'];
315  }
316 
317  return null;
318  }
319 
320  public function getJti($client_id, $subject, $audience, $expiration, $jti)
321  {
322  //TODO: Needs couchbase implementation.
323  throw new \Exception('getJti() for the Couchbase driver is currently unimplemented.');
324  }
325 
326  public function setJti($client_id, $subject, $audience, $expiration, $jti)
327  {
328  //TODO: Needs couchbase implementation.
329  throw new \Exception('setJti() for the Couchbase driver is currently unimplemented.');
330  }
331 }
OAuth2\Storage\CouchbaseDB\checkRestrictedGrantType
checkRestrictedGrantType($client_id, $grant_type)
Definition: CouchbaseDB.php:123
OAuth2\Storage\CouchbaseDB\getAuthorizationCode
getAuthorizationCode($code)
Definition: CouchbaseDB.php:169
OAuth2\Storage\CouchbaseDB\getUser
getUser($username)
Definition: CouchbaseDB.php:264
OAuth2\OpenID\Storage\AuthorizationCodeInterface
Definition: AuthorizationCodeInterface.php:14
OAuth2\Storage\CouchbaseDB\getUserDetails
getUserDetails($username)
Definition: CouchbaseDB.php:221
OAuth2\Storage\CouchbaseDB\setRefreshToken
setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope=null)
Definition: CouchbaseDB.php:238
OAuth2\Storage\CouchbaseDB\getRefreshToken
getRefreshToken($refresh_token)
Definition: CouchbaseDB.php:231
OAuth2\Storage\CouchbaseDB\checkPassword
checkPassword($user, $password)
Definition: CouchbaseDB.php:259
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
OAuth2\Storage\CouchbaseDB\setAuthorizationCode
setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope=null, $id_token=null)
Definition: CouchbaseDB.php:176
php
OAuth2\Storage\CouchbaseDB\$db
$db
Definition: CouchbaseDB.php:25
OAuth2\Storage\CouchbaseDB\getClientKey
getClientKey($client_id, $subject)
Definition: CouchbaseDB.php:294
OAuth2\Storage\UserCredentialsInterface
Definition: UserCredentialsInterface.php:13
OAuth2\Storage\CouchbaseDB\getClientScope
getClientScope($client_id)
Definition: CouchbaseDB.php:307
OAuth2\Storage\CouchbaseDB\getClientDetails
getClientDetails($client_id)
Definition: CouchbaseDB.php:90
OAuth2\Storage\AuthorizationCodeInterface
Definition: AuthorizationCodeInterface.php:13
OAuth2\Storage\CouchbaseDB\setJti
setJti($client_id, $subject, $audience, $expiration, $jti)
Definition: CouchbaseDB.php:326
OAuth2\Storage\CouchbaseDB\$config
$config
Definition: CouchbaseDB.php:26
OAuth2\Storage\CouchbaseDB\setClientDetails
setClientDetails($client_id, $client_secret=null, $redirect_uri=null, $grant_types=null, $scope=null, $user_id=null)
Definition: CouchbaseDB.php:97
OAuth2\Storage\CouchbaseDB\unsetRefreshToken
unsetRefreshToken($refresh_token)
Definition: CouchbaseDB.php:251
OAuth2\Storage\CouchbaseDB\setUser
setUser($username, $password, $firstName=null, $lastName=null)
Definition: CouchbaseDB.php:271
OAuth2\Storage\CouchbaseDB\checkUserCredentials
checkUserCredentials($username, $password)
Definition: CouchbaseDB.php:212
OAuth2\Storage\CouchbaseDB\__construct
__construct($connection, $config=array())
Definition: CouchbaseDB.php:28
OAuth2\Storage\ClientCredentialsInterface
Definition: ClientCredentialsInterface.php:12
OAuth2\Storage\CouchbaseDB\isPublicClient
isPublicClient($client_id)
Definition: CouchbaseDB.php:80
OAuth2\Storage\RefreshTokenInterface
Definition: RefreshTokenInterface.php:13
OAuth2\Storage\CouchbaseDB\expireAuthorizationCode
expireAuthorizationCode($code)
Definition: CouchbaseDB.php:204
OAuth2\Storage\CouchbaseDB\setObjectByType
setObjectByType($name, $id, $array)
Definition: CouchbaseDB.php:57
OAuth2\Storage\CouchbaseDB
Definition: CouchbaseDB.php:24
OAuth2\Storage\CouchbaseDB\getObjectByType
getObjectByType($name, $id)
Definition: CouchbaseDB.php:51
OAuth2\Storage\CouchbaseDB\deleteObjectByType
deleteObjectByType($name, $id)
Definition: CouchbaseDB.php:65
OAuth2\Storage
Definition: AccessTokenInterface.php:3
OAuth2\Storage\CouchbaseDB\getAccessToken
getAccessToken($access_token)
Definition: CouchbaseDB.php:137
OAuth2\Storage\JwtBearerInterface
Definition: JwtBearerInterface.php:16
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
OAuth2\Storage\CouchbaseDB\getJti
getJti($client_id, $subject, $audience, $expiration, $jti)
Definition: CouchbaseDB.php:320
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
OAuth2\Storage\CouchbaseDB\setAccessToken
setAccessToken($access_token, $client_id, $user_id, $expires, $scope=null)
Definition: CouchbaseDB.php:144
OAuth2\Storage\CouchbaseDB\checkClientCredentials
checkClientCredentials($client_id, $client_secret=null)
Definition: CouchbaseDB.php:71
false
if(!defined("FALSE_VAL")) define("FALSE_VAL" false
Definition: constants.inc.php:9
OAuth2\Storage\AccessTokenInterface
Definition: AccessTokenInterface.php:12