yii2-oauth2-server icon indicating copy to clipboard operation
yii2-oauth2-server copied to clipboard

Custom grant_type requires custom storage_type

Open ebuzzz opened this issue 8 years ago • 4 comments

I've been working on a custom grant_type to incorporate OTP in the normal user_credentials grant_type. This requires a storageMap property with the same name. The main problem is that the base library (bshaffes/oauth2) only accepts a few fixed values as keys for the storage map:

So this won't work:

'storageMap' => [
    'user_credentials' => 'common\models\User',
    'user_credentials_otp' => 'common\models\User',
],
'grantTypes' => [
    'user_credentials' => [
        'class' => 'OAuth2\GrantType\UserCredentials',
    ],
    'user_credentials_otp' => [
        'class' => 'common\components\GrantType\UserCredentialsOtp',
    ],
]

It returns the error:

"unknown storage key "user_credentials_otp", must be one of [access_token, authorization_code, client_credentials, client, refresh_token, user_credentials, user_claims, public_key, jwt_bearer, scope]"

I've forked the library and added a custom storageName property to the grantType configuration. This will allow me to override the storageMap key for a specific grantType.

'storageMap' => [
    'user_credentials' => 'common\models\User',
    'user_credentials_otp' => 'common\models\User',
],
'grantTypes' => [
    'user_credentials' => [
        'class' => 'OAuth2\GrantType\UserCredentials',
    ],
    'user_credentials_otp' => [
        'class' => 'common\components\GrantType\UserCredentialsOtp',
        'storageName' => 'user_credentials'
    ],
]

Am I the only one having this problem? And would this be something we can add to the library? It's backwards compatible and I think the only way to support this right now.

ebuzzz avatar Aug 23 '16 08:08 ebuzzz

See pull #102 for my fix to this problem.

ebuzzz avatar Aug 23 '16 09:08 ebuzzz

@eborned I am having the same issue when trying to implement my own grantTypes. However, your https://github.com/Filsh/yii2-oauth2-server/pull/102 seems not working, it still returns the same "unknown storage key" error.

yang-he avatar May 06 '17 11:05 yang-he

Can you show your config?

On May 6, 2017 13:07, "Yang He" [email protected] wrote:

@eborned https://github.com/eborned I am having the same issue when trying to implement my own grantTypes. However, your #102 https://github.com/Filsh/yii2-oauth2-server/pull/102 seems not working, it still returns the same "unknown storage key" error.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Filsh/yii2-oauth2-server/issues/101#issuecomment-299632497, or mute the thread https://github.com/notifications/unsubscribe-auth/AD4OYpzz3Au30mdnc7lD6TgWmfomUuD3ks5r3FRigaJpZM4Jqq13 .

ebuzzz avatar May 06 '17 11:05 ebuzzz

Hi @eborned, please see my config in below, it is actually same as you demonstrated above.

'modules' => [
        'oauth2' => [
            'class' => 'filsh\yii2\oauth2server\Module',
            'tokenParamName' => 'access_token',
            'tokenAccessLifetime' => 3600*24,
            'storageMap' => [
                'user_credentials' => 'api\common\models\User',
                'user_credentials_custom' => 'api\common\models\User',
            ],
            'grantTypes' => [
                'user_credentials' => [
                    'class' => 'OAuth2\GrantType\UserCredentials',
                ],
                'user_credentials_custom' => [
                    'class' => 'api\components\OAuth2\GrantType\WechatAppCode',
                    'storageName' => 'user_credentials_custom',
                ],
                'refresh_token' => [
                    'class' => 'OAuth2\GrantType\RefreshToken',
                    'always_issue_new_refresh_token' => true,
                ],
            ],
        ],
        ...
        ...
    ],

yang-he avatar May 06 '17 16:05 yang-he