2016-02-14 23:20:10 +05:30
|
|
|
<?php
|
2016-11-27 03:13:42 +05:30
|
|
|
namespace api\components\OAuth2\Storage;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2016-12-18 04:50:53 +05:30
|
|
|
use api\components\OAuth2\Entities\ClientEntity;
|
2016-11-27 03:13:42 +05:30
|
|
|
use api\components\OAuth2\Entities\ScopeEntity;
|
2016-02-14 23:20:10 +05:30
|
|
|
use common\models\OauthScope;
|
|
|
|
use League\OAuth2\Server\Storage\AbstractStorage;
|
|
|
|
use League\OAuth2\Server\Storage\ScopeInterface;
|
2016-12-18 04:50:53 +05:30
|
|
|
use yii\base\ErrorException;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
class ScopeStorage extends AbstractStorage implements ScopeInterface {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function get($scope, $grantType = null, $clientId = null) {
|
2016-12-18 04:50:53 +05:30
|
|
|
$query = OauthScope::find();
|
|
|
|
if ($grantType === 'authorization_code') {
|
|
|
|
$query->onlyPublic()->usersScopes();
|
|
|
|
} elseif ($grantType === 'client_credentials') {
|
|
|
|
$query->machineScopes();
|
|
|
|
$isTrusted = false;
|
|
|
|
if ($clientId !== null) {
|
|
|
|
$client = $this->server->getClientStorage()->get($clientId);
|
|
|
|
if (!$client instanceof ClientEntity) {
|
|
|
|
throw new ErrorException('client storage must return instance of ' . ClientEntity::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
$isTrusted = $client->isTrusted();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$isTrusted) {
|
|
|
|
$query->onlyPublic();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scopes = $query->all();
|
2016-12-10 02:12:07 +05:30
|
|
|
if (!in_array($scope, $scopes, true)) {
|
2016-02-14 23:20:10 +05:30
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$entity = new ScopeEntity($this->server);
|
2016-11-27 21:49:13 +05:30
|
|
|
$entity->setId($scope);
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|