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

Class user_credentials does not exist #84

Closed ivantree closed 4 years ago

ivantree commented 8 years ago

qq 20160408114037

ghost commented 8 years ago

+1. Would it be?

foreach($this->storageMap as $name=>$class) { $storages[$name] = \Yii::$container->get($class); }

vzani commented 8 years ago

+1

foreach($this->storageMap as $name=>$class) { $storages[$name] = \Yii::$container->get($class); }

This solution worked for me, I believe a fix is needed

danDanV1 commented 8 years ago

Referencing my comment on https://github.com/Filsh/yii2-oauth2-server/issues/39#issuecomment-246840160

I'm using dev-master.

JWT tokens as described in the readme aren't in the 2.0 release. They are in dev-master however.

@hiqsol @Filsh I've bootstrapped oauth2 module, but didn't seem to make a difference. Still throwing "Class user_credentials does not exist"

Also tried with bshaffer/oauth2-server-php at 1.8

    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log','oauth2'],
    'controllerNamespace' => 'restapi\controllers',
    'modules' => [
        'oauth2' => [
            'class' => 'filsh\yii2\oauth2server\Module',
            'useJwtToken' => true,
            'tokenParamName' => 'accessToken',
            'tokenAccessLifetime' => 3600 * 24,
            'storageMap' => [
                'user_credentials' => 'restapi\modules\v1\models\ApiUser',
                'public_key' => 'restapi\storage\PublicKeyStorage',
                'access_token' => 'restapi\storage\JwtAccessToken',
            ],
            'grantTypes' => [
                'user_credentials' => [
                    'class' => 'OAuth2\GrantType\UserCredentials',
                ],
                'refresh_token' => [
                    'class' => 'OAuth2\GrantType\RefreshToken',
                    'always_issue_new_refresh_token' => true
                ]
            ]
        ]
    ],
danDanV1 commented 8 years ago

