Module: WebBotAuth::Signer

Defined in:
app/services/web_bot_auth/signer.rb

Overview

Produces the RFC 9421 request-signature headers for outbound bot/agent
requests.

Fail-open by design: any misconfiguration (no key, disabled, bad URL,
unexpected error) returns no headers and the caller proceeds unsigned —
signing must never break a fetch.

Constant Summary collapse

VALIDITY =

The validity window for an outbound request signature.

5.minutes

Class Method Summary collapse

Class Method Details

.headers_for(url:, now: Time.current) ⇒ Hash{String=>String}

Build the Web Bot Auth signing headers for a single outbound request.

The signature covers the target +@authority+ and our +signature-agent+,
so a receiving site can fetch our directory and verify the request.

Parameters:

  • url (String, URI)

    the target URL — its +@authority+ is signed

  • now (Time) (defaults to: Time.current)

    the signature creation time (injectable for tests)

Returns:

  • (Hash{String=>String})

    the +Signature-Agent+, +Signature-Input+,
    and +Signature+ headers, or an empty hash when signing is unavailable



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/web_bot_auth/signer.rb', line 25

def headers_for(url:, now: Time.current)
  return {} unless WebBotAuth.sign_outbound?

  authority = WebBotAuth.authority_for(url)
  return {} if authority.blank?

  signature = WebBotAuth::MessageSignature.new(
    label: 'sig1',
    components: [
      WebBotAuth::MessageSignature::Component.new(name: '@authority', value: authority),
      WebBotAuth::MessageSignature::Component.new(name: 'signature-agent', value: WebBotAuth::SIGNATURE_AGENT)
    ],
    created: now.to_i,
    expires: (now + VALIDITY).to_i,
    keyid: WebBotAuth::Key.kid,
    tag: WebBotAuth::TAG_REQUEST
  )

  { 'Signature-Agent' => WebBotAuth::SIGNATURE_AGENT }.merge(signature.headers)
rescue StandardError => e
  Rails.logger.warn("[web_bot_auth] outbound signing skipped: #{e.class}: #{e.message}")
  {}
end