1

我一直在按照这里的说明进行操作:https ://gist.github.com/danvbe/4476697 ,我已经不止一次地阅读了整个线程,但我没有找到解决问题的方法。我想将 oauth 捆绑包仅用于帐户链接,从 oauth 提供者持久保存用户数据。我的用户将不会使用 oauth 进行身份验证。尽管如此,我已经实现了整个事情,看看它是否可以与 github 作为提供者一起使用,但没有。我可以进入授权页面,但是当我点击允许访问时,我不可避免地会重定向到登录页面并出现此错误No oauth code in the request。如果停止使用自定义的 FOSUBUserProvider 并更改为默认的 HWI ,那么我会在 Github 中注册应用程序,但无法保存数据。

重要提示:我尝试从 HWI 完全复制 FOSUBUserProvider 并且仍然存在相同的问题,因此可能与它的实现无关,但可能与服务定义或配置有关。任何帮助是极大的赞赏。

这些是相关文件:

FOSUBUserProvider.php

class FOSUBUserProvider extends BaseClass
{
    /**
     * {@inheritDoc}
     */
    public function connect(UserInterface $user, UserResponseInterface $response)
    {
        $property = $this->getProperty($response);
        $username = $response->getUsername();

        //on connect - get the access token and the user ID
        $service = $response->getResourceOwner()->getName();

        $setter = 'set'.ucfirst($service);
        $setter_id = $setter.'Id';
        $setter_token = $setter.'AccessToken';

        //we "disconnect" previously connected users
        if (null !== $previousUser = $this->userManager->findUserBy(array($property => $username))) {
            $previousUser->$setter_id(null);
            $previousUser->$setter_token(null);
            $this->userManager->updateUser($previousUser);
        }

        //we connect current user
        $user->$setter_id($username);
        $user->$setter_token($response->getAccessToken());

        $this->userManager->updateUser($user);
    }

    /**
     * {@inheritdoc}
     */
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
    {
        $username = $response->getUsername();
        $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
        //when the user is registrating
        if (null === $user) {
            $service = $response->getResourceOwner()->getName();
            $setter = 'set'.ucfirst($service);
            $setter_id = $setter.'Id';
            $setter_token = $setter.'AccessToken';
            // create new user here
            $user = $this->userManager->createUser();
            $user->$setter_id($username);
            $user->$setter_token($response->getAccessToken());
            //I have set all requested data with the user's username
            //modify here with relevant data
            $user->setUsername($username);
            $user->setEmail($username);
            $user->setPassword($username);
            $user->setEnabled(true);
            $this->userManager->updateUser($user);
            return $user;
        }

        //if user exists - go with the HWIOAuth way
        $user = parent::loadUserByOAuthUserResponse($response);

        $serviceName = $response->getResourceOwner()->getName();
        $setter = 'set' . ucfirst($serviceName) . 'AccessToken';

        //update access token
        $user->$setter($response->getAccessToken());

        return $user;
    }

}

配置.yml

hwi_oauth:
    #this is my custom user provider, created from FOSUBUserProvider - will manage the
    #automatic user registration on your site, with data from the provider (facebook. google, etc.)
    #and also, the connecting part (get the token and the user_id)
    connect:
        account_connector: custom.user.provider
    # name of the firewall in which this bundle is active, this setting MUST be set
    firewall_name: main

    # optional FOSUserBundle integration
    fosub:
        # try 30 times to check if a username is available (foo, foo1, foo2 etc)
        username_iterations: 30

        # mapping between resource owners (see below) and properties
        properties:
            github: githubId

    # optional HTTP Client configuration
    http_client:
        verify_peer:   false

    resource_owners:
        github:
            type:                 github
            client_id:            xxxxxxxxxxxxxxxxxxxxxx
            client_secret:        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            scope:                "repo, delete_repo, notifications, gist"
            options:
                csrf:             true

安全.yml

providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls: #CAUTION! The order of the firewalls IS ON PURPOSE! DON'T CHANGE!
        # Disabling the security for the web debug toolbar, the profiler and Assetic.
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        # -> custom firewall for the admin area of the URL
        admin:
            pattern:            /admin(.*)
            context:            user
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
            logout:
                path:           /admin/logout
            anonymous:          true

        # -> end custom configuration

        # defaut login area for standard users

        # This firewall is used to handle the public login area
        # This part is handled by the FOS User Bundle
        main:
            pattern:             .*
            context:             user
            form_login:
                provider:       fos_userbundle
                login_path:     /login
                use_forward:    false
                check_path:     /login_check
                failure_path:   null
            logout:             true
            anonymous:          true

            # Login path for OAuth providers
            oauth:
                resource_owners:
                    github:             "/login/check-github"
                    trello:             "/login/check-trello"
                login_path:        /login
                failure_path:      /login

                # FOSUB integration
#                oauth_user_provider:
#                    service: hwi_oauth.user.provider.fosub_bridge
                oauth_user_provider:
                    #this is my custom user provider, created from FOSUBUserProvider - will manage the
                    #automatic user registration on website, with data from the provider (github. trello, etc.)
                    service: custom.user.provider

    access_control:
        # URL of FOSUserBundle which need to be available to anonymous users
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Admin login page needs to be access without credential
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Secured part of the site
        # This config requires being logged for the whole site and having the admin role for the admin part.
        # Change these rules to adapt them to your needs
        - { path: ^/admin/, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
        - { path: ^/.*, role: ROLE_USER } #This is on purpose.

路由.yml

hwi_oauth_security:
    resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
    prefix: /connect

hwi_oauth_connect:
    resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
    prefix: /connect

hwi_oauth_redirect:
    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
    prefix:   /connect

服务.yml

parameters:
    custom.user.provider.class: My\Bundle\Path\Security\Core\User\FOSUBUserProvider

services:
    sonata.admin.user:
        class: My\Bundle\Path\Admin\Model\UserAdmin
        tags:
#            - { name: sonata.admin, manager_type: orm, group: users, label: users, label_translator_strategy: sonata.admin.label.strategy.underscore }
        arguments:
            - ~
            - My\Bundle\Path\Entity\User
            - SonataAdminBundle:CRUD
        calls:
            - [setTranslationDomain, [SonataUserBundle]]
            - [setUserManager, [@fos_user.user_manager]]
            - [setSecurityContext, [@security.context]]

    custom.user.provider:
        class: "%custom.user.provider.class%"
        #this is the place where the properties are passed to the UserProvider - see config.yml
        arguments: [@fos_user.user_manager,{github: github_id, trello: trello_id}]
4

1 回答 1

0

好吧,经过多次尝试和错误,我发现了问题:

Github 中的回调 URL 是:http://mywebsite/login/check-github但那是错误的。事实是我从来没有发现这个值必须设置成什么,所以我在猜测。偶然我发现了正确的 URL:http://mywebsite/connect/service/github适用于我的情况,我的配置。

我有一次在尝试默认 HWI Provider 时发现它,使用浏览器控制台检查重定向。

于 2013-11-14T04:34:55.433 回答