Coder Social home page Coder Social logo

Comments (16)

 avatar commented on June 28, 2024

+1. Would it be?

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

from yii2-oauth2-server.

vzani avatar vzani commented on June 28, 2024

+1

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

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

from yii2-oauth2-server.

danDanV1 avatar danDanV1 commented on June 28, 2024

Referencing my comment on #39 (comment)

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
                ]
            ]
        ]
    ],

from yii2-oauth2-server.

danDanV1 avatar danDanV1 commented on June 28, 2024

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.

from yii2-oauth2-server.

danDanV1 avatar danDanV1 commented on June 28, 2024

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.

from yii2-oauth2-server.

mtangoo avatar mtangoo commented on June 28, 2024

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

from yii2-oauth2-server.

danDanV1 avatar danDanV1 commented on June 28, 2024

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

from yii2-oauth2-server.

rlmckenney avatar rlmckenney commented on June 28, 2024

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?

from yii2-oauth2-server.

danDanV1 avatar danDanV1 commented on June 28, 2024

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.

from yii2-oauth2-server.

mtangoo avatar mtangoo commented on June 28, 2024

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

from yii2-oauth2-server.

rlmckenney avatar rlmckenney commented on June 28, 2024

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.

from yii2-oauth2-server.

rlmckenney avatar rlmckenney commented on June 28, 2024

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?

from yii2-oauth2-server.

rlmckenney avatar rlmckenney commented on June 28, 2024

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.

from yii2-oauth2-server.

mtangoo avatar mtangoo commented on June 28, 2024

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

from yii2-oauth2-server.

mtangoo avatar mtangoo commented on June 28, 2024

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

from yii2-oauth2-server.

varp avatar varp commented on June 28, 2024

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

from yii2-oauth2-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.