Symfony Security: Auth with session or oauth

1.9k views Asked by At

I have developed a REST API, there are two ways to connect to it: session and oauth. Basically, my website will use the session mode and third-party softwares will use the oauth mode.

I managed to make make both session and oauth modes to work in symfony, but I can't make them work at the same time.

Here is my firewalls security config:

firewalls:
    auth_oauth_token:
        pattern:    ^/auth/oauth/v2/token
        security:   false

    api:
        pattern:    ^/api
        anonymous:  false
        fos_oauth:  true
        stateless:  true

    auth:
        pattern:    ^/
        anonymous: ~
        form_login:
            login_path: /auth/session/check
            check_path: /auth/session/login
            always_use_default_target_path: true
            default_target_path: /auth/session/check
            failure_path: /auth/session/check
            failure_forward: false
            use_forward: false
            failure_forward: false
            username_parameter: username
            password_parameter: password
            post_only: true
            remember_me: false
            require_previous_session: false
        logout:
            path: /auth/session/logout
            target: /auth/session/logged_out
            invalidate_session: false

Session handling: /auth/session. OAuth handling: /auth/oauth. Api: /api.

So, with this config, with "api" firewall first, I can log in with a token. But even logged in with a session, if I don't specify the token, I won't have access.

With "auth" firewall first, I can log in with the session form. But even if I specify a token, I won't have access.

I'm getting crazy with this. I found on stack overflow something about chain providers, I would probably need something like "chain firewall"... if forbidden, check another firewall.

Thank you

1

There are 1 answers

1
fdellutri On

I solved by duplicating the routes of the api controllers, so that I have a route /api/method which relies on OAuth2, and a /webapi/method route which relies on the standard (main) firewall:

In security.yml:

firewalls:
    api:
        pattern:    ^/api
        fos_oauth:  true
        stateless:  true

    oauth_token:
        pattern:    ^/oauth/v2/token
        security:   false

    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            login_path: /login
            check_path: /login_check
        logout:       true
        anonymous:    true

access_control:        
    - { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }
    - { path: ^/web-api, roles: [ IS_AUTHENTICATED_FULLY ] }

In routing.yml:

acme_api:
    type: rest 
    prefix: /
    resource: "@AcmeBundle/Resources/config/routing_api.yml"

In routing_api.yml:

# REST API - OAUTH Access
acme_api_users:
    resource: AcmeBundle\Controller\UsersController
    type:     rest
    defaults: {_format: json}
    prefix:   /api
    name_prefix:  api_

# REST API - Frontend Client Access 
acme_webapi_users:
    resource: AcmeBundle\Controller\UsersController
    type:     rest
    defaults: {_format: json}
    prefix:   /web-api
    name_prefix:  webapi_