GeniusesOfSymfony / WebSocketBundle

:part_alternation_mark: Websocket server for Symfony applications (powered by Ratchet), includes a Autobahn.JS based JavaScript client
MIT License
609 stars 140 forks source link

FOSUserBundle authenticated user displayed as anonymous #202

Closed TonyGao closed 7 years ago

TonyGao commented 7 years ago

Hi, there

Environment: Symfony 2.8.14, "friendsofsymfony/user-bundle": "~2.0@dev", "gos/web-socket-bundle": "^1.8"

After authenticated by fosuserbundle, I found I can't get user by user name with clientManipulator. Configuration as below

gos_web_socket:
    server:
        port: 8080
        host: 127.0.0.1
        router:
            resources:
                - @SundayForumBundle/Resources/config/pubsub/routing.yml
    client:
        firewall: main
        session_handler: @session.handler.pdo
        storage:
            driver: @gos_web_socket.server.in_memory.client_storage.driver
            decorator: ~
    pushers:
        wamp:
            host: 127.0.0.1
            port: 8080
fos_user:
    db_driver: orm
    firewall_name: main
    user_class: Sunday\UserBundle\Entity\User
    use_listener: false
    registration:
        form:
            type: Sunday\UserBundle\Form\Type\RegistrationType
services:
    twig.text_extension:
        class: Twig_Extensions_Extension_Text
        tags:
            - name: twig.extension
    pdo:
        class: PDO
        arguments:
            dsn: mysql:host=%database_host%;dbname=%database_name%
            user: %database_user%
            password: %database_password%
        calls:
            - [ setAttribute, [3, 2] ] # \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION
    session.handler.pdo:
        class: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
        arguments: [@pdo, {lock_mode: 0, db_table: sunday_session}]
security:

    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_ADMIN: ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager
                default_target_path: /
                use_referer: true
            logout: true
            anonymous: true

    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 }

dump($this->clientManipulator);

#authenticationProvider: Gos\Bundle\WebSocketBundle\Client\Auth\WebsocketAuthenticationProvider {#499
    #tokenStorage: Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage {#497
      -token: Symfony\Component\Security\Core\Authentication\Token\AnonymousToken {#940
        -secret: "main"
        -user: "anon-15094988995847ce353bac0404228404"
        -roles: []
        -authenticated: true
        -attributes: []
      }
    }
    #firewalls: array:1 [
      0 => "main"
    ]
    #logger: Symfony\Bridge\Monolog\Logger {#501}
    #clientStorage: Gos\Bundle\WebSocketBundle\Client\ClientStorage {#502}

gos:websocket:server command output

2016-12-07 17:11:49] websocket.INFO: anon-20026516845847d24e4ffcd253729173 disconnected {"connection_id":852,"session_id":"20026516845847d24e4ffcd253729173","storage_id":852,"username":"anon-20026516845847d24e4ffcd253729173"}
[2016-12-07 17:11:49] websocket.DEBUG: INSERT CLIENT 857 {"user":"s:37:\"anon-21354439775847d2557fb64849730617\";"}
[2016-12-07 17:11:49] websocket.INFO: anon-21354439775847d2557fb64849730617 connected {"connection_id":857,"session_id":"21354439775847d2557fb64849730617","storage_id":857}
[2016-12-07 17:11:49] websocket.DEBUG: GET CLIENT 857
[2016-12-07 17:11:49] websocket.INFO: anon-21354439775847d2557fb64849730617 subscribe to sunday/igor
[2016-12-07 17:11:49] websocket.DEBUG: Matched route "igor_topic"
[2016-12-07 17:11:49] websocket.DEBUG: GET CLIENT 857
[2016-12-07 17:11:49] websocket.INFO: anon-21354439775847d2557fb64849730617 publish to sunday/igor
[2016-12-07 17:11:49] websocket.DEBUG: Matched route "igor_topic"
[2016-12-07 17:11:49] websocket.DEBUG: GET CLIENT 857

Any idea? What's wrong with it?

TonyGao commented 7 years ago

Well, I have a try with WebsocketAppDemo, and I just use memory for authentication, the problem exist as well. By the way It's Symfony 3.2.

security:
    providers:
        in_memory:
            memory:
                users:
                    tony:
                        password: 123
                        roles: 'ROLE_USER'

Test source code here WebsocketAppDemo-test

It's hard to trace the problem, maybe WebsocketAuthenticationProvider can't get the right Session, and the $connection->Session is empty

nash4ever commented 7 years ago

Hi, I faced the same problem, the solution was in my case, that I had to set gos_web_socket host the same as my site url. ( if you access to your website from demosite.dev and you register websocket to demosite.dev:8080, it will work ).

TonyGao commented 7 years ago

@nash4ever you're right, I fix it, actually I config it as domain in the yml, but it still not work. Finally, as Jack said, Javascript client must connect to the domain too, but not 127.0.0.1 even if it's resolved by /etc/hosts. It's just because the bundle provide a mechanism to protect itself for cross domain websocket call, that's a great feature. Close the issue :) thx