FOSOAuthServer:使用HWIOAUTHBUNDLE时无法在api中检索访问令牌

时间:2014-11-06 12:53:11

标签: symfony oauth-2.0 hwioauthbundle fosoauthserverbundle

将FOSOAuthServerBundle用作OAuthServer,将HWIOAuthBundle用作OAuth客户端时,这两个应用程序都具有FOSUSerBundle集成。

我在检索访问令牌方面遇到了问题,访问令牌将用于向客户端应用程序发送用户响应。

请有人帮我解决。

<?php


public function userAction(Request $request)
{

    $user = $this->get('security.context')->getToken()->getUser();
    if($user) {
        $user = $this->getDoctrine()->getRepository('EparUserBundle:User')->find(
            $this->get('security.context')->getToken()->getUser()
        );
        return new JsonResponse(array(
            'id' => $user->getId(),
            'username' => $user->getUsername(),
            'email' => $user->getEmail()
        ));
    }

    return new JsonResponse(array(
        'message' => 'User is not identified'
    ));

}

下面的FosOAuthServer config.yml和security.yml:

fos_oauth_server:

    db_driver: orm       # Driver availables: orm, mongodb, or propel

    client_class:        Epar\Bundle\UserBundle\Entity\Client
    access_token_class:  Epar\Bundle\UserBundle\Entity\AccessToken
    refresh_token_class: Epar\Bundle\UserBundle\Entity\RefreshToken
    auth_code_class:     Epar\Bundle\UserBundle\Entity\AuthCode
    service:
          storage:                fos_oauth_server.storage.default
          user_provider:          fos_user.user_manager
          client_manager:         fos_oauth_server.client_manager.default
          access_token_manager:   fos_oauth_server.access_token_manager.default
          refresh_token_manager:  fos_oauth_server.refresh_token_manager.default
          auth_code_manager:      fos_oauth_server.auth_code_manager.default
          options:
              # Prototype
              #key:                []

              # Example
              # supported_scopes: string

              # Changing tokens and authcode lifetime
              access_token_lifetime: 3600
              refresh_token_lifetime: 1209600
              auth_code_lifetime: 30
              supported_scopes: user
              # Token type to respond with. Currently only "Bearer" supported.
              #token_type: string

              #realm:

              # Enforce redirect_uri on input for both authorize and token steps.
              #enforce_redirect: true or false

              # Enforce state to be passed in authorization (see RFC 6749, section 10.12)
              #enforce_state: true or false
    template:
        engine:                 twig

Security.yml

# app/config/security.yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        administration:
            switch_user: true
            context:     user
            pattern:     /administration*
            form_login:
                provider:     fos_userbundle
                login_path:   /administration/login
                check_path:   /administration/login_check
                failure_path: /administration/login
                default_target_path: /administration/
                use_forward:  false
                use_referer:  true
                always_use_default_target_path: true

            logout:
                path:   /administration/logout
                target: /administration/login
            anonymous: ~
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
            logout:       true
            anonymous:    true
        oauth_token:
            pattern:    ^/oauth/v2/token
            security:   false

        oauth_authorize:
            pattern:    ^/oauth/v2/auth
            form_login:
                provider: fos_userbundle
                check_path: /oauth/v2/auth_login_check
                login_path: /oauth/v2/auth_login
                use_referer: true
            anonymous: true
            context: connect
            # Add your favorite authentication process here

        api:
            pattern:    ^/api
            fos_oauth:  true
            stateless:  true
            anonymous:  true # can be omitted as its default value


    access_control:
        - { path: ^/administration/login, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/administration/login_check, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api, roles: [ IS_AUTHENTICATED_ANONYMOUSLY ] }
        - { path: "/administration.*", role: ROLE_ADMIN }

HWIOAuthBundle应用程序config.yml&amp; security.yml

config.yml

hwi_oauth:

    http_client:
        timeout:       10 # Time in seconds, after library will shutdown request, by default: 5
        verify_peer:   false # Setting allowing you to turn off SSL verification, by default: true
        ignore_errors: false # Setting allowing you to easier debug request errors, by default: true
        max_redirects: 1 # Number of HTTP redirection request after which library will shutdown request,
                         # by default: 5
    #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:
#       confirmation: true
        account_connector: my_user_provider
    # name of the firewall in which this bundle is active, this setting MUST be set
    firewall_name: main
    fosub:
        username_iterations: 30
        properties:
            # these properties will be used/redefined later in the custom FOSUBUserProvider service.
            github: githubID
            atssso: atsssoID
    resource_owners:
        github:
            type:                github
            client_id:           *******
            client_secret:       ********
            scope:               "user:email"
        atssso:
            type: oauth2
            client_id : 4_1u2nw1clcdy8o4kk84o004s0000oo0kkkw8ow8sg8koowo0c4c
            client_secret: v5sa4t4sylcgsgkg8cosws4400k4s0okg48cgc8ccgk8sg4o4
            access_token_url:    http://192.168.11.71/atssso/web/app_dev.php/oauth/v2/token
            authorization_url:   http://192.168.11.71/atssso/web/app_dev.php/oauth/v2/auth
            infos_url:           http://192.168.11.71/atssso/web/app_dev.php/api/user
            user_response_class: HWI\Bundle\OAuthBundle\OAuth\Response\PathUserResponse
            scope:               "user"
            paths:
                identifier: id
                nickname:   username
                #realname:   ["first_name", "last_name"]
                realname: username
                email:      email
    # here you will add one (or more) configurations for resource owners

security.yml

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_USER

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

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

        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
                login_path: /login
                check_path: /login_check
            oauth:
                resource_owners:
                    github:           "/login/check-github"
                    atssso: "/login/service/atssso"
                login_path:        /login
                failure_path:      /login
                oauth_user_provider:
                    #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.)
                    service: my_user_provider
            logout:       true
            anonymous:    true

        login:
            pattern:  ^/login$
            security: false

            remember_me:
                key: "%secret%"
                lifetime: 31536000 # 365 days in seconds
                path: /
                domain: ~ # Defaults to the current domain from $_SERVER

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }

0 个答案:

没有答案