Module: AnyCable::JWT
- Defined in:
- lib/anycable/jwt.rb
Defined Under Namespace
Modules: BasicImpl Classes: DecodeError, ExpiredSignature, VerificationError
Class Attribute Summary collapse
-
.jwt_impl ⇒ Object
Returns the value of attribute jwt_impl.
Class Method Summary collapse
- .decode(token, secret_key: AnyCable.config.jwt_secret) ⇒ Object
- .encode(payload, expires_at: nil, secret_key: AnyCable.config.jwt_secret, ttl: AnyCable.config.jwt_ttl) ⇒ Object
Class Attribute Details
.jwt_impl ⇒ Object
Returns the value of attribute jwt_impl.
82 83 84 |
# File 'lib/anycable/jwt.rb', line 82 def jwt_impl @jwt_impl end |
Class Method Details
.decode(token, secret_key: AnyCable.config.jwt_secret) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/anycable/jwt.rb', line 100 def decode(token, secret_key: AnyCable.config.jwt_secret) raise ArgumentError, "JWT encryption key is not specified. Add it via `jwt_secret` or `secret` option" if secret_key.nil? || secret_key.empty? jwt_impl.decode(token, secret_key).then do |decoded| ::JSON.parse(decoded.fetch("ext"), symbolize_names: true) end.then { |data| Serializer.deserialize(data) } end |
.encode(payload, expires_at: nil, secret_key: AnyCable.config.jwt_secret, ttl: AnyCable.config.jwt_ttl) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/anycable/jwt.rb', line 84 def encode(payload, expires_at: nil, secret_key: AnyCable.config.jwt_secret, ttl: AnyCable.config.jwt_ttl) raise ArgumentError, "JWT encryption key is not specified. Add it via `jwt_secret` or `secret` option" if secret_key.nil? || secret_key.empty? encoded = Serializer.serialize(payload).to_json data = {ext: encoded} data[:exp] = expires_at.to_i if expires_at if ttl&.positive? && !data.key?(:exp) data[:exp] = Time.now.to_i + ttl end jwt_impl.encode(data, secret_key) end |