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.



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

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.



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

def header
  @header
end

#payloadObject (readonly)

Returns the value of attribute payload.



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

def payload
  @payload
end

#signatureObject (readonly)

Returns the value of attribute signature.



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

def signature
  @signature
end

Class Method Details

.base64url_decode(str) ⇒ Object



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

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

Instance Method Details

#decode_segmentsObject



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

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



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

def verify
  @options.each do |key, val|
    next unless key.to_s.match(/verify/)

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