Sha256: af9aa5a2ab10815c98c9292640f507c4fb1fae028242d5d0aef6ba84f1dd114f

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module ApiSignature
  class AuthHeader
    attr_reader :authorization

    TOKEN_REGEX = /^(API-HMAC-SHA256)\s+/.freeze
    AUTHN_PAIR_DELIMITERS = /(?:,|\t+)/.freeze

    def initialize(authorization)
      @authorization = authorization.to_s
    end

    def credential
      data[0]
    end

    def signature
      options['Signature']
    end

    def signed_headers
      return [] unless options['SignedHeaders']

      @signed_headers ||= options['SignedHeaders'].split(/;\s?/).map(&:strip)
    end

    def options
      @options ||= (data[1] || {})
    end

    private

    def data
      @data ||= (parse_token_with_options || [])
    end

    def parse_token_with_options
      return unless authorization[TOKEN_REGEX]

      params = token_params_from authorization
      [params.shift[1], Hash[params]]
    end

    def token_params_from(auth)
      rewrite_param_values params_array_from raw_params(auth)
    end

    def raw_params(auth)
      auth.sub(TOKEN_REGEX, '').split(/\s*#{AUTHN_PAIR_DELIMITERS}\s*/)
    end

    def params_array_from(raw_params)
      raw_params.map { |param| param.split(/=(.+)?/) }
    end

    def rewrite_param_values(array_params)
      array_params.each { |param| (param[1] || +'').gsub!(/^"|"$/, '') }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
api_signature-1.0.0 lib/api_signature/auth_header.rb