Module: Linzer::Helper
- Included in:
- Linzer
- Defined in:
- lib/linzer/helper.rb
Overview
These methods are mixed into the Linzer module and can be called directly as Linzer.sign! and Linzer.verify!.
Convenience methods for signing and verifying HTTP messages.
These methods provide a simpler interface for common use cases, handling message wrapping and signature attachment automatically.
Instance Method Summary collapse
-
#sign!(request_or_response, **args) ⇒ Object
Signs an HTTP request or response and attaches the signature.
-
#verify!(request_or_response, key: nil, no_older_than: 900) {|keyid| ... } ⇒ true
Verifies a signed HTTP request or response.
Instance Method Details
#sign!(request_or_response, **args) ⇒ Object
Signs an HTTP request or response and attaches the signature.
This is a convenience method that wraps the message, creates a signature, and attaches it to the original HTTP message in one step.
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/linzer/helper.rb', line 49 def sign!(request_or_response, **args) = Message.new(request_or_response) = {} label = args[:label] [:label] = label if label .merge!(args.fetch(:params, {})) key = args.fetch(:key) signature = Linzer::Signer.sign(key, , args.fetch(:components), ) .attach!(signature) end |
#verify!(request_or_response, key: nil, no_older_than: 900) {|keyid| ... } ⇒ true
Verifies a signed HTTP request or response.
Extracts the signature from the message headers, rebuilds the signature base, and verifies the cryptographic signature.
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/linzer/helper.rb', line 97 def verify!(request_or_response, key: nil, no_older_than: 900) = Message.new(request_or_response) signature_headers = {} %w[signature-input signature].each do |name| value = .header(name) signature_headers[name] = value if value end signature = Signature.build(signature_headers) keyid = signature.parameters["keyid"] raise Linzer::Error, "key not found" if !key && !keyid verify_key = block_given? ? (yield keyid) : key Linzer.verify(verify_key, , signature, no_older_than: no_older_than) end |