cent 3.0.0 → 5.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 DELETED
@@ -1,96 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'jwt'
4
- require 'cent/error'
5
-
6
- module Cent
7
- # Cent::Notary
8
- #
9
- # Handle token generation
10
- #
11
- 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
- #
18
- def initialize(secret:, algorithm: 'HS256')
19
- raise Error, 'Secret can not be nil' if secret.nil?
20
-
21
- @secret = secret
22
- @algorithm = algorithm
23
- end
24
-
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)
49
- payload = {
50
- 'sub' => sub,
51
- 'info' => info,
52
- 'exp' => exp
53
- }.compact
54
-
55
- JWT.encode(payload, secret, algorithm)
56
- end
57
-
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)
82
- payload = {
83
- 'client' => client,
84
- 'channel' => channel,
85
- 'info' => info,
86
- 'exp' => exp
87
- }.compact
88
-
89
- JWT.encode(payload, secret, algorithm)
90
- end
91
-
92
- private
93
-
94
- attr_reader :secret, :algorithm
95
- end
96
- end