Sha256: 24523605a326b1ef80bc5e8e9c57d387a64a5c7e9115873279cc69fbcfdcb18c
Contents?: true
Size: 1.6 KB
Versions: 16
Compression:
Stored size: 1.6 KB
Contents
# frozen_string_literal: true require 'json' module Trusona module Api # ## A HMAC message suitable for authorization to the Trusona API class HashedMessage def initialize(params = {}) validate(params) @method = params[:method] @body = params[:body] @content_type = params[:content_type] @path = params[:path] @date = params[:date] @secret = Trusona.config.secret @token = Trusona.config.token end def auth_header "TRUSONA #{@token}:#{signature}" end def signature Base64.strict_encode64( OpenSSL::HMAC.hexdigest( OpenSSL::Digest::SHA256.new, @secret, prepare_data ) ) end private def body_digest digestable_body = '' digestable_body = @body unless @body.nil? || @body.empty? OpenSSL::Digest::MD5.new.hexdigest(digestable_body) end def invalid_param?(param) param.nil? end def invalid_method?(method) http_methods = %w[GET POST DELETE PATCH PUT] return true if invalid_param?(method) return true unless http_methods.include?(method.strip.upcase) end def prepare_data data = [ @method.to_s, body_digest, @content_type, @date, @path ] data.join("\n") end def validate(params) raise ArgumentError if invalid_method?(params[:method]) raise ArgumentError if invalid_param?(params[:path]) end end end end
Version data entries
16 entries across 16 versions & 1 rubygems