Class: JWT::Token
- Inherits:
-
Object
- Object
- JWT::Token
- Includes:
- Claims::VerificationMethods
- Defined in:
- lib/jwt/token.rb
Overview
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"}
Instance Attribute Summary collapse
-
#header ⇒ Hash
readonly
Returns the decoded header of the JWT token.
-
#payload ⇒ Hash
readonly
Returns the payload of the JWT token.
Instance Method Summary collapse
-
#detach_payload! ⇒ Object
Detaches the payload according to datatracker.ietf.org/doc/html/rfc7515#appendix-F.
-
#encoded_header ⇒ String
Returns the encoded header of the JWT token.
-
#encoded_payload ⇒ String
Returns the encoded payload of the JWT token.
-
#encoded_signature ⇒ String
Returns the encoded signature of the JWT token.
-
#initialize(payload:, header: {}) ⇒ Token
constructor
Initializes a new Token instance.
-
#jwt ⇒ String
(also: #to_s)
Returns the JWT token as a string.
-
#sign!(algorithm:, key:) ⇒ void
Signs the JWT token.
-
#signature ⇒ String
Returns the decoded signature of the JWT token.
-
#signing_input ⇒ String
Returns the signing input of the JWT token.
Methods included from Claims::VerificationMethods
#claim_errors, #valid_claims?, #verify_claims!
Constructor Details
#initialize(payload:, header: {}) ⇒ Token
Initializes a new Token instance.
24 25 26 27 |
# File 'lib/jwt/token.rb', line 24 def initialize(payload:, header: {}) @header = header&.transform_keys(&:to_s) @payload = payload end |
Instance Attribute Details
#header ⇒ Hash (readonly)
Returns the decoded header of the JWT token.
46 47 48 |
# File 'lib/jwt/token.rb', line 46 def header @header end |
#payload ⇒ Hash (readonly)
Returns the payload of the JWT token.
58 59 60 |
# File 'lib/jwt/token.rb', line 58 def payload @payload end |
Instance Method Details
#detach_payload! ⇒ Object
Detaches the payload according to datatracker.ietf.org/doc/html/rfc7515#appendix-F
84 85 86 87 88 |
# File 'lib/jwt/token.rb', line 84 def detach_payload! @detached_payload = true nil end |
#encoded_header ⇒ String
Returns the encoded header of the JWT token.
51 52 53 |
# File 'lib/jwt/token.rb', line 51 def encoded_header @encoded_header ||= ::JWT::Base64.url_encode(JWT::JSON.generate(header)) end |
#encoded_payload ⇒ String
Returns the encoded payload of the JWT token.
63 64 65 |
# File 'lib/jwt/token.rb', line 63 def encoded_payload @encoded_payload ||= ::JWT::Base64.url_encode(JWT::JSON.generate(payload)) end |
#encoded_signature ⇒ String
Returns the encoded signature of the JWT token.
39 40 41 |
# File 'lib/jwt/token.rb', line 39 def encoded_signature @encoded_signature ||= ::JWT::Base64.url_encode(signature) end |
#jwt ⇒ String Also known as: to_s
Returns the JWT token as a string.
78 79 80 |
# 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 |
#sign!(algorithm:, key:) ⇒ void
This method returns an undefined value.
Signs the JWT token.
96 97 98 99 100 101 102 103 104 105 |
# 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 |
#signature ⇒ String
Returns the decoded signature of the JWT token.
32 33 34 |
# File 'lib/jwt/token.rb', line 32 def signature @signature ||= ::JWT::Base64.url_decode(encoded_signature || '') end |
#signing_input ⇒ String
Returns the signing input of the JWT token.
70 71 72 |
# File 'lib/jwt/token.rb', line 70 def signing_input @signing_input ||= [encoded_header, encoded_payload].join('.') end |