5 use phpcassa\ColumnFamily;
6 use phpcassa\ColumnSlice;
7 use phpcassa\Connection\ConnectionPool;
41 OpenIDAuthorizationCodeInterface
60 if ($connection instanceof ConnectionPool) {
61 $this->cassandra = $connection;
63 if (!is_array($connection)) {
64 throw new \InvalidArgumentException(
'First argument to OAuth2\Storage\Cassandra must be an instance of phpcassa\Connection\ConnectionPool or a configuration array');
66 $connection = array_merge(array(
67 'keyspace' =>
'oauth2',
71 $this->cassandra =
new ConnectionPool($connection[
'keyspace'], $connection[
'servers']);
74 $this->config = array_merge(array(
76 'column_family' =>
'auth',
79 'client_key' =>
'oauth_clients:',
80 'access_token_key' =>
'oauth_access_tokens:',
81 'refresh_token_key' =>
'oauth_refresh_tokens:',
82 'code_key' =>
'oauth_authorization_codes:',
83 'user_key' =>
'oauth_users:',
84 'jwt_key' =>
'oauth_jwt:',
85 'scope_key' =>
'oauth_scopes:',
86 'public_key_key' =>
'oauth_public_keys:',
92 if (isset($this->cache[$key])) {
93 return $this->cache[$key];
95 $cf =
new ColumnFamily($this->cassandra, $this->config[
'column_family']);
98 $value = $cf->get($key,
new ColumnSlice(
"",
""));
99 $value = array_shift($value);
100 }
catch (\cassandra\NotFoundException $e) {
104 return json_decode($value,
true);
107 protected function setValue($key, $value, $expire = 0)
109 $this->cache[$key] = $value;
111 $cf =
new ColumnFamily($this->cassandra, $this->config[
'column_family']);
113 $str = json_encode($value);
116 $seconds = $expire -
time();
118 $cf->insert($key, array(
'__data' => $str),
null, $seconds);
119 }
catch (\Exception $e) {
125 $cf->insert($key, array(
'__data' => $str));
126 }
catch (\Exception $e) {
136 unset($this->cache[$key]);
138 $cf =
new ColumnFamily($this->cassandra, $this->config[
'column_family']);
141 $cf->remove($key, array(
'__data'));
142 }
catch (\Exception $e) {
152 return $this->
getValue($this->config[
'code_key'] . $code);
155 public function setAuthorizationCode($authorization_code, $client_id, $user_id, $redirect_uri, $expires, $scope =
null, $id_token =
null)
158 $this->config[
'code_key'] . $authorization_code,
159 compact(
'authorization_code',
'client_id',
'user_id',
'redirect_uri',
'expires',
'scope',
'id_token'),
166 $key = $this->config[
'code_key'] . $code;
167 unset($this->cache[$key]);
175 if ($user = $this->
getUser($username)) {
185 return $user[
'password'] == sha1($password);
190 return $this->
getUser($username);
195 if (!$userInfo = $this->
getValue($this->config[
'user_key'] . $username)) {
200 return array_merge(array(
201 'user_id' => $username,
205 public function setUser($username, $password, $first_name =
null, $last_name =
null)
207 $password = sha1($password);
210 $this->config[
'user_key'] . $username,
211 compact(
'username',
'password',
'first_name',
'last_name')
222 return isset($client[
'client_secret'])
223 && $client[
'client_secret'] == $client_secret;
232 return empty($result[
'client_secret']);
238 return $this->
getValue($this->config[
'client_key'] . $client_id);
241 public function setClientDetails($client_id, $client_secret =
null, $redirect_uri =
null, $grant_types =
null, $scope =
null, $user_id =
null)
244 $this->config[
'client_key'] . $client_id,
245 compact(
'client_id',
'client_secret',
'redirect_uri',
'grant_types',
'scope',
'user_id')
252 if (isset($details[
'grant_types'])) {
253 $grant_types = explode(
' ', $details[
'grant_types']);
255 return in_array($grant_type, (array) $grant_types);
265 return $this->
getValue($this->config[
'refresh_token_key'] . $refresh_token);
268 public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope =
null)
271 $this->config[
'refresh_token_key'] . $refresh_token,
272 compact(
'refresh_token',
'client_id',
'user_id',
'expires',
'scope'),
279 return $this->
expireValue($this->config[
'refresh_token_key'] . $refresh_token);
285 return $this->
getValue($this->config[
'access_token_key'].$access_token);
288 public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope =
null)
291 $this->config[
'access_token_key'].$access_token,
292 compact(
'access_token',
'client_id',
'user_id',
'expires',
'scope'),
299 return $this->
expireValue($this->config[
'access_token_key'] . $access_token);
305 $scope = explode(
' ', $scope);
307 $result = $this->
getValue($this->config[
'scope_key'].
'supported:global');
309 $supportedScope = explode(
' ', (
string) $result);
311 return (count(array_diff($scope, $supportedScope)) == 0);
316 if (is_null($client_id) || !$result = $this->
getValue($this->config[
'scope_key'].
'default:'.$client_id)) {
317 $result = $this->
getValue($this->config[
'scope_key'].
'default:global');
323 public function setScope($scope, $client_id =
null, $type =
'supported')
325 if (!in_array($type, array(
'default',
'supported'))) {
326 throw new \InvalidArgumentException(
'"$type" must be one of "default", "supported"');
329 if (is_null($client_id)) {
330 $key = $this->config[
'scope_key'].$type.
':global';
332 $key = $this->config[
'scope_key'].$type.
':'.$client_id;
335 return $this->
setValue($key, $scope);
341 if (!$jwt = $this->
getValue($this->config[
'jwt_key'] . $client_id)) {
345 if (isset($jwt[
'subject']) && $jwt[
'subject'] == $subject ) {
354 return $this->
setValue($this->config[
'jwt_key'] . $client_id, array(
356 'subject' => $subject
367 if (isset($clientDetails[
'scope'])) {
368 return $clientDetails[
'scope'];
374 public function getJti($client_id, $subject, $audience, $expiration, $jti)
377 throw new \Exception(
'getJti() for the Cassandra driver is currently unimplemented.');
380 public function setJti($client_id, $subject, $audience, $expiration, $jti)
383 throw new \Exception(
'setJti() for the Cassandra driver is currently unimplemented.');
389 $public_key = $this->
getValue($this->config[
'public_key_key'] . $client_id);
390 if (is_array($public_key)) {
391 return $public_key[
'public_key'];
393 $public_key = $this->
getValue($this->config[
'public_key_key']);
394 if (is_array($public_key)) {
395 return $public_key[
'public_key'];
401 $public_key = $this->
getValue($this->config[
'public_key_key'] . $client_id);
402 if (is_array($public_key)) {
403 return $public_key[
'private_key'];
405 $public_key = $this->
getValue($this->config[
'public_key_key']);
406 if (is_array($public_key)) {
407 return $public_key[
'private_key'];
413 $public_key = $this->
getValue($this->config[
'public_key_key'] . $client_id);
414 if (is_array($public_key)) {
415 return $public_key[
'encryption_algorithm'];
417 $public_key = $this->
getValue($this->config[
'public_key_key']);
418 if (is_array($public_key)) {
419 return $public_key[
'encryption_algorithm'];
429 if (!is_array($userDetails)) {
433 $claims = explode(
' ', trim($claims));
434 $userClaims = array();
437 $validClaims = explode(
' ', self::VALID_CLAIMS);
438 foreach ($validClaims
as $validClaim) {
439 if (in_array($validClaim, $claims)) {
440 if ($validClaim ==
'address') {
442 $userClaims[
'address'] = $this->
getUserClaim($validClaim, $userDetails[
'address'] ?: $userDetails);
444 $userClaims = array_merge($userClaims, $this->
getUserClaim($validClaim, $userDetails));
454 $userClaims = array();
455 $claimValuesString = constant(sprintf(
'self::%s_CLAIM_VALUES', strtoupper($claim)));
456 $claimValues = explode(
' ', $claimValuesString);
458 foreach ($claimValues
as $value) {
459 if ($value ==
'email_verified') {
460 $userClaims[$value] = $userDetails[$value]==
'true' ?
true :
false;
462 $userClaims[$value] = isset($userDetails[$value]) ? $userDetails[$value] :
null;