lib/json_jws/sign.rb in json_jws-0.1.0 vs lib/json_jws/sign.rb in json_jws-0.2.0

- old
+ new

@@ -5,22 +5,21 @@ module JsonJws::Sign def build_signature(jwk, header, payload) payload = payload.to_json unless payload.is_a?(String) raise "header must be a Hash" unless header.is_a?(Hash) - encoded_payload = Base64.urlsafe_encode64(payload) - encoded_header = Base64.urlsafe_encode64(header.to_json) - - JOSE::JWS.sign(jwk, payload, header).to_hash["signature"] + jose_signed_map = JOSE::JWS.sign(jwk, payload, header) + jose_signed_map.to_hash.slice("protected", "signature").values end - def add_signature(jws, jwk, header, payload) - protected_header = Base64.urlsafe_encode64(header.to_json) - payload = payload.to_json unless payload.is_a?(String) + def add_signature(jws, jwk, protected_header) + encoded_protected_header = Base64.urlsafe_encode64(protected_header.to_json) + raw_payload = Base64.decode64(jws["payload"]) + protected_header, signature = build_signature(jwk, protected_header, raw_payload) jws["signatures"] << { "protected" => protected_header, - "signature" => build_signature(jwk, header, payload), + "signature" => signature, } jws end end