Пример #1
0
 /**
  * Create a brand new Access Token by params
  * @param $client_id
  * @param $scope
  * @param $audience
  * @param null $user_id
  * @return AccessToken
  */
 public function createAccessTokenFromParams($client_id, $scope, $audience, $user_id = null)
 {
     $access_token = AccessToken::createFromParams($scope, $client_id, $audience, $user_id, $this->configuration_service->getConfigValue('OAuth2.AccessToken.Lifetime'));
     $cache_service = $this->cache_service;
     $client_service = $this->client_service;
     $auth_service = $this->auth_service;
     $this_var = $this;
     $this->tx_service->transaction(function () use($client_id, $scope, $audience, $user_id, &$access_token, &$this_var, &$cache_service, &$client_service, &$auth_service) {
         $value = $access_token->getValue();
         $hashed_value = Hash::compute('sha256', $value);
         $this_var->storesAccessTokenOnCache($access_token);
         $client_id = $access_token->getClientId();
         $client = $client_service->getClientById($client_id);
         //stores in DB
         $access_token_db = new DBAccessToken(array('value' => $hashed_value, 'from_ip' => IPHelper::getUserIp(), 'lifetime' => $access_token->getLifetime(), 'scope' => $access_token->getScope(), 'audience' => $access_token->getAudience()));
         $access_token_db->client()->associate($client);
         if (!is_null($user_id)) {
             $user = $auth_service->getUserById($user_id);
             $access_token_db->user()->associate($user);
         }
         $access_token_db->Save();
         //stores brand new access token hash value on a set by client id...
         $cache_service->addMemberSet($client_id . TokenService::ClientAccessTokenPrefixList, $hashed_value);
         $cache_service->incCounter($client_id . TokenService::ClientAccessTokensQty, TokenService::ClientAccessTokensQtyLifetime);
     });
     return $access_token;
 }