cent 2.2.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/cent/notary.rb CHANGED
@@ -6,84 +6,93 @@ require 'cent/error'
6
6
  module Cent
7
7
  # Cent::Notary
8
8
  #
9
- # Handle token generation
9
+ # Issues JWT tokens for Centrifugo client connections and channel subscriptions.
10
+ # Supports HMAC, RSA and ECDSA families of algorithms (HS256/384/512,
11
+ # RS256/384/512, ES256/384/512).
10
12
  #
13
+ # @see https://centrifugal.dev/docs/server/authentication
14
+ # @see https://centrifugal.dev/docs/server/channel_token_auth
11
15
  class Notary
12
- # @param secret [String | OpenSSL::PKey::RSA | OpenSSL::PKey::EC] Secret key for the algorithm of your choice.
13
- # @param algorithm [String] Specify algorithm(one from HMAC, RSA or ECDSA family). Default is HS256.
14
- #
15
- # @example Construct new client instance
16
- # notary = Cent::Notary.new(secret: 'secret')
17
- #
16
+ # @param secret [String, OpenSSL::PKey::RSA, OpenSSL::PKey::EC] Secret key
17
+ # for the chosen algorithm. For HMAC pass the raw secret as a String. For
18
+ # RSA/ECDSA pass a PEM-loaded {OpenSSL::PKey::RSA} / {OpenSSL::PKey::EC}.
19
+ # @param algorithm [String] JWT algorithm, defaults to `HS256`.
18
20
  def initialize(secret:, algorithm: 'HS256')
19
21
  raise Error, 'Secret can not be nil' if secret.nil?
20
22
 
21
- @secret = secret
23
+ @secret = secret
22
24
  @algorithm = algorithm
23
25
  end
24
26
 
25
- # Generate connection JWT for the given user
26
- #
27
- # @param sub [String]
28
- # Standard JWT claim which must contain an ID of current application user.
29
- #
30
- # @option channel [String]
31
- # Channel that client tries to subscribe to (string).
32
- #
33
- # @param exp [Integer]
34
- # (default: nil) UNIX timestamp seconds when token will expire.
35
- #
36
- # @param info [Hash]
37
- # (default: {}) This claim is optional - this is additional information about
38
- # client connection that can be provided for Centrifugo.
39
- #
40
- # @example Get user JWT with expiration and extra info
41
- # notary.issue_connection_token(sub: '1', exp: 3600, info: { 'role' => 'admin' })
42
- # #=> "eyJhbGciOiJIUzI1NiJ9.eyJzdWIi..."
43
- #
44
- # @see (https://centrifugal.github.io/centrifugo/server/authentication/)
45
- #
46
- # @return [String]
47
- #
48
- def issue_connection_token(sub:, info: nil, exp: nil)
27
+ # Issue a connection JWT used by clients when establishing a real-time
28
+ # connection to Centrifugo.
29
+ #
30
+ # @param sub [String] Standard JWT claim with the application user ID.
31
+ # Use an empty string for anonymous connections.
32
+ # @param exp [Integer] UNIX timestamp (seconds) when the token expires.
33
+ # @param iat [Integer] UNIX timestamp (seconds) when the token was issued.
34
+ # @param jti [String] Unique token identifier.
35
+ # @param aud [String] Token audience (matches `client.token.audience`).
36
+ # @param iss [String] Token issuer (matches `client.token.issuer`).
37
+ # @param info [Hash] Arbitrary public info attached to the connection.
38
+ # @param b64info [String] Base64-encoded `info` (for binary payloads).
39
+ # @param channels [Array<String>] Server-side subscription channel list.
40
+ # @param subs [Hash] Server-side subscriptions with per-channel options.
41
+ # @param meta [Hash] Server-only metadata attached to the connection.
42
+ # @param expire_at [Integer] Override connection expiration timestamp.
43
+ #
44
+ # @return [String] Encoded JWT.
45
+ def issue_connection_token(sub:, exp: nil, iat: nil, jti: nil, aud: nil, iss: nil,
46
+ info: nil, b64info: nil, channels: nil, subs: nil,
47
+ meta: nil, expire_at: nil)
49
48
  payload = {
50
49
  'sub' => sub,
50
+ 'exp' => exp,
51
+ 'iat' => iat,
52
+ 'jti' => jti,
53
+ 'aud' => aud,
54
+ 'iss' => iss,
51
55
  'info' => info,
52
- 'exp' => exp
56
+ 'b64info' => b64info,
57
+ 'channels' => channels,
58
+ 'subs' => subs,
59
+ 'meta' => meta,
60
+ 'expire_at' => expire_at
53
61
  }.compact
