Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[help needed] Authentication Verification inside websocket handler for room #895

Closed
rebootcode opened this issue Feb 2, 2018 · 3 comments

Comments

@rebootcode
Copy link

rebootcode commented Feb 2, 2018

First of all, I am always a fan of this amazing framework. Thanks to @kataras who is been devoting so much his time to build such an amazing piece of code. I am learning golang every day using this framework and it is such a major boost to confidence.

While coding for my app, I can not solve myself on how I can verify users inside WebSocket handler for Room/group chat.

Since ctx response cannot be used inside WebSocket handler. I am confused to figure out, how verification can be done for users chatting in the room whether they are allowed to chat or not in given room.

My Code :-

// initial code like iris.New(), session, db and other code skipped

ws := websocket.New(websocket.Config{})
app.Get("/ws", ws.Handler())
app.Any("/iris-ws.js", func(ctx context.Context) {
    ctx.Write(websocket.ClientSource)
})
app.Get("/", func(ctx context.Context){
    ctx.View("chat.html")
})
ws.OnConnection(func(c websocket.Connection) {
    var curRoom = "library"
    c.On("join", func(room string) {
        c.Join(room)
    })
    c.On("chat", func(messages string) {
        var token map[string]string
        // other code like join room on page open and receive message
        // I need verification hook here to check whether `user` is allowed to send message in this room or not.
        c.To(curRoom).Emit("chat", "Message here.....")
    })
})

As you can see above, it does not verify where dat["name"] is allowed to chat on given room or not. I need tips/help to add a hook for limiting room to chat between only allowed users without compromising the performance 😞

I am using iris 8.5 while writing this code and inside my app.

@kataras
Copy link
Owner

kataras commented Feb 3, 2018

First of all @rebootcode thanks for your nice words about Iris and your contribution with your questions, bug reporting and more over the last months, I really appreciate good devs with meanful questions.

If you don't join the client to a room manually it will not be able to send a message, even if you c.To(room).Emit.. therefore I want to ask you, why you need to verify if "x" client is joined to a room? (It's easy, we can add a function which will return true/false for a connection client joined to a specific room name, is that the case?)

Note:

Please setup the OnConnection event before the ws.Handler() call.

@kataras
Copy link
Owner

kataras commented Feb 3, 2018

IsJoined added on the Connection, upgrade from 8.5 to 10 with go get -u github.com/kataras/iris. Read our HISTORY.md, they are not any serious breaking changes so you should be fine. Please test it and write down the results, thank you @rebootcode !

@speedwheel
Copy link
Contributor

speedwheel commented Feb 3, 2018

@corebreaker I just want to add that you can access the ctx inside the websocket handler by doing:
ctx := c.Context()

github-actions bot pushed a commit to goproxies/github.com-kataras-iris that referenced this issue Jul 27, 2020
Former-commit-id: 560fc8b911a9c1352be577d2f7bebd1fac7b5d4a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants