/**
  * Persists a new access token to permanent storage.
  *
  * @param \League\OAuth2\Server\Entities\AccessTokenEntityInterface $accessTokenEntity
  * @return mixed
  */
 public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
 {
     $db = CommonModel::getDb();
     $transaction = $db->beginTransaction();
     try {
         $accessTokenModel = new AccessTokensModel();
         $accessTokenModel->id = $accessTokenEntity->getIdentifier();
         $accessTokenModel->expire_time = $accessTokenEntity->getExpiryDateTime()->getTimestamp();
         $accessTokenModel->user_id = $accessTokenEntity->getUserIdentifier();
         $accessTokenModel->client_id = $accessTokenEntity->getClient()->getIdentifier();
         if (!$accessTokenModel->save()) {
             return false;
         }
         foreach ($accessTokenEntity->getScopes() as $item) {
             $accessTokenScopesModel = new AccessTokenScopesModel();
             $accessTokenScopesModel->access_token_id = $accessTokenModel->id;
             $accessTokenScopesModel->scope_id = $item->getIdentifier();
             $accessTokenScopesModel->save();
         }
         $transaction->commit();
         return true;
     } catch (\Exception $e) {
         $transaction->rollBack();
         return false;
     }
 }
 /**
  * @inheritDoc
  */
 public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
 {
     $as = $this->config->getString('auth');
     $auth = new \SimpleSAML_Auth_Simple($as);
     // We should be authenticated so this returns the session user attributes (or [] if not)
     $attributes = $auth->getAttributes();
     $scopes = [];
     foreach ($accessTokenEntity->getScopes() as $scope) {
         $scopes[] = $scope->getIdentifier();
     }
     $this->conn->insert($this->getTableName(), ['id' => $accessTokenEntity->getIdentifier(), 'scopes' => $scopes, 'attributes' => $attributes, 'expires_at' => $accessTokenEntity->getExpiryDateTime(), 'user_id' => $accessTokenEntity->getUserIdentifier(), 'client_id' => $accessTokenEntity->getClient()->getIdentifier()], ['string', 'json_array', 'json_array', 'datetime', 'string', 'string']);
 }
 /**
  * Persists a new access token to permanent storage.
  *
  * @param \League\OAuth2\Server\Entities\AccessTokenEntityInterface|AccessTokenEntity $accessTokenEntity
  */
 public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
 {
     $accessTokenModel = $this->modelResolver->getModel('AccessTokenModel');
     $newAccessToken = ['token' => $accessTokenEntity->getIdentifier(), 'client_id' => $accessTokenEntity->getClient()->getIdentifier(), 'expire_time' => $accessTokenEntity->getExpiryDateTime()];
     if (!is_null($accessTokenEntity->getUserIdentifier())) {
         $newAccessToken['user_id'] = $accessTokenEntity->getUserIdentifier();
     }
     if (!is_null($accessTokenEntity->getName())) {
         $newAccessToken['name'] = $accessTokenEntity->getName();
     }
     if ($accessTokenEntity->getScopes() !== []) {
         $scopes = array_map(function ($Scope) {
             /* @var ScopeEntity $Scope */
             return $Scope->getIdentifier();
         }, $accessTokenEntity->getScopes());
         if ($accessTokenModel::$canHandleArray) {
             $newAccessToken['scopes'] = $scopes;
         } else {
             $newAccessToken['scopes'] = json_encode($scopes);
         }
     }
     $accessTokenModel::create($newAccessToken);
 }
Example #4
0
 /**
  * Persists a new access token to permanent storage.
  *
  * @param \League\OAuth2\Server\Entities\Interfaces\AccessTokenEntityInterface $accessTokenEntity
  **/
 public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
 {
     $scopes = [];
     foreach ($accessTokenEntity->getScopes() as $scope) {
         $scopes[] = \Ivyhjk\OAuth2\Server\Adapter\Phalcon\Model\Scope::findFirst(['conditions' => 'id = :id:', 'bind' => ['id' => $scope->getIdentifier()]]);
     }
     // prx($accessTokenEntity->getExpiryDateTime()->format('Y-m-d H:i:s'));
     $accessToken = new \Ivyhjk\OAuth2\Server\Adapter\Phalcon\Model\AccessToken();
     $accessToken->id = $accessTokenEntity->getIdentifier();
     $accessToken->user_id = $accessTokenEntity->getUserIdentifier();
     $accessToken->client_id = $accessTokenEntity->getClient()->getIdentifier();
     $accessToken->expire_time = $accessTokenEntity->getExpiryDateTime()->format('Y-m-d H:i:s');
     $accessToken->scopes = $scopes;
     $accessToken->save();
 }