如何限制谁可以获得有效的访问令牌?

时间:2014-10-06 11:45:26

标签: symfony oauth fosuserbundle fosrestbundle fosoauthserverbundle

我开发了一项APIRest服务来使用移动应用中的数据。我已经使用了Symfony2软件包:FOSOAuthServerBundle,FOSUserBundle和FOSRestBundle,所有工作正常,但我需要区分管理面板用户和api用户,因为现在如果我尝试通过应用程序管理员进行身份验证,我将获得一个令牌但我只想与api用户一起获得一个有效的。

例如,对于下一个用户,我应该获得带有第二个请求的有效令牌和带有第一个请求的无效授权响应,但我获得了包含它们的有效令牌:

adminuser(ROLE_ADMIN)

https://app.myweb.com/oauth/v2/token?username=adminuser&password=12345&client_id=...&client_secret=...&grant_type=password

appuser(ROLE_APP)

https://app.myweb.com/oauth/v2/token?username=appuser&password=12345&client_id=...&client_secret=...&grant_type=password

非常感谢你帮我解决这个问题,对不起我的英语。

1 个答案:

答案 0 :(得分:0)

来自FOSOAuthServerBundle文档:

https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/blob/master/Resources/doc/index.md#step-5-configure-fosoauthserverbundle

创建用户提供程序并将其添加到fos_oauth_server配置:

fos_oauth_server:
    ...
    service:
        user_provider: my_custom_user_provider_service

创建提供程序,扩展Symfony \ Component \ Security \ Core \ User \ UserProviderInterface接口,并检查用户是否已授予ROLE_APP。记录在这里:

http://symfony.com/doc/current/cookbook/security/custom_provider.html