54
62
 
55
63
  JWT.encode(payload, secret, algorithm)
56
64
  end
57
65
 
58
- # Generate JWT for private channels
59
- #
60
- # @param client [String]
61
- # Client ID which wants to subscribe on channel
62
- #
63
- # @option channel [String]
64
- # Channel that client tries to subscribe to (string).
65
- #
66
- # @param exp [Integer]
67
- # (default: nil) UNIX timestamp seconds when token will expire.
68
- #
69
- # @param info [Hash]
70
- # (default: {}) This claim is optional - this is additional information about
71
- # client connection that can be provided for Centrifugo.
72
- #
73
- # @example Get private channel JWT with expiration and extra info
74
- # notary.issue_channel_token(client: 'client', channel: 'channel', exp: 3600, info: { 'message' => 'wat' })
75
- # #=> eyJhbGciOiJIUzI1NiJ9.eyJjbGllbnQiOiJjbG..."
76
- #
77
- # @see (https://centrifugal.github.io/centrifugo/server/private_channels/)
78
- #
79
- # @return [String]
80
- #
81
- def issue_channel_token(client:, channel:, info: nil, exp: nil)
66
+ # Issue a subscription JWT used by clients to authorize subscription to a
67
+ # channel that requires token authorization.
68
+ #
69
+ # @param sub [String] Application user ID (same meaning as in connection token).
70
+ # @param channel [String] Channel this subscription token is valid for.
71
+ # @param exp [Integer] UNIX timestamp (seconds) when the token expires.
72
+ # @param iat [Integer] UNIX timestamp (seconds) when the token was issued.
73
+ # @param jti [String] Unique token identifier.
74
+ # @param aud [String] Token audience.
75
+ # @param iss [String] Token issuer.
76
+ # @param info [Hash] Arbitrary channel info.
77
+ # @param b64info [String] Base64-encoded `info`.
78
+ # @param override [Hash] Per-subscription channel option overrides.
79
+ # @param expire_at [Integer] Override subscription expiration timestamp.
80
+ #
81
+ # @return [String] Encoded JWT.
82
+ def issue_channel_token(sub:, channel:, exp: nil, iat: nil, jti: nil, aud: nil, iss: nil,
83
+ info: nil, b64info: nil, override: nil, expire_at: nil)
82
84
  payload = {
83
- 'client' => client,
85
+ 'sub' => sub,
84
86
  'channel' => channel,
87
+ 'exp' => exp,
88
+ 'iat' => iat,
89
+ 'jti' => jti,
90
+ 'aud' => aud,
91
+ 'iss' => iss,
85
92
  'info' => info,
86
- 'exp' => exp
93
+ 'b64info' => b64info,
94
+ 'override' => override,
95
+ 'expire_at' => expire_at
87
96
  }.compact
88
97
 
89
98
  JWT.encode(payload, secret, algorithm)
data/lib/cent/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cent
4
- VERSION = '2.2.0'
4
+ VERSION = '4.0.0'
5
5
  end
data/lib/cent.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'cent/version'
4
+ require 'cent/error'
4
5
  require 'cent/notary'
