class JWT::Token
Represents a JWT
token
Basic token signed using the HS256 algorithm:
token = JWT::Token.new(payload: {pay: 'load'}) token.sign!(algorithm: 'HS256', key: 'secret') token.jwt # => eyJhb....
Custom headers will be combined with generated headers:
token = JWT::Token.new(payload: {pay: 'load'}, header: {custom: "value"}) token.sign!(algorithm: 'HS256', key: 'secret') token.header # => {"custom"=>"value", "alg"=>"HS256"}
Attributes
Public Class Methods
Source
Public Instance Methods
Source
# File lib/jwt/token.rb, line 84 def detach_payload! @detached_payload = true nil end
Detaches the payload according to datatracker.ietf.org/doc/html/rfc7515#appendix-F
Source
Source
Source
Source
# File lib/jwt/token.rb, line 78 def jwt @jwt ||= (@signature && [encoded_header, @detached_payload ? '' : encoded_payload, encoded_signature].join('.')) || raise(::JWT::EncodeError, 'Token is not signed') end
Returns the JWT
token as a string.
@return [String] the JWT
token as a string. @raise [JWT::EncodeError] if the token is not signed or other encoding issues
Also aliased as: to_s
Source
# File lib/jwt/token.rb, line 96 def sign!(algorithm:, key:) raise ::JWT::EncodeError, 'Token already signed' if @signature JWA.resolve(algorithm).tap do |algo| header.merge!(algo.header) @signature = algo.sign(data: signing_input, signing_key: key) end nil end
Signs the JWT
token.
@param algorithm [String, Object] the algorithm to use for signing. @param key [String] the key to use for signing. @return [void] @raise [JWT::EncodeError] if the token is already signed or other problems when signing