Skip to content

Commit cee3653

Browse files
authored
Make subscription tokens compatible with Centrifugo v4+ (centrifugal#24)
1 parent f57a372 commit cee3653

3 files changed

Lines changed: 10 additions & 10 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ All channels starting with $ considered private and require a **channel token**
7171
Private channel subscription token is also JWT([see the claims](https://centrifugal.github.io/centrifugo/server/private_channels/))
7272

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

7676
#=> "eyJhbGciOiJIUzI1NiJ9..."
7777
```

lib/cent/notary.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def issue_connection_token(sub:, info: nil, exp: nil)
5757

5858
# Generate JWT for private channels
5959
#
60-
# @param client [String]
61-
# Client ID which wants to subscribe on channel
60+
# @param sub [String]
61+
# Standard JWT claim which must contain an ID of current application user.
6262
#
6363
# @option channel [String]
6464
# Channel that client tries to subscribe to (string).
@@ -71,16 +71,16 @@ def issue_connection_token(sub:, info: nil, exp: nil)
7171
# client connection that can be provided for Centrifugo.
7272
#
7373
# @example Get private channel JWT with expiration and extra info
74-
# notary.issue_channel_token(client: 'client', channel: 'channel', exp: 3600, info: { 'message' => 'wat' })
74+
# notary.issue_channel_token(sub: '1', channel: 'channel', exp: 3600, info: { 'message' => 'wat' })
7575
# #=> eyJhbGciOiJIUzI1NiJ9.eyJjbGllbnQiOiJjbG..."
7676
#
7777
# @see (https://centrifugal.github.io/centrifugo/server/private_channels/)
7878
#
7979
# @return [String]
8080
#
81-
def issue_channel_token(client:, channel:, info: nil, exp: nil)
81+
def issue_channel_token(sub:, channel:, info: nil, exp: nil)
8282
payload = {
83-
'client' => client,
83+
'sub' => sub,
8484
'channel' => channel,
8585
'info' => info,
8686
'exp' => exp

spec/cent/notary_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,21 @@
8383

8484
describe '#issue_channel_token' do
8585
subject(:channel_token) do
86-
notary.issue_channel_token(client: 'client', channel: 'channel', info: { 'foo' => 'bar' })
86+
notary.issue_channel_token(sub: '1', channel: 'channel', info: { 'foo' => 'bar' })
8787
end
8888

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

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

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

100-
it { expect(channel_token).to match(/^eyJhbGciOiJIUzI1NiJ9.*uQCqas$/) }
100+
it { expect(channel_token).to match(/^eyJhbGciOiJIUzI1NiJ9.*yht5hk$/) }
101101
end
102102

103103
context 'with RSA key' do

0 commit comments

Comments
 (0)