5
6
  require 'cent/client'
6
7
 
7
8
  # Centrifugo Ruby Client
8
9
  module Cent
9
- # Here will be code...
10
10
  end
metadata CHANGED
@@ -1,85 +1,65 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cent
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Prikhodko
8
- autorequire:
8
+ - Centrifugal Labs
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-28 00:00:00.000000000 Z
11
+ date: 1980-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.0.0
19
+ version: '2.0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: 3.0.0
22
+ version: '4'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ">"
27
+ - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 1.0.0
29
+ version: '2.0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: 3.0.0
32
+ version: '4'
33
33
  - !ruby/object:Gem::Dependency
34
- name: faraday_middleware
34
+ name: jwt
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - "~>"
37
+ - - ">="
38
38
  - !ruby/object:Gem::Version
39
- version: '1.0'
39
+ version: '2.2'
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: 2.0.0
42
+ version: '4'
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - "~>"
48
- - !ruby/object:Gem::Version
49
- version: '1.0'
50
- - - "<"
51
- - !ruby/object:Gem::Version
52
- version: 2.0.0
53
- - !ruby/object:Gem::Dependency
54
- name: jwt
55
- requirement: !ruby/object:Gem::Requirement
56
- requirements:
57
- - - "~>"
47
+ - - ">="
58
48
  - !ruby/object:Gem::Version
59
49
  version: '2.2'
60
- type: :runtime
61
- prerelease: false
62
- version_requirements: !ruby/object:Gem::Requirement
63
- requirements:
64
- - - "~>"
50
+ - - "<"
65
51
  - !ruby/object:Gem::Version
66
- version: '2.2'
52
+ version: '4'
67
53
  description: |
68
- Provides helper classes Cent::Client and Cent::Notary.
69
-
70
- `Cent::Client` is made to communicate to the server API
71
- `Client::Notary` is a simple JWT wrapper to generate authorization tokens for the frontend
54
+ Ruby client for Centrifugo server HTTP API. Provides Cent::Client to call
55
+ Centrifugo server methods (publish, broadcast, subscribe, presence, history, ...)
56
+ and Cent::Notary to issue JWT connection and subscription tokens.
72
57
  email:
73
58
  - prikha@gmail.com
74
59
  executables: []
75
60
  extensions: []
76
61
  extra_rdoc_files: []
77
62
  files:
78
- - ".github/workflows/main.yml"
79
- - ".github/workflows/release.yml"
80
- - ".gitignore"
81
- - ".rspec"
82
- - ".rubocop.yml"
83
63
  - CHANGELOG.md
84
64
  - Gemfile
85
65
  - LICENSE.txt
@@ -91,16 +71,15 @@ files:
91
71
  - lib/cent.rb
92
72
  - lib/cent/client.rb
93
73
  - lib/cent/error.rb
94
- - lib/cent/http.rb
95
74
  - lib/cent/notary.rb
96
75
  - lib/cent/version.rb
97
76
  homepage: https://github.com/centrifugal/rubycent
98
77
  licenses:
99
78
  - MIT
100
79
  metadata:
101
- homepage_uri: https://github.com/centrifugal/rubycent
102
80
  source_code_uri: https://github.com/centrifugal/rubycent
103
- post_install_message:
81
+ changelog_uri: https://github.com/centrifugal/rubycent/releases
82
+ bug_tracker_uri: https://github.com/centrifugal/rubycent/issues
104
83
  rdoc_options: []
105
84
  require_paths:
106
85
  - lib
@@ -108,15 +87,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
87
  requirements:
109
88
  - - ">="
110
89
  - !ruby/object:Gem::Version
111
- version: 2.5.0
90
+ version: '3.0'
112
91
  required_rubygems_version: !ruby/object:Gem::Requirement
113
92
  requirements:
114
93
  - - ">="
115
94
  - !ruby/object:Gem::Version
116
95
  version: '0'
117
96
  requirements: []
