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

Make channel tokens be compatible with Centrifugo v4+ #24

Merged
merged 1 commit into from
Oct 31, 2024

Conversation

trushkevich
Copy link
Contributor

Hello. I have discovered that if rubycent version 3.0.0 is being used against Centrifugo server versions 4+ (tested with centrifugo server 4.1.5 and 5.4.6, and centrifuge v5.2.2 nodejs module) then channel tokens issued with the help of client parameter do not allow subscribing to channels. Debug log in centrifugo server in this case looks like this:

2024-10-30 17:57:12 [INF] starting Centrifugo engine=memory gomaxprocs=8 pid=237467 runtime=go1.23.2 version=5.4.6
2024-10-30 17:57:12 [INF] using config file path=/home/centrifugo/config.json
2024-10-30 17:57:12 [INF] enabled JWT verifiers algorithms="HS256, HS384, HS512"
2024-10-30 17:57:12 [INF] serving websocket, api, admin endpoints on :8000
2024-10-30 18:13:22 [DBG] http request addr=[::1]:42912 duration="94.323µs" method=GET path=/connection/websocket status=101
2024-10-30 18:13:22 [DBG] client connection established client=10fd032f-697d-469d-90a9-79983e79f70a transport=websocket
2024-10-30 18:13:22 [DBG] client authenticated client=10fd032f-697d-469d-90a9-79983e79f70a user=user__33
2024-10-30 18:13:22 [INF] token user mismatch channel=foo client=10fd032f-697d-469d-90a9-79983e79f70a tokenUser= user=user__33
2024-10-30 18:13:22 [INF] token user mismatch channel=$user#user__33 client=10fd032f-697d-469d-90a9-79983e79f70a tokenUser= user=user__33
2024-10-30 18:13:22 [DBG] client connection completed client=10fd032f-697d-469d-90a9-79983e79f70a duration=268.267833ms transport=websocket
2024-10-30 18:13:22 [DBG] closing client connection client=10fd032f-697d-469d-90a9-79983e79f70a reason="invalid token" user=user__33

After applying changes in this PR (so basically you just need to use sub instead of client in payload sent to JWT.encode, just like for connection tokens) I can successfully subscribe to channels:

2024-10-30 18:37:06 [DBG] http request addr=[::1]:59212 duration="86.397µs" method=GET path=/connection/websocket status=101
2024-10-30 18:37:06 [DBG] client connection established client=9a4d9a8b-0de0-4c9c-bf76-823f7a88ffa7 transport=websocket
2024-10-30 18:37:06 [DBG] client authenticated client=9a4d9a8b-0de0-4c9c-bf76-823f7a88ffa7 user=user__33
2024-10-30 18:37:06 [DBG] client subscribed to channel channel=foo client=9a4d9a8b-0de0-4c9c-bf76-823f7a88ffa7 user=user__33
2024-10-30 18:37:06 [DBG] client subscribed to channel channel=$user#user__33 client=9a4d9a8b-0de0-4c9c-bf76-823f7a88ffa7 user=user__33

If everything is ok and I'm not missing anything, could you please review, merge the PR and release a new version to rubygems ?

@trushkevich
Copy link
Contributor Author

trushkevich commented Oct 30, 2024

ok, I see that there are issues further (e.g. publishing doesn't work), closing

@FZambia
Copy link
Member

FZambia commented Oct 31, 2024

@trushkevich hello!

Actually PR seems totally correct - token generator should be updated to set sub instead of client for latest Centrifugo versions. Note, you can use any other library to generate Centrifugo client tokens, usually in other server API SDKs we avoid providing methods to generate tokens letting users to just use any JWT library of their choice.

Regarding publishing you mentioned – what error you have? I think rubycent should be compatible with server publishing API of Centrifugo.

@trushkevich
Copy link
Contributor Author

@FZambia

Regarding publishing you mentioned – what error you have?

it looks like I just didn't notice that endpoint url should now include /api, after updating it messages are being published successfully

reopened

@trushkevich trushkevich reopened this Oct 31, 2024
@FZambia
Copy link
Member

FZambia commented Oct 31, 2024

Many thanks!! Merging, will release a bit later - currently vacationing 🌴

@FZambia FZambia merged commit cee3653 into centrifugal:master Oct 31, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants