Skip to content

Commit

Permalink
Make subscription tokens compatible with Centrifugo v4+ (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
trushkevich authored Oct 31, 2024
1 parent f57a372 commit cee3653
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
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

0 comments on commit cee3653

Please sign in to comment.