118
- rubygems_version: 3.0.3.1
119
- signing_key:
97
+ rubygems_version: 3.6.9
120
98
  specification_version: 4
121
- summary: Centrifugo API V2 Ruby Client
99
+ summary: Centrifugo server API client for Ruby
122
100
  test_files: []
@@ -1,18 +0,0 @@
1
- name: Ruby
2
-
3
- on: [push,pull_request]
4
-
5
- jobs:
6
- build:
7
- runs-on: ubuntu-latest
8
- steps:
9
- - uses: actions/checkout@v2
10
- - name: Set up Ruby
11
- uses: ruby/setup-ruby@v1
12
- with:
13
- ruby-version: 2.7.1
14
- - name: Run the default task
15
- run: |
16
- gem install bundler -v 2.2.10
17
- bundle install
18
- bundle exec rake
@@ -1,34 +0,0 @@
1
- name: Release Ruby Gem
2
-
3
- on:
4
- push:
5
- tags:
6
- - 'v[0-9].[0-9]+.[0-9]+'
7
- jobs:
8
- build-and-release:
9
- name: Release
10
- runs-on: ubuntu-latest
11
- permissions:
12
- packages: write
13
- contents: read
14
-
15
- steps:
16
- - name: Checkout
17
- uses: actions/checkout@v2
18
-
19
- - name: Set up Ruby 2.6
20
- uses: ruby/setup-ruby@477b21f02be01bcb8030d50f37cfec92bfa615b6
21
- with:
22
- ruby-version: 2.6
23
- - run: bundle install
24
-
25
- - name: Publish to RubyGems
26
- run: |
27
- mkdir -p $HOME/.gem
28
- touch $HOME/.gem/credentials
29
- chmod 0600 $HOME/.gem/credentials
30
- printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
31
- gem build *.gemspec
32
- gem push *.gem
33
- env:
34
- GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_API_KEY}}"
data/.gitignore DELETED
@@ -1,20 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
- *.bundle
11
- *.so
12
- *.o
13
- *.a
14
- mkmf.log
15
- .ruby-gemset
16
- .ruby-versions
17
- *.iml
18
- .idea/
19
- .rspec_status
20
-
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.rubocop.yml DELETED
@@ -1,13 +0,0 @@
1
- require:
2
- - rubocop-rspec
3
- - rubocop-rake
4
- AllCops:
5
- TargetRubyVersion: 2.5
6
- NewCops: enable
7
- RSpec/ExampleLength:
8
- Max: 15
9
- Metrics/BlockLength:
10
- Exclude:
11
- - 'spec/**/*'
12
- Gemspec/RequireMFA:
13
- Enabled: false
data/lib/cent/http.rb DELETED
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'cent/error'
4
-
5
- module Cent
6
- # Cent::ResponseError
7
- #
8
- # Raised when response from Centrifugo contains any error as result of API command execution.
9
- #
10
- class ResponseError < Error
11
- attr_reader :code
12
-
13
- def initialize(code:, message:)
14
- @code = code
15
- super message
16
- end
17
- end
18
-
19
- # Cent::HTTP
20
- #
21
- # Holds request call and response handling logic
22
- #
23
- class HTTP
24
- attr_reader :connection
25
-
26
- # @param connection [Faraday::Connection] HTTP Connection object
27
- #
28
- def initialize(connection:)
29
- @connection = connection
30
- end
31
-
32
- # Perform POST request to centrifugo API
33
- # @param body [Hash] Request body(non serialized)
34
- #
35
- # @raise [Cent::ResponseError]
36
- #
37
- # @return [Hash] Parsed response body
38
- #
39
- def post(body: nil)
40
- response = connection.post(nil, body)
41
-
42
- raise ResponseError.new(**response.body['error'].transform_keys(&:to_sym)) if response.body.key?('error')
43
-
44
- response.body
45
- end
46
- end
47
- end