具有FOSUserBundle的FOSOAuthServerBundle-如何使其工作? [英] FOSOAuthServerBundle with FOSUserBundle - How to make it works?

查看:75
本文介绍了具有FOSUserBundle的FOSOAuthServerBundle-如何使其工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我的项目效果很好.我使用FOSUserBundle来管理用户.现在,我想实现OAuth,所以我正在使用FOSOAuthServerBundle.大多数开发人员推荐使用该捆绑软件来实现OAuth.

我遵循了FOSOAuthServerBundle的文档.通常,我必须在security.yml中添加更多信息,但我不知道该怎么做...

这是我的安全性.yml:

security:
    encoders:
       Symfony\Component\Security\Core\User\User: plaintext
       Moodress\Bundle\UserBundle\Entity\User: sha512

    role_hierarchy:
       ROLE_ADMIN:       ROLE_USER
       ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
       main:
           id: fos_user.user_provider.username

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false
        oauth_token:
            pattern:    ^/oauth/v2/token
            security:   false

        oauth_authorize:
            pattern:    ^/oauth/v2/auth

        main:
            pattern: ^/
            fos_oauth:  true
            stateless:  true
            anonymous: true

我想防火墙中可能要添加一些信息?

我真的不知道如何使用FOSUserBundle使FOSOAuthServerBundle起作用.以前,仅使用FOSUserBundle,我使用了登录表单和FOSUserBundle的登录检查.现在,我已经放置了FOSOAuthServerBundle的所有基本配置,接下来我要做什么?我应该使用哪种形式?哪个登录检查?令牌是由FOSOAuthServerBundle自动创建的吗?在文档中,它们显示了如何创建客户端...我应该在我的项目中添加此代码吗?如果是...在哪里? :/

我在网上找到了这篇文章: http://blog.logicexception .com/2012/04/securing-syfmony2-rest-service-wiith.html

我试图实现这一点,但是我不敢相信我们需要添加所有这些文件才能使其正常工作...

如果有人知道如何使FSOUserBundle和FOSOAuthServerBundle一起工作,那将非常有帮助.

解决方案

我刚刚安装了此捆绑包并开始使用它.

我认为您需要首先了解有关OAuth身份验证工作原理的更多信息.

通过这种方式,您将了解FOSUserBundle机制与OAuth并不完全相同.

您的链接是正确设置捆绑软件的最佳信息.

我正在使用MongoDB存储所有4个必需的文档:客户端,AuthCode,RefreshToken和AccessToken

称为创建新客户端"的步骤基本上是FOSUserBundle for OAuth的注册"过程.

OAuth将使用客户端授予访问权限.

OAuth的主要思想是保护API,因此建议您将配置切换为匿名:false

然后您将看到以下消息:

{"error":"access_denied","error_description":"OAuth2 authentication required"}

调用API时

OAuth的想法是获取访问令牌以调用您的API. 阅读此内容: http://blog.tankist.de/blog/2013/07/16/oauth2-explained-part-1-principles-and-terminology/

这是需要遵循OAuth身份验证过程的时间.

有5种基本方法可供使用:

const GRANT_TYPE_AUTH_CODE = 'authorization_code';
const GRANT_TYPE_IMPLICIT = 'token';
const GRANT_TYPE_USER_CREDENTIALS = 'password';
const GRANT_TYPE_CLIENT_CREDENTIALS = 'client_credentials';
const GRANT_TYPE_REFRESH_TOKEN = 'refresh_token';

要了解每种信息,请查找有关OAuth RFC的更多文档.

它们每个都对应于对的特定调用: /oauth/v2/token?client_id = [CLIENT_ID]& response_type = code& redirect_uri = URL& grant_type = token

Cf: https://github .com/FriendsOfSymfony/oauth2-php/blob/master/lib/OAuth2/OAuth2.php#L182

也请阅读此链接:

我还在努力.

希望有帮助.


此链接还指示如何使用FOSUserBundle User& UserManager可能使用密码grant_type:如果您要对用户进行身份验证,请不要忘记设置用户提供程序.

下面是使用FOSUserBundle用户提供程序的示例: https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/blob/master/Resources/doc/index.md

