Class: JWT::Decode

Inherits:
Object
  • Object
show all
Defined in:
lib/jwt/decode.rb

Overview

Decoding logic for JWT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jwt, key, verify, options, &keyfinder) ⇒ Decode

Returns a new instance of Decode.



13
14
15
16
17
18
19
# File 'lib/jwt/decode.rb', line 13

def initialize(jwt, key, verify, options, &keyfinder)
  @jwt = jwt
  @key = key
  @verify = verify
  @options = options
  @keyfinder = keyfinder
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



11
12
13
# File 'lib/jwt/decode.rb', line 11

def header
  @header
end

#payloadObject (readonly)

Returns the value of attribute payload.



11
12
13
# File 'lib/jwt/decode.rb', line 11

def payload
  @payload
end

#signatureObject (readonly)

Returns the value of attribute signature.



11
12
13
# File 'lib/jwt/decode.rb', line 11

def signature
  @signature
end

Class Method Details

.base64url_decode(str) ⇒ Object



44
45
46
47
# File 'lib/jwt/decode.rb', line 44

def self.base64url_decode(str)
  str += '=' * (4 - str.length.modulo(4))
  Base64.decode64(str.tr('-_', '+/'))
end

Instance Method Details

#decode_segmentsObject



21
22
23
24
25
26
27
# File 'lib/jwt/decode.rb', line 21

def decode_segments
  header_segment, payload_segment, crypto_segment = raw_segments(@jwt, @verify)
  @header, @payload = decode_header_and_payload(header_segment, payload_segment)
  @signature = Decode.base64url_decode(crypto_segment.to_s) if @verify
  signing_input = [header_segment, payload_segment].join('.')
  [@header, @payload, @signature, signing_input]
end

#verifyObject



49
50
51
52
53
54
55
# File 'lib/jwt/decode.rb', line 49

def verify
  @options.each do |key, val|
    next unless key.to_s =~ /verify/

    Verify.send(key, payload, @options) if val
  end
end