5 use Aws\DynamoDb\DynamoDbClient;
44 OpenIDAuthorizationCodeInterface
51 if (!($connection instanceof DynamoDbClient)) {
52 if (!is_array($connection)) {
53 throw new \InvalidArgumentException(
'First argument to OAuth2\Storage\Dynamodb must be an instance a configuration array containt key, secret, region');
55 if (!array_key_exists(
"key",$connection) || !array_key_exists(
"secret",$connection) || !array_key_exists(
"region",$connection) ) {
56 throw new \InvalidArgumentException(
'First argument to OAuth2\Storage\Dynamodb must be an instance a configuration array containt key, secret, region');
58 $this->client = DynamoDbClient::factory(array(
59 'key' => $connection[
"key"],
60 'secret' => $connection[
"secret"],
61 'region' =>$connection[
"region"]
64 $this->client = $connection;
67 $this->config = array_merge(array(
68 'client_table' =>
'oauth_clients',
69 'access_token_table' =>
'oauth_access_tokens',
70 'refresh_token_table' =>
'oauth_refresh_tokens',
71 'code_table' =>
'oauth_authorization_codes',
72 'user_table' =>
'oauth_users',
73 'jwt_table' =>
'oauth_jwt',
74 'scope_table' =>
'oauth_scopes',
75 'public_key_table' =>
'oauth_public_keys',
82 $result = $this->client->getItem(array(
83 "TableName"=> $this->config[
'client_table'],
84 "Key" => array(
'client_id' => array(
'S' => $client_id))
87 return $result->count()==1 && $result[
"Item"][
"client_secret"][
"S"] == $client_secret;
92 $result = $this->client->getItem(array(
93 "TableName"=> $this->config[
'client_table'],
94 "Key" => array(
'client_id' => array(
'S' => $client_id))
97 if ($result->count()==0) {
101 return empty($result[
"Item"][
"client_secret"]);
107 $result = $this->client->getItem(array(
108 "TableName"=> $this->config[
'client_table'],
109 "Key" => array(
'client_id' => array(
'S' => $client_id))
111 if ($result->count()==0) {
114 $result = $this->dynamo2array($result);
115 foreach (array(
'client_id',
'client_secret',
'redirect_uri',
'grant_types',
'scope',
'user_id')
as $key => $val) {
116 if (!array_key_exists ($val, $result)) {
117 $result[$val] =
null;
124 public function setClientDetails($client_id, $client_secret =
null, $redirect_uri =
null, $grant_types =
null, $scope =
null, $user_id =
null)
126 $clientData = compact(
'client_id',
'client_secret',
'redirect_uri',
'grant_types',
'scope',
'user_id');
127 $clientData = array_filter($clientData,
function ($value) {
return !is_null($value); });
129 $result = $this->client->putItem(array(
130 'TableName' => $this->config[
'client_table'],
131 'Item' => $this->client->formatAttributes($clientData)
140 if (isset($details[
'grant_types'])) {
141 $grant_types = explode(
' ', $details[
'grant_types']);
143 return in_array($grant_type, (array) $grant_types);
153 $result = $this->client->getItem(array(
154 "TableName"=> $this->config[
'access_token_table'],
155 "Key" => array(
'access_token' => array(
'S' => $access_token))
157 if ($result->count()==0) {
160 $token = $this->dynamo2array($result);
161 if (array_key_exists (
'expires', $token)) {
162 $token[
'expires'] = strtotime($token[
'expires']);
168 public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope =
null)
171 $expires = date(
'Y-m-d H:i:s', $expires);
173 $clientData = compact(
'access_token',
'client_id',
'user_id',
'expires',
'scope');
174 $clientData = array_filter($clientData,
function ($value) {
return !
empty($value); });
176 $result = $this->client->putItem(array(
177 'TableName' => $this->config[
'access_token_table'],
178 'Item' => $this->client->formatAttributes($clientData)
187 $result = $this->client->deleteItem(array(
188 'TableName' => $this->config[
'access_token_table'],
189 'Key' => $this->client->formatAttributes(array(
"access_token" => $access_token))
198 $result = $this->client->getItem(array(
199 "TableName"=> $this->config[
'code_table'],
200 "Key" => array(
'authorization_code' => array(
'S' => $code))
202 if ($result->count()==0) {
205 $token = $this->dynamo2array($result);
206 if (!array_key_exists(
"id_token", $token )) {
207 $token[
'id_token'] =
null;
209 $token[
'expires'] = strtotime($token[
'expires']);
215 public function setAuthorizationCode($authorization_code, $client_id, $user_id, $redirect_uri, $expires, $scope =
null, $id_token =
null)
218 $expires = date(
'Y-m-d H:i:s', $expires);
220 $clientData = compact(
'authorization_code',
'client_id',
'user_id',
'redirect_uri',
'expires',
'id_token',
'scope');
221 $clientData = array_filter($clientData,
function ($value) {
return !
empty($value); });
223 $result = $this->client->putItem(array(
224 'TableName' => $this->config[
'code_table'],
225 'Item' => $this->client->formatAttributes($clientData)
234 $result = $this->client->deleteItem(array(
235 'TableName' => $this->config[
'code_table'],
236 'Key' => $this->client->formatAttributes(array(
"authorization_code" => $code))
245 if ($user = $this->
getUser($username)) {
254 return $this->
getUser($username);
264 $claims = explode(
' ', trim($claims));
265 $userClaims = array();
268 $validClaims = explode(
' ', self::VALID_CLAIMS);
269 foreach ($validClaims
as $validClaim) {
270 if (in_array($validClaim, $claims)) {
271 if ($validClaim ==
'address') {
273 $userClaims[
'address'] = $this->
getUserClaim($validClaim, $userDetails[
'address'] ?: $userDetails);
275 $userClaims = array_merge($userClaims, $this->
getUserClaim($validClaim, $userDetails));
285 $userClaims = array();
286 $claimValuesString = constant(sprintf(
'self::%s_CLAIM_VALUES', strtoupper($claim)));
287 $claimValues = explode(
' ', $claimValuesString);
289 foreach ($claimValues
as $value) {
290 if ($value ==
'email_verified') {
291 $userClaims[$value] = $userDetails[$value]==
'true' ?
true :
false;
293 $userClaims[$value] = isset($userDetails[$value]) ? $userDetails[$value] :
null;
303 $result = $this->client->getItem(array(
304 "TableName"=> $this->config[
'refresh_token_table'],
305 "Key" => array(
'refresh_token' => array(
'S' => $refresh_token))
307 if ($result->count()==0) {
310 $token = $this->dynamo2array($result);
311 $token[
'expires'] = strtotime($token[
'expires']);
316 public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope =
null)
319 $expires = date(
'Y-m-d H:i:s', $expires);
321 $clientData = compact(
'refresh_token',
'client_id',
'user_id',
'expires',
'scope');
322 $clientData = array_filter($clientData,
function ($value) {
return !
empty($value); });
324 $result = $this->client->putItem(array(
325 'TableName' => $this->config[
'refresh_token_table'],
326 'Item' => $this->client->formatAttributes($clientData)
334 $result = $this->client->deleteItem(array(
335 'TableName' => $this->config[
'refresh_token_table'],
336 'Key' => $this->client->formatAttributes(array(
"refresh_token" => $refresh_token))
345 return $user[
'password'] == sha1($password);
350 $result = $this->client->getItem(array(
351 "TableName"=> $this->config[
'user_table'],
352 "Key" => array(
'username' => array(
'S' => $username))
354 if ($result->count()==0) {
357 $token = $this->dynamo2array($result);
358 $token[
'user_id'] = $username;
363 public function setUser($username, $password, $first_name =
null, $last_name =
null)
366 $password = sha1($password);
368 $clientData = compact(
'username',
'password',
'first_name',
'last_name');
369 $clientData = array_filter($clientData,
function ($value) {
return !is_null($value); });
371 $result = $this->client->putItem(array(
372 'TableName' => $this->config[
'user_table'],
373 'Item' => $this->client->formatAttributes($clientData)
383 $scope = explode(
' ', $scope);
384 $scope_query = array();
386 foreach ($scope
as $key => $val) {
387 $result = $this->client->query(array(
388 'TableName' => $this->config[
'scope_table'],
390 'KeyConditions' => array(
392 'AttributeValueList' => array(array(
'S' => $val)),
393 'ComparisonOperator' =>
'EQ'
397 $count += $result[
'Count'];
400 return $count == count($scope);
406 $result = $this->client->query(array(
407 'TableName' => $this->config[
'scope_table'],
408 'IndexName' =>
'is_default-index',
409 'Select' =>
'ALL_ATTRIBUTES',
410 'KeyConditions' => array(
411 'is_default' => array(
412 'AttributeValueList' => array(array(
'S' =>
'true')),
413 'ComparisonOperator' =>
'EQ',
417 $defaultScope = array();
418 if ($result->count() > 0) {
419 $array = $result->toArray();
420 foreach ($array[
"Items"]
as $item) {
421 $defaultScope[] = $item[
'scope'][
'S'];
424 return empty($defaultScope) ?
null : implode(
' ', $defaultScope);
433 $result = $this->client->getItem(array(
434 "TableName"=> $this->config[
'jwt_table'],
435 "Key" => array(
'client_id' => array(
'S' => $client_id),
'subject' => array(
'S' => $subject))
437 if ($result->count()==0) {
440 $token = $this->dynamo2array($result);
442 return $token[
'public_key'];
451 if (isset($clientDetails[
'scope'])) {
452 return $clientDetails[
'scope'];
458 public function getJti($client_id, $subject, $audience, $expires, $jti)
463 public function setJti($client_id, $subject, $audience, $expires, $jti)
472 $result = $this->client->getItem(array(
473 "TableName"=> $this->config[
'public_key_table'],
474 "Key" => array(
'client_id' => array(
'S' => $client_id))
476 if ($result->count()==0) {
479 $token = $this->dynamo2array($result);
481 return $token[
'public_key'];
487 $result = $this->client->getItem(array(
488 "TableName"=> $this->config[
'public_key_table'],
489 "Key" => array(
'client_id' => array(
'S' => $client_id))
491 if ($result->count()==0) {
494 $token = $this->dynamo2array($result);
496 return $token[
'private_key'];
501 $result = $this->client->getItem(array(
502 "TableName"=> $this->config[
'public_key_table'],
503 "Key" => array(
'client_id' => array(
'S' => $client_id))
505 if ($result->count()==0) {
508 $token = $this->dynamo2array($result);
510 return $token[
'encryption_algorithm'];
518 private function dynamo2array($dynamodbResult)
521 foreach ($dynamodbResult[
"Item"]
as $key => $val) {
522 $result[$key] = $val[
"S"];
523 $result[] = $val[
"S"];