Sha256: 8acec3f8f73357fe84a0aa5f57c690ceda306ce68c6e7fe680df972e1ec2bfcc

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true
require 'json'

# JWT::Decode module
module JWT
  # Decoding logic for JWT
  class Decode
    attr_reader :header, :payload, :signature

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

    def initialize(jwt, verify)
      @jwt = jwt
      @verify = verify
    end

    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

    private

    def raw_segments(jwt, verify)
      segments = jwt.split('.')
      required_num_segments = verify ? [3] : [2, 3]
      raise(JWT::DecodeError, 'Not enough or too many segments') unless required_num_segments.include? segments.length
      segments
    end

    def decode_header_and_payload(header_segment, payload_segment)
      header = JSON.parse(Decode.base64url_decode(header_segment))
      payload = JSON.parse(Decode.base64url_decode(payload_segment))
      [header, payload]
    rescue JSON::ParserError
      raise JWT::DecodeError, 'Invalid segment encoding'
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
jwt-blockstack-2.0.0.beta2 lib/jwt/decode.rb
jwt-blockstack-2.0.0.beta1 lib/jwt/decode.rb
jwt-2.0.0.beta1 lib/jwt/decode.rb