module AnyCable::Rails::JWT

Constants

ALGORITHM

Public Class Methods

decode(token) click to toggle source
# File lib/anycable-rails-jwt.rb, line 29
def decode(token)
  key = AnyCable.config.jwt_id_key
  raise ArgumentError, "JWT encryption key is not specified. Add it via `jwt_id_key` option" if key.blank?

  ::JWT.decode(token, key, true, {algorithm: ALGORITHM}).then do |decoded|
    JSON.parse(decoded.first.fetch("ext"))
  end.then do |serialized_ids|
    serialized_ids.transform_values! { |v| AnyCable::Rails.deserialize(v) }
    serialized_ids
  end
end
encode(expires_at: nil, **identifiers) click to toggle source
# File lib/anycable-rails-jwt.rb, line 16
def encode(expires_at: nil, **identifiers)
  key = AnyCable.config.jwt_id_key
  raise ArgumentError, "JWT encryption key is not specified. Add it via `jwt_id_key` option" if key.blank?

  expires_at ||= AnyCable.config.jwt_id_ttl.seconds.from_now

  serialized_ids = identifiers.transform_values { |v| AnyCable::Rails.serialize(v) }

  payload = {ext: serialized_ids.to_json, exp: expires_at.to_i}

  ::JWT.encode(payload, key, ALGORITHM)
end