# app/config/config.yml
fos_oauth_server:
    ...

    service:
        user_provider: fos_user.user_manager

Currently my project works very well. I use FOSUserBundle for the management of my users. Now, I want to implement OAuth, so I'm using FOSOAuthServerBundle. Most of developers recommend this bundle for implement OAuth.

I followed the documentation of FOSOAuthServerBundle. Normally, I have to add more information in my security.yml but I don't know exactly what I have to do ...

Here is my security.yml :

security:
    encoders:
       Symfony\Component\Security\Core\User\User: plaintext
       Moodress\Bundle\UserBundle\Entity\User: sha512

    role_hierarchy:
       ROLE_ADMIN:       ROLE_USER
       ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
       main:
           id: fos_user.user_provider.username

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false
        oauth_token:
            pattern:    ^/oauth/v2/token
            security:   false

        oauth_authorize:
            pattern:    ^/oauth/v2/auth

        main:
            pattern: ^/
            fos_oauth:  true
            stateless:  true
            anonymous: true

I guess that there are some information to add in firewalls maybe ??

I really don't know how to make works FOSOAuthServerBundle with FOSUserBundle. Before, with just FOSUserBundle, I used the login form and the login check of FOSUserBundle. Now that I put all the basic configuration of FOSOAuthServerBundle, what I have to do next ? Which form should I use? Which login check? The token is created automatically by FOSOAuthServerBundle ? In the documentation, they show how to create a client... Am I supposed to add this code in my project ? If yes... where ? :/

I found this article on the web : http://blog.logicexception.com/2012/04/securing-syfmony2-rest-service-wiith.html

I tried to implement this, but I can't believe that we need to add all this files to make it work...

If someone knows how to make works FOSOAuthServerBundle with FOSUserBundle, it would be very helpful.

解决方案

I've just installed this bundle and started playing with it.

I think you need to learn first more about how OAuth authentication works.

This way you will understand that the FOSUserBundle mechanisms are not exactly the same as OAuth.

Your link is the best piece of information to setup correctly the bundle.

I'm using MongoDB to store all the 4 required documents : Client, AuthCode, RefreshToken and AccessToken

The step called "Create a new client" is basically the "register" process of FOSUserBundle for OAuth.

OAuth will use the client to give permission to access.

The main idea of OAuth is to secure an API, therefore I suggest you switch your config to anonymous: false

Then you'll see the message :

{"error":"access_denied","error_description":"OAuth2 authentication required"}

when you call your API

The idea of OAuth is to get an Access Token to call your API. Read this : http://blog.tankist.de/blog/2013/07/16/oauth2-explained-part-1-principles-and-terminology/

This is when the OAuth authentication process needs to be followed.

There are 5 basic methods to use :

const GRANT_TYPE_AUTH_CODE = 'authorization_code';
const GRANT_TYPE_IMPLICIT = 'token';
const GRANT_TYPE_USER_CREDENTIALS = 'password';
const GRANT_TYPE_CLIENT_CREDENTIALS = 'client_credentials';
const GRANT_TYPE_REFRESH_TOKEN = 'refresh_token';

To learn about each, go find more documentation about OAuth RFC.

Each of them correspond to a specific call to : /oauth/v2/token?client_id=[CLIENT_ID]&response_type=code&redirect_uri=URL&grant_type=token

Cf: https://github.com/FriendsOfSymfony/oauth2-php/blob/master/lib/OAuth2/OAuth2.php#L182

Also read this link : blog.tankist.de/blog/2013/08/20/oauth2-explained-part-4-implementing-custom-grant-type-symfony2-fosoauthserverbundle/

The part "Time to test" explains how to use OAuth.

I'm still working on it.

Hope it helps.


Also this link indicates how to use FOSUserBundle User & UserManager probably to use the password grant_type : If you're authenticating users, don't forget to set the user provider.

Here's an example using the FOSUserBundle user provider: https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/blob/master/Resources/doc/index.md

# app/config/config.yml
fos_oauth_server:
    ...

    service:
        user_provider: fos_user.user_manager

这篇关于具有FOSUserBundle的FOSOAuthServerBundle-如何使其工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