Also tried checking out the last commit to 2.0.1 (#44aa6091ce128c9fd0e81c684044a6b57fbfc8eb)

Did this fix in main.php for user_credentials error foreach($this->storageMap as $name => $class) { $storages[$name] = \Yii::$container->get($class); }

But get Invalid grant types configuration for refresh_token because default storage mappings aren't in there. @mtangoo I saw you took that out in earlier commit.

@mtangoo is your jwt working with dev-master? I don't know what the issue is here.

danDanV1 commented 8 years ago

Ok. My bad. So dev-master has some issues.

I checked out the 2.0.1 branch and JWT is stable and working. No more user_credentials error.

Solution if anyone else is looking: "filsh/yii2-oauth2-server": "2.0.1.x-dev@dev"

Would be desirable if there was a note about this in the 2.0.0 readme that's default when visiting this repo.

mtangoo commented 8 years ago

Sorry I have been busy. I think the old branches need to be wiped out of existence. 2.0.1.x-dev ought to be dev-master and out of it come some release.

Am not the author of the extension and have no access to repo, so can't do changes.

I think I will re-write the extension IF I happen to get free time.

I was experimenting with something here and its not hard to do that https://github.com/hosannahighertech/upendo

danDanV1 commented 8 years ago

That's good to know that I'm on the right branch now. Should be helpful if anyone else finds this thread in the mean time. Thanks for clearing that up @mtangoo

rlmckenney commented 7 years ago

So I used

"filsh/yii2-oauth2-server": "2.0.1.x-dev@dev"

as outlined by @edeis53 above, but I am still getting the dreaded "Class user_credentials does not exist" error. Did you make any other configuration changes?

danDanV1 commented 7 years ago

Worked for me. "filsh/yii2-oauth2-server": "2.0.1.x-dev@dev", You might need to check the readme for that branch, I think it may be different from the master as I recall as part of my confusion. Follow that you should be fine. I am using JWT tokens as well.

mtangoo commented 7 years ago

@rlmckenney did you run composer update? Ca you post composer.json file?

rlmckenney commented 7 years ago

I am not using JWT yet. I wanted to get the base config working. When I ran composer update I got ...

Updating dependencies (including require-dev)
  - Installing bshaffer/oauth2-server-php (v1.8.0)
    Loading from cache

  - Installing filsh/yii2-oauth2-server (v2.0.1.x-dev 44aa609)
    Cloning 44aa6091ce128c9fd0e81c684044a6b57fbfc8eb from cache

and the modules section from my config file is ...

    'modules' => [
        'oauth2' => [
            'class' => 'filsh\yii2\oauth2server\Module',
            'useJwtToken' => false,
            'tokenParamName' => 'accessToken',
            'tokenAccessLifetime' => 3600 * 24,
            'storageMap' => [
                'user_credentials' => 'api\common\models\User',
            ],
            'grantTypes' => [
//                'client_credentials' => [
//                    'class' => 'OAuth2\GrantType\ClientCredentials',
//                    'allow_public_clients' => false
//                ],
                'user_credentials' => [
                    'class' => 'OAuth2\GrantType\UserCredentials',
                ],
                'refresh_token' => [
                    'class' => 'OAuth2\GrantType\RefreshToken',
                    'always_issue_new_refresh_token' => true
                ]
            ]
        ],
        'v2' => [
            'basePath' => '@app/versions/v2',
            'class' => 'api\versions\v2\Module'
        ],

I am rechecking the README but I don't think that I missed anything.

rlmckenney commented 7 years ago

I have tried enabling JWT with

            'useJwtToken' => true,
            'storageMap' => [
                'user_credentials' => 'api\common\models\User',
                'public_key' => 'api\storage\PublicKeyStorage',
                'access_token' => 'OAuth2\Storage\JwtAccessToken',
            ],

and I still have the same problem. What am I missing?

rlmckenney commented 7 years ago

After reading the code and some further experimentation, I was able to get things working. Here is what I did ...

  1. Use "filsh/yii2-oauth2-server": "2.0.1.x-dev@dev" in my composer.json as discussed above.
  2. Update \filsh\yii2\oauth2server\Module.php at line 104 to replace
foreach(array_keys($this->storageMap) as $name) {
    $storages[$name] = \Yii::$container->get($name);
}

with

foreach ($this->storageMap as $name => $class) { 
    $storages[$name] = \Yii::$container->get($class);
}
  1. Add the following methods to the User class so that it implements the \OAuth2\Storage\ClientCredentialsInterface
    /**
     * @inheritdoc
     */
    private function getClient($client_id)
    {
        return \filsh\yii2\oauth2server\models\OauthClients::findOne(['client_id' => $client_id]);
    }

    /**
     * @inheritdoc
     */
    public function getClientDetails($client_id)
    {
        $client = $this->getClient($client_id);
        if (empty($client)) return [];

        return [
            "redirect_uri" => $client->redirect_uri,      // REQUIRED redirect_uri registered for the client
            "client_id"    => $client->client_id,         // OPTIONAL the client id
            "grant_types"  => $client->grant_types,       // OPTIONAL an array of restricted grant types
            "user_id"      => $client->user_id,           // OPTIONAL the user identifier associated with this client
            "scope"        => $client->scope,             // OPTIONAL the scopes allowed for this client
        ];
    }

    /**
     * @inheritdoc
     */
    public function getClientScope($client_id)
    {
        $client = $this->getClient($client_id);
        if (empty($client)) return null;

        return $client->scope;
    }

    /**
     * @inheritdoc
     */
    public function checkRestrictedGrantType($client_id, $grant_type)
    {
        $client = $this->getClient($client_id);
        if (empty($client)) return false;

        $validGrantTypes = explode(" ", $client->grant_types);
        return in_array($grant_type, $validGrantTypes);
    }

    /**
     * @inheritdoc
     */
    public function checkClientCredentials($client_id, $client_secret = null)
    {
        $client = $this->getClient($client_id);
        if (empty($client)) return false;

        return ($client->client_secret == $client_secret);
    }

    /**
     * @inheritdoc
     */
    public function isPublicClient($client_id)
    {
        return true;
    }
  1. Update the modules section of main config to add 'client_credentials' => 'api\common\models\User', to storageMap and remove refresh_token from grantTypes.
    'modules' => [
        'oauth2' => [
            'class' => 'filsh\yii2\oauth2server\Module',
            'tokenParamName' => 'accessToken',
            'tokenAccessLifetime' => 3600 * 3,
            'useJwtToken' => true,
            'storageMap' => [
                'client_credentials' => 'api\common\models\User',
                'user_credentials' => 'api\common\models\User',
                'public_key' => 'api\storage\PublicKeyStorage',
                'access_token' => 'OAuth2\Storage\JwtAccessToken',
            ],
            'grantTypes' => [
//                'client_credentials' => [
//                    'class' => 'OAuth2\GrantType\ClientCredentials',
//                    'allow_public_clients' => false
//                ],
                'user_credentials' => [
                    'class' => 'OAuth2\GrantType\UserCredentials',
                ],
//                'refresh_token' => [
//                    'class' => 'OAuth2\GrantType\RefreshToken',
//                    'always_issue_new_refresh_token' => true
//                ]
            ]
        ],

I hope this additional information helps someone else.

mtangoo commented 7 years ago

Since I have no commit access I'll fork it up and will consider your solution

mtangoo commented 7 years ago

I created a fork of this module as promised. I would like to hear your comments as well as your suggestions. (PR are mostly welcome).

https://github.com/hosannahighertech/yii2-oauth2-server

varp commented 4 years ago

@mtangoo @rlmckenney @vzani @ivantree @danDanV1 the fork mentioned by @mtangoo is archived. By the way, the project support is restored.