filsh / yii2-oauth2-server

A wrapper for implementing an OAuth2 Server(https://github.com/bshaffer/oauth2-server-php)
MIT License
332 stars 167 forks source link

Error trying to get JWT #118

Closed jonmunm closed 7 years ago

jonmunm commented 7 years ago

Hi Everyone

I'm trying to change the token type to JWT, and I've been following the instructions given here (https://github.com/Filsh/yii2-oauth2-server), so I created the files (In fact, I just copy them):

  1. app\storage\PublicKeyStorage
  2. app\storage\JwtAccessToken

Then in my config file I have something like this:

    'modules' => [
        'oauth2' => [
            'class' => 'filsh\yii2\oauth2server\Module',            
            'tokenParamName' => 'accessToken',
            'tokenAccessLifetime' => 3600,
            'useJwtAccessTokens' => true,
            'storageMap' => [
                'user_credentials' => 'app\models\User',
                'public_key' => 'app\storage\PublicKeyStorage',
                'access_token' => 'app\storage\JwtAccessToken'
            ],
            'grantTypes' => [
                'user_credentials' => [
                    'class' => 'OAuth2\GrantType\UserCredentials',
                ],
                'client_credentials' => [
                    'class' => 'OAuth2\GrantType\ClientCredentials',
                ],
                'refresh_token' => [
                    'class' => 'OAuth2\GrantType\RefreshToken',
                    'always_issue_new_refresh_token' => true
                ]
            ]
        ],

But when I send a request, I got this error:

Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: filsh\yii2\oauth2server\Module::useJwtAccessTokens

So, after some research I decide to remove de 'useJwtAccessTokens' => true from the config. Then I put it in vendor\filsh\yii2-oauth2-server\Module.php. The code with the new line is like this (just a fragment of it):

    public function getServer()
    {
        if(!$this->has('server')) {
            $storages = [];
            foreach(array_keys($this->storageMap) as $name) {
                $storages[$name] = \Yii::$container->get($name);
            }

            $grantTypes = [];
            foreach($this->grantTypes as $name => $options) {
                if(!isset($storages[$name]) || empty($options['class'])) {
                    throw new \yii\base\InvalidConfigException('Invalid grant types configuration.');
                }

                $class = $options['class'];
                unset($options['class']);

                $reflection = new \ReflectionClass($class);
                $config = array_merge([0 => $storages[$name]], [$options]);

                $instance = $reflection->newInstanceArgs($config);
                $grantTypes[$name] = $instance;
            }

            $server = \Yii::$container->get(Server::className(), [
                $this,
                $storages,
                [
                    'token_param_name' => $this->tokenParamName,
                    'access_lifetime' => $this->tokenAccessLifetime,
                    'use_jwt_access_tokens' => true,
                ],
                $grantTypes
            ]);

            $this->set('server', $server);
        }
        return $this->get('server');
    }

Note where I put the option 'use_jwt_access_tokens' => true. After this, I got stuck with a new final error that says:

{
  "name": "Not instantiable",
  "message": "Can not instantiate OAuth2\\Storage\\PublicKeyInterface.",
  "code": 0,
  "type": "yii\\di\\NotInstantiableException",
  "file": "/media/sf_desarrollos/qualitat_api/yii_basic/basic/vendor/yiisoft/yii2/di/Container.php",
  "line": 370,
  "stack-trace": [
    "#0 /media/sf_desarrollos/qualitat_api/yii_basic/basic/vendor/yiisoft/yii2/di/Container.php(154): yii\\di\\Container->build('OAuth2\\Storage\\...', Array, Array)",
    "#1 /media/sf_desarrollos/qualitat_api/yii_basic/basic/vendor/yiisoft/yii2/di/Container.php(454): yii\\di\\Container->get('OAuth2\\Storage\\...')",
    "#2 /media/sf_desarrollos/qualitat_api/yii_basic/basic/vendor/yiisoft/yii2/di/Container.php(368): yii\\di\\Container->resolveDependencies(Array, Object(ReflectionClass))",
    "#3 /media/sf_desarrollos/qualitat_api/yii_basic/basic/vendor/yiisoft/yii2/di/Container.php(154): yii\\di\\Container->build('app\\storage\\Jwt...', Array, Array)",
    "#4 /media/sf_desarrollos/qualitat_api/yii_basic/basic/vendor/yiisoft/yii2/di/Container.php(172): yii\\di\\Container->get('app\\storage\\Jwt...', Array, Array)",
    "#5 /media/sf_desarrollos/qualitat_api/yii_basic/basic/vendor/filsh/yii2-oauth2-server/Module.php(80): yii\\di\\Container->get('access_token')",
    "#6 /media/sf_desarrollos/qualitat_api/yii_basic/basic/vendor/filsh/yii2-oauth2-server/controllers/RestController.php(25): filsh\\yii2\\oauth2server\\Module->getServer()",
    "#7 [internal function]: filsh\\yii2\\oauth2server\\controllers\\RestController->actionToken()",
    "#8 /media/sf_desarrollos/qualitat_api/yii_basic/basic/vendor/yiisoft/yii2/base/InlineAction.php(55): call_user_func_array(Array, Array)",
    "#9 /media/sf_desarrollos/qualitat_api/yii_basic/basic/vendor/yiisoft/yii2/base/Controller.php(154): yii\\base\\InlineAction->runWithParams(Array)",
    "#10 /media/sf_desarrollos/qualitat_api/yii_basic/basic/vendor/yiisoft/yii2/base/Module.php(454): yii\\base\\Controller->runAction('token', Array)",
    "#11 /media/sf_desarrollos/qualitat_api/yii_basic/basic/vendor/yiisoft/yii2/web/Application.php(100): yii\\base\\Module->runAction('oauth2/rest/tok...', Array)",
    "#12 /media/sf_desarrollos/qualitat_api/yii_basic/basic/vendor/yiisoft/yii2/base/Application.php(375): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))",
    "#13 /media/sf_desarrollos/qualitat_api/yii_basic/basic/web/index.php(12): yii\\base\\Application->run()",
    "#14 {main}"
  ]
}

The only guess I can make is tha something happen with the indicated interface.

I Hope you can help me, or tell me how to proper set the config to use JWT.

Bye

mtangoo commented 7 years ago

Can you try this? https://github.com/hosannahighertech/yii2-oauth2-server

jonmunm commented 7 years ago

Thanks, working

mtangoo commented 7 years ago

What was the issue and what solved it?

jonmunm commented 7 years ago

What do you mean??

mtangoo commented 7 years ago

Don't mind, I saw your other post now I get it!