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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ All channels starting with $ considered private and require a **channel token**
Private channel subscription token is also JWT([see the claims](https://centrifugal.github.io/centrifugo/server/private_channels/))

```ruby
notary.issue_channel_token(client: 'client', channel: 'channel', exp: 1629050099, info: { scope: 'admin' })
notary.issue_channel_token(sub: '42', channel: 'channel', exp: 1629050099, info: { scope: 'admin' })

#=> "eyJhbGciOiJIUzI1NiJ9..."
```
Expand Down
10 changes: 5 additions & 5 deletions lib/cent/notary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def issue_connection_token(sub:, info: nil, exp: nil)

# Generate JWT for private channels
#
# @param client [String]
# Client ID which wants to subscribe on channel
# @param sub [String]
# Standard JWT claim which must contain an ID of current application user.
#
# @option channel [String]
# Channel that client tries to subscribe to (string).
Expand All @@ -71,16 +71,16 @@ def issue_connection_token(sub:, info: nil, exp: nil)
# client connection that can be provided for Centrifugo.
#
# @example Get private channel JWT with expiration and extra info
# notary.issue_channel_token(client: 'client', channel: 'channel', exp: 3600, info: { 'message' => 'wat' })
# notary.issue_channel_token(sub: '1', channel: 'channel', exp: 3600, info: { 'message' => 'wat' })
# #=> eyJhbGciOiJIUzI1NiJ9.eyJjbGllbnQiOiJjbG..."
#
# @see (https://centrifugal.github.io/centrifugo/server/private_channels/)
#
# @return [String]
#
def issue_channel_token(client:, channel:, info: nil, exp: nil)
def issue_channel_token(sub:, channel:, info: nil, exp: nil)
payload = {
'client' => client,
'sub' => sub,
'channel' => channel,
'info' => info,
'exp' => exp
Expand Down
8 changes: 4 additions & 4 deletions spec/cent/notary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,21 @@

describe '#issue_channel_token' do
subject(:channel_token) do
notary.issue_channel_token(client: 'client', channel: 'channel', info: { 'foo' => 'bar' })
notary.issue_channel_token(sub: '1', channel: 'channel', info: { 'foo' => 'bar' })
end

let(:notary) { described_class.new(secret: 'secret') }

context 'with no expiration' do
it { expect(channel_token).to match(/^eyJhbGciOiJIUzI1NiJ9.*SopvNY$/) }
it { expect(channel_token).to match(/^eyJhbGciOiJIUzI1NiJ9.*50TnzY$/) }
end

context 'with expiration' do
subject(:channel_token) do
notary.issue_channel_token(client: 'client', channel: 'channel', info: { 'foo' => 'bar' }, exp: 1_628_877_060)
notary.issue_channel_token(sub: '1', channel: 'channel', info: { 'foo' => 'bar' }, exp: 1_628_877_060)
end

it { expect(channel_token).to match(/^eyJhbGciOiJIUzI1NiJ9.*uQCqas$/) }
it { expect(channel_token).to match(/^eyJhbGciOiJIUzI1NiJ9.*yht5hk$/) }
end

context 'with RSA key' do
Expand Down