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, key:, components: nil, label: nil, params: {}, profile: nil) ⇒ 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, key:, components: nil, label: nil, params: {}, profile: nil) ⇒ 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.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/linzer/helper.rb', line 59 def sign!(request_or_response, key:, components: nil, label: nil, params: {}, profile: nil) ctx = Signature::Context.new( message: Message.new(request_or_response), key: key, label: label, components: Array(components), params: Hash(params) ) resolved_profile = Signature::Profile.resolve(profile) resolved_profile&.apply(ctx) signature = Linzer::Signer.sign( ctx.key, ctx., ctx.components, ctx.params ) ctx..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.
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/linzer/helper.rb', line 116 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 |