From b9e9688a8e1175c0848f27a5cb743a2be43d2da4 Mon Sep 17 00:00:00 2001 From: Anton Trushkevich Date: Wed, 30 Oct 2024 19:13:52 +0100 Subject: [PATCH] Make channel tokens be compatible with Centrifugo v4+ --- README.md | 2 +- lib/cent/notary.rb | 10 +++++----- spec/cent/notary_spec.rb | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b63b648..28e808b 100644 --- a/README.md +++ b/README.md @@ -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..." ``` diff --git a/lib/cent/notary.rb b/lib/cent/notary.rb index dbcd80f..99f279b 100644 --- a/lib/cent/notary.rb +++ b/lib/cent/notary.rb @@ -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). @@ -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 diff --git a/spec/cent/notary_spec.rb b/spec/cent/notary_spec.rb index b8de0db..10b8051 100644 --- a/spec/cent/notary_spec.rb +++ b/spec/cent/notary_spec.rb @@ -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