hwi / HWIOAuthBundle

OAuth client integration for Symfony. Supports both OAuth1.0a and OAuth2.
MIT License
2.27k stars 798 forks source link

The service "hwi_oauth.security.oauth_utils" has a dependency on a non-existent service "hwi_oauth.resource_ownermap.secured_area" #598

Closed JonnyD closed 6 years ago

JonnyD commented 10 years ago
ServiceNotFoundException: The service "hwi_oauth.security.oauth_utils" has a dependency on a non-existent service "hwi_oauth.resource_ownermap.secured_area".

Routing

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

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

Security

security:
    encoders:
        DW\DWBundle\Entity\User: plaintext

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

    providers:
        main:
            entity:
                class: DW\DWBundle\Entity\User
                property: username

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

        login:
            pattern:  ^/login$
            security: false

        secured_area:
            pattern:    ^/
            form_login:
                login_path:  login
                check_path:  login_check
                default_target_path: /
                always_use_default_target_path: true
            logout:
                path:   logout
                target: /
            anonymous: ~
            http_basic:
                realm: "Secured Demo Area"

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, roles: ROLE_USER }

Config

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }

framework:
    #esi:             ~
    #translator:      { fallback: "%locale%" }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        # handler_id set to null will use default session handler from php.ini
        handler_id:  ~
    fragments:       ~
    http_method_override: true

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"

# Assetic Configuration
assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    bundles:        [ ]
    #java: /usr/bin/java
    filters:
        cssrewrite: ~
        #closure:
        #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
        #yui_css:
        #    jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        # if using pdo_sqlite as your database driver, add the path in parameters.yml
        # e.g. database_path: "%kernel.root_dir%/data/data.db3"
        # path:     "%database_path%"

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }

hwi_oauth:
    # name of the firewall in which this bundle is active, this setting MUST be set
    firewall_name: secured_area

    # an optional setting to configure a query string parameter which can be used to redirect
    # the user after authentication, e.g. /connect/facebook?_destination=/my/destination will
    # redirect the user to /my/destination after facebook authenticates them.  If this is not
    # set then the user will be redirected to the original resource that they requested, or
    # the base address if no resource was requested.  This is similar to the behaviour of
    # [target_path_parameter for form login](http://symfony.com/doc/2.0/cookbook/security/form_login.html).
    # target_path_parameter: _destination

    # an optional setting to use the HTTP REFERER header to be used in case no
    # previous URL was stored in the session (i.e. no resource was requested).
    # This is similar to the behaviour of
    # [using the referring URL for form login](http://symfony.com/doc/2.0/cookbook/security/form_login.html#using-the-referring-url).
    # use_referer: true

    # here you will add one (or more) configurations for resource owners
    # and other settings you want to adjust in this bundle, just checkout the list below!
    resource_owners:
        any_name:
            type:                facebook
            client_id:           <client_id>
            client_secret:       <client_secret>
stof commented 10 years ago

In your config, you have:

hwi_oauth:
    # name of the firewall in which this bundle is active, this setting MUST be set
    firewall_name: secured_area

but hwi_oauth is not active in your firewall_name firewall

JonnyD commented 10 years ago

What do you mean? I have secured_area in the list of firewalls in security.yml or am I missing something else?

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

        login:
            pattern:  ^/login$
            security: false

        secured_area:
            pattern:    ^/
            form_login:
                login_path:  login
                check_path:  login_check
                default_target_path: /
                always_use_default_target_path: true
            logout:
                path:   logout
                target: /
            anonymous: ~
            http_basic:
                realm: "Secured Demo Area"
stof commented 10 years ago

you have this firewall, but it only has form_login and http_basic as authentication ways, not OAuth

JonnyD commented 10 years ago

Ah I see, I totally overlooked this part of the documentation: https://github.com/hwi/HWIOAuthBundle/blob/master/Resources/doc/3-configuring_the_security_layer.md

Thanks!

eddiejaoude commented 10 years ago

Note: I had a similar error, because I had a mistake in my security.yml config...(firewalls was specified twice).

security:
    providers:
        in_memory:
            memory: ~

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

        default:
            anonymous: ~

    firewalls:
        secured_area:
            anonymous: ~
            oauth:
                resource_owners:
                    my_github:          "/login/check-github"
                login_path:        /login
                use_forward:       false
                failure_path:      /login

                oauth_user_provider:
                    service: hwi_oauth.user.provider

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
caraneilwarner commented 9 years ago

For posterity, I was seeing this error...but only when I tried to run unit tests.

Adding this to a service definition file (facebook.xml) did the trick:

<!-- HWIOAuth has a dynamically-named dependency: a service with id "hwi_oauth.resource_ownermap.%s", 
where %s is the firewall_name assigned to hwi_oauth. The firewall_name we gave hwi_oauth is "main"
(see config.yml). Below, we pre-emptively declare a service named "hwi_oauth.resource_ownermap.main". 
If we don't do this, the service is "non-existent" when unit tests are run, and they fail.-->
<service id="hwi_oauth.resource_ownermap.main"
class="HWI\Bundle\OAuthBundle\Security\Http\ResourceOwnerMap">
     <argument type="service" id="security.http_utils" />
     <argument type="collection"/>
     <argument type="collection"/>
</service>

If you run into the same issue, make sure you replace "main" with whatever your firewall_name is. If it's "secured_area" you'd want to add:

<service id="hwi_oauth.resource_ownermap.secured_area"
class="HWI\Bundle\OAuthBundle\Security\Http\ResourceOwnerMap">...</service>
magicuniverse-azeem commented 6 years ago

Has this issue been resolved? I'm using symfony4 and getting the same error. Followed up the advice from the comments, but no success.

DocMarmel commented 6 years ago

problem "solved" when we add public: true in the ResourceOwnerMap declaration service (as proposed by @carawarner ) in SF 4.0.9

XWB commented 6 years ago

:+1: