basecradle 0.14.3 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd8cac9ca320e03ce203f1adc4867d3a9e87adb8512dc34401d4b98ed1a39cda
4
- data.tar.gz: 8f5240bf68714298207b18bc4a85f71505d5f812d8b795647479ddc3e2bc1ace
3
+ metadata.gz: 5d56a7fe24eeefe9a3e86258a703bfb8650bc5f357077772097759ffc7c61451
4
+ data.tar.gz: e4ac5b57cdb61c49a89e5d6cffda5b05a909fa45e535628cd853f26492c1e664
5
5
  SHA512:
6
- metadata.gz: 20ddd64efd96b24332c343c08f2d6cede7f7b7bd4ea844eaa04e946a9ff2565534ede68f742d9cbeaef9b605c7400a36b5b44c2c9b822e1c085871dcf7d9fb49
7
- data.tar.gz: d8da0222407c24563f545d40692745fd066670073ab0cb9ae531232f4adb67d1bec22103df90b531831a553f63bce3a6323f2780645ba46efa878b1fd6cecdf8
6
+ metadata.gz: c6bb6ccf04c1dd5d20072f42a8a6fed6cc69897bc1f6d9d1952c1e05aa3844e3524b7e916b8e6bc9e1c987f61ddea1d14d8059738a48c5354c2d394a572f9732
7
+ data.tar.gz: 0d8beaaa166bb8e1077569e457b8d8e547587263320e0c428b0788ef8e3af59db29cf95cf57f727f7966cfd3fcc6c26ed953f7a80a2ba96b08e82350a86a289f
data/CHANGELOG.md CHANGED
@@ -9,6 +9,111 @@ section**. The newest heading is always the version `lib/basecradle/version.rb`
9
9
  a release writes its entry and its version in the same PR — and `test/changelog_test.rb`
10
10
  fails CI if the two ever disagree.
11
11
 
12
+ ## [0.15.0] - 2026-11-20
13
+
14
+ A minor release that adds two verbs for an API operation the platform documented on
15
+ 2026-11-20. Nothing is removed and no signature changes. Its release pipeline also moved
16
+ to a Ruby with Ruby's 2026-11-18 security fixes. That is not a fix you get by upgrading
17
+ the gem: the SDK is pure Ruby and ships no interpreter, so the fixes reach you only when
18
+ you upgrade your own Ruby, to 3.3.14, 3.4.13 or 4.0.10.
19
+
20
+ ### Added
21
+
22
+ - **`WebhookEndpoint#configure_verification` and `#clear_verification`**
23
+ ([#314](https://github.com/basecradle/basecradle-ruby/pull/314), mirroring
24
+ [basecradle-python#376](https://github.com/basecradle/basecradle-python/issues/376)).
25
+ They set and remove an endpoint's signature verification by wrapping
26
+ `PATCH /webhook_endpoints/{uuid}`, which the platform documented together with a second
27
+ scheme, `github`.
28
+ - **`configure_verification(verifier:, credential:, signature_header:)`** sends only the
29
+ keywords you pass, as `verifier`, `verification_credential` and `signature_header`.
30
+ It raises `ArgumentError` before any request when you pass none.
31
+ - **`verifier:`** is passed through as a string: `"hmac_sha256_hex"` or `"github"`
32
+ today, so a scheme added later needs no release. One the platform does not know
33
+ raises `BaseCradle::ValidationError`.
34
+ - **`credential:`** is the signing secret. A blank one is sent as given, and the API
35
+ treats it as no change, so the current secret stays. It is sent as its bytes, which
36
+ must be UTF-8. A credential that is not a String, or whose bytes are not UTF-8,
37
+ raises `ArgumentError` without showing it, and nothing is sent. It is write-only: the
38
+ API never returns it, and the SDK keeps no copy and never puts it in an `inspect` or
39
+ an error message.
40
+ - **`signature_header:`** names the header the signature is read from, for
41
+ `hmac_sha256_hex` only. A `github` endpoint always reads `X-Hub-Signature-256`.
42
+ - **`clear_verification`** sends `clear_credential: true`, which removes the secret and
43
+ turns verification off.
44
+ - Both update the endpoint in place and return it, like `rotate`, so the new state
45
+ reads at `endpoint.content.verification`.
46
+
47
+ ### Security
48
+
49
+ - **The release pipeline runs on a Ruby with the 2026-11-18 Regexp and String fixes**
50
+ ([#312](https://github.com/basecradle/basecradle-ruby/pull/312),
51
+ [#311](https://github.com/basecradle/basecradle-ruby/issues/311)). The release and
52
+ the daily dependency scan pinned `ruby/setup-ruby` v1.338.0, which predates the fixed
53
+ builds. They now pin v1.343.0, which installs 3.3.14, 3.4.13 and 4.0.10, so the publish
54
+ job that builds and pushes the gem runs on 3.4.13. CI now pins every action it runs to a
55
+ full commit, and `lint` runs on 3.4. Ruby 3.2, the gem's floor, is past its end of
56
+ life and has no fixed build. It still runs where the floor itself is under test: the
57
+ `test (3.2)` job, the release rehearsal's build-and-install check, and the scan's 3.2
58
+ resolution.
59
+
60
+ ## [0.14.4] - 2026-11-17
61
+
62
+ A patch release with two behavior fixes, both about a credential. No verb is added or
63
+ removed, and no signature changes. It affects a caller whose token is one HTTP cannot
64
+ carry exactly as given: `Client.new` now raises `ArgumentError` before any request.
65
+ **Some such tokens used to authenticate, so a caller whose token can carry whitespace at
66
+ either end (a padded line in an env file, say) must pass it exactly as minted** (see
67
+ below). The `login` fix changes nothing for a caller who already passes a `max_retries`
68
+ of zero or more.
69
+
70
+ ### Fixed
71
+
72
+ - **A token HTTP cannot carry raises `ArgumentError` when the client is built, and the
73
+ error never shows it**
74
+ ([#304](https://github.com/basecradle/basecradle-ruby/pull/304),
75
+ [#301](https://github.com/basecradle/basecradle-ruby/issues/301)). A token follows the
76
+ same rule as an `idempotency_key` (0.14.3): printable ASCII, with spaces or tabs only
77
+ between characters. `BaseCradle::Client.new`, whether given the token or reading
78
+ `BASECRADLE_TOKEN`, raises `ArgumentError` for any other token, saying where the fault
79
+ is and never showing the token, not even the character at fault. The SDK never strips
80
+ or repairs a token. What a caller who passed such a token sees now:
81
+ - Whitespace at either end (`"bc_uat_… "`): until now, **the token was sent as given and
82
+ the request authenticated**, because a hop stripped the whitespace before the platform
83
+ read it. It now raises.
84
+ - A token made only of whitespace (`BASECRADLE_TOKEN=" "`): until now, a client was
85
+ built and every request failed. It now raises `ArgumentError`, not
86
+ `MissingTokenError`, which is still raised only for a missing or empty token.
87
+ - A trailing newline, as a token read from a file or an env file often keeps
88
+ (`"bc_uat_…\n"`): until now, the client was built and every request failed with
89
+ Net::HTTP's own `ArgumentError` ("header field value cannot include CR/LF"), which
90
+ named no argument. It now raises when the client is built, and the message says a
91
+ file or an env file often keeps the trailing newline.
92
+ - Any other control character, or a non-ASCII character: until now, the token was sent
93
+ as raw bytes. It now raises.
94
+ - A string in an encoding that is not ASCII-compatible (UTF-16, say): until now, every
95
+ request failed with an `Encoding::CompatibilityError`. It now raises `ArgumentError`
96
+ naming the encoding.
97
+
98
+ The Python SDK made the same change
99
+ ([basecradle-python#359](https://github.com/basecradle/basecradle-python/issues/359)).
100
+ - **`Client.login` with a negative `max_retries` raises before it mints a credential**
101
+ ([#305](https://github.com/basecradle/basecradle-ruby/pull/305)). `login` checked
102
+ `max_retries` only when it built the client, after `POST /session` had already minted a
103
+ token, so `login(max_retries: -1)` created a live credential the caller never received.
104
+ It still raises the same `ArgumentError`, but now before anything is sent, as the
105
+ Python SDK's `login` does.
106
+
107
+ ### Security
108
+
109
+ - **The daily dependency scan writes as this repository's own bot, and only as it**
110
+ ([#303](https://github.com/basecradle/basecradle-ruby/pull/303),
111
+ [#309](https://github.com/basecradle/basecradle-ruby/pull/309),
112
+ [basecradle#870](https://github.com/basecradle/basecradle/issues/870)). Its
113
+ `[SECURITY]` issues and their labels now come from `basecradle-ruby-ai[bot]`, not the
114
+ capital's App, and the scan reads only issues that bot wrote. Nothing in the gem itself
115
+ changes.
116
+
12
117
  ## [0.14.3] - 2026-11-16
13
118
 
14
119
  A patch release with one behavior fix. No verb is added or removed, and no signature
@@ -858,6 +963,8 @@ the Python SDK's behavior in idiomatic Ruby. Zero runtime dependencies.
858
963
  - **Quality bars** — a README-as-tested-doc harness (every example runs against a mocked
859
964
  API) and a spec drift-guard (CI fails if the live API grows beyond the SDK).
860
965
 
966
+ [0.15.0]: https://github.com/basecradle/basecradle-ruby/releases/tag/v0.15.0
967
+ [0.14.4]: https://github.com/basecradle/basecradle-ruby/releases/tag/v0.14.4
861
968
  [0.14.3]: https://github.com/basecradle/basecradle-ruby/releases/tag/v0.14.3
862
969
  [0.14.2]: https://github.com/basecradle/basecradle-ruby/releases/tag/v0.14.2
863
970
  [0.14.1]: https://github.com/basecradle/basecradle-ruby/releases/tag/v0.14.1
data/README.md CHANGED
@@ -27,6 +27,11 @@ bc = BaseCradle::Client.new # reads BASECRADLE_TOKEN
27
27
  bc = BaseCradle::Client.new("bc_uat_...") # …or pass it explicitly
28
28
  ```
29
29
 
30
+ The token is sent exactly as given, so it must be one HTTP can carry. A token read from a
31
+ file or an env file often keeps its trailing newline, and a token with a control
32
+ character, a non-ASCII character, or whitespace at either end raises `ArgumentError` when
33
+ the client is built. The SDK never strips or repairs it, and the error never shows it.
34
+
30
35
  No token yet? Mint one with your basecradle.com credentials. `login` hands back a
31
36
  ready-to-use client — the new token is on `bc.token`:
32
37
 
@@ -192,6 +197,11 @@ endpoint.disable # pause deliveries (410 to senders) without losing history
192
197
  endpoint.enable # resume
193
198
  endpoint.rotate # leaked URL? new ingest_url, old one dies, uuid unchanged
194
199
 
200
+ # Require signed deliveries. Here: a GitHub webhook, whose Secret at GitHub is this value
201
+ endpoint.configure_verification(verifier: "github", credential: "kT9vQ2mXw7RbLp4ZsYc81NdHaUeJ3gFo")
202
+ puts endpoint.content.verification.signature_header # X-Hub-Signature-256
203
+ endpoint.clear_verification # remove the secret: unsigned deliveries are accepted again
204
+
195
205
  # Read what came in — across all timelines, or narrowed
196
206
  bc.webhook_events.filter(endpoint: endpoint).each do |event|
197
207
  puts [event.content.content_type, event.content.payload].inspect
@@ -206,9 +216,28 @@ An endpoint's identity is `endpoint.content.uuid` — the wire carries no top-le
206
216
  and the SDK invents none. `BaseCradle.uuid_of(endpoint)` reads it too, and is what
207
217
  `.filter(endpoint:)` uses, so you can pass either an endpoint or a uuid.
208
218
 
219
+ **Signature verification is configured with two verbs.**
220
+ `endpoint.configure_verification(verifier:, credential:, signature_header:)` sends only
221
+ the keywords you pass, so anything left out stays as it is; passing none raises
222
+ `ArgumentError` before any request. `verifier:` is the scheme — `"hmac_sha256_hex"` (the
223
+ default; any sender you control) or `"github"` (GitHub's own `X-Hub-Signature-256`, so a
224
+ GitHub webhook can point straight at the `ingest_url`) — and is passed through as given,
225
+ so a scheme the platform adds later needs no SDK release; one it does not know raises
226
+ `BaseCradle::ValidationError`. `credential:` is the signing secret, and setting it turns
227
+ verification on; a blank one is no change at all (the API's rule): the current secret
228
+ stays, and `verification.enabled` cannot tell you a rotation was skipped, so make sure a
229
+ secret read from the environment is not empty before passing it. It is **write-only**: the
230
+ API never returns it, and the SDK keeps no copy and never puts it in an `inspect` or an
231
+ error message. `signature_header:` names the header an `hmac_sha256_hex` endpoint reads the
232
+ signature from. `endpoint.clear_verification` removes the secret and turns verification
233
+ off. Both return the endpoint, updated in place; read the result at
234
+ `endpoint.content.verification`. How senders sign is in the API docs:
235
+ [Verifying Inbound Deliveries](https://basecradle.com/docs/api#verifying-inbound-deliveries-optional).
236
+
209
237
  Each event embeds its endpoint **in full**, so `event.webhook_endpoint` is a live
210
238
  `BaseCradle::WebhookEndpoint` — its *current* state reads without a second request, and
211
- `disable` / `enable` / `rotate` work straight off the event. Two fields are the event's
239
+ its verbs (`disable`, `enable`, `rotate`, `configure_verification`, `clear_verification`)
240
+ work straight off the event. Two fields are the event's
212
241
  **historical** facts, fixed when the delivery arrived, and they are the only ones:
213
242
  `content.ingest_token_at_receipt` (which — possibly since-rotated — URL it came in on) and
214
243
  `content.verified_at_receipt` (whether its signature was verified). Everything inside the
@@ -3,6 +3,7 @@
3
3
  require "json"
4
4
 
5
5
  require_relative "errors"
6
+ require_relative "header_value"
6
7
  require_relative "rendering"
7
8
 
8
9
  module BaseCradle
@@ -34,28 +35,15 @@ module BaseCradle
34
35
  end
35
36
 
36
37
  # Why HTTP cannot carry this key exactly as given, or +nil+ when it can (#295). A key is
37
- # sent as given and never repaired, so it must be a header value no hop can change:
38
- # printable ASCII (+!+ to +~+), with spaces or tabs allowed only between those
39
- # characters. Net::HTTP refuses only CR and LF; it sends NUL, every other control
40
- # character and non-ASCII bytes, and a hop that receives whitespace at either end of a
41
- # value strips it, so the platform would store a different key. The one meaning every
42
- # SDK shares refuses all of them, not whatever one HTTP library tolerates. A string in
43
- # an encoding that is not ASCII-compatible (UTF-16, say) is refused as a whole: its
44
- # bytes are not the characters it holds.
38
+ # sent as given and never repaired, so it must be a header value no hop can change: the
39
+ # rule is +header_value_defect+, the one the token is held to as well. A key is not a
40
+ # secret, so the reason shows the character at fault.
45
41
  def self.unsendable_idempotency_key_reason(key)
46
- unless key.encoding.ascii_compatible?
47
- return "its encoding, #{key.encoding}, is not ASCII-compatible; pass an ASCII or UTF-8 string"
48
- end
49
-
50
- key.each_char.with_index do |char, index|
51
- return "#{char.inspect} at index #{index} is not ASCII" unless char.ascii_only?
52
- unless char == " " || char == "\t" || (0x21..0x7E).cover?(char.ord)
53
- return "#{char.inspect} at index #{index} is a control character"
54
- end
55
- end
56
- return unless key.start_with?(" ", "\t") || key.end_with?(" ", "\t")
42
+ index, reason = header_value_defect(key)
43
+ return if reason.nil?
44
+ return reason if index.nil?
57
45
 
58
- "it begins or ends with whitespace, which a header value cannot"
46
+ "#{key[index].inspect} at index #{index} #{reason}"
59
47
  end
60
48
 
61
49
  # The per-request headers carrying an optional idempotency key — +nil+ when no key was
@@ -7,6 +7,7 @@ require "uri"
7
7
  require_relative "contact_messages"
8
8
  require_relative "dashboard"
9
9
  require_relative "errors"
10
+ require_relative "header_value"
10
11
  require_relative "items"
11
12
  require_relative "rendering"
12
13
  require_relative "serialization"
@@ -78,13 +79,20 @@ module BaseCradle
78
79
  # dedupes keyed creates, so a resend can't duplicate the record). An unkeyed +POST+ is
79
80
  # never retried, whatever this is set to — and a blank key (empty, or only ASCII
80
81
  # whitespace) is no key. Retries back off exponentially, capped at RETRY_MAX_DELAY seconds.
82
+ #
83
+ # The token is sent exactly as given, so it must be one HTTP can carry: a token with a
84
+ # control character (the trailing newline of one read from a file or an env file), a
85
+ # non-ASCII character, or whitespace at either end raises ArgumentError here, before
86
+ # any request. The SDK never strips or repairs it, and the error never shows it.
81
87
  def initialize(token = nil, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT,
82
88
  max_retries: DEFAULT_MAX_RETRIES)
83
89
  resolved = token || ENV.fetch("BASECRADLE_TOKEN", nil)
84
90
  raise MissingTokenError, MISSING_TOKEN_MESSAGE if resolved.nil? || resolved.empty?
85
- raise ArgumentError, "max_retries must be >= 0" if max_retries.negative?
91
+ self.class.refuse_negative_max_retries(max_retries)
86
92
 
87
- @token = resolved
93
+ # The one string checked is the one sent: "Bearer #{@token}" interpolates it as is.
94
+ @token = resolved.to_s
95
+ refuse_unsendable_token(@token)
88
96
  @base_url = base_url
89
97
  @timeout = timeout
90
98
  @max_retries = max_retries
@@ -123,9 +131,14 @@ module BaseCradle
123
131
  # The minted token is on the returned client as +#token+ — save it; it is never
124
132
  # retrievable again. The credential itself is on +#session+ (so +bc.session.uuid+ is
125
133
  # what revokes it later), and +#start_here+ points at the Dashboard. +name+ is an
126
- # optional label to tell credentials apart later.
134
+ # optional label to tell credentials apart later. A negative +max_retries+ raises
135
+ # ArgumentError before anything is sent, so no credential is minted.
127
136
  def self.login(email_address:, password:, name: nil, base_url: DEFAULT_BASE_URL,
128
137
  timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES)
138
+ # Before the mint, as the Python SDK's login does: a bad count refused only when the
139
+ # client is built would have minted a credential the caller never receives.
140
+ refuse_negative_max_retries(max_retries)
141
+
129
142
  payload = { "email_address" => email_address, "password" => password }
130
143
  payload["name"] = name unless name.nil?
131
144
 
@@ -227,6 +240,11 @@ module BaseCradle
227
240
  handle(deliver(method, path, json: json, params: params, form: form, headers: headers))
228
241
  end
229
242
 
243
+ # One check for +new+ and +login+, so a count +new+ refuses never reaches the mint.
244
+ def self.refuse_negative_max_retries(max_retries)
245
+ raise ArgumentError, "max_retries must be >= 0" if max_retries.negative?
246
+ end
247
+
230
248
  # The shared low-level send: returns the Net::HTTPResponse or raises APIConnectionError.
231
249
  def self.perform(uri, request, timeout)
232
250
  http = Net::HTTP.new(uri.host, uri.port)
@@ -291,6 +309,30 @@ module BaseCradle
291
309
  "than the object: model.to_h holds no client."
292
310
  end
293
311
 
312
+ # Refuse a token HTTP cannot carry exactly as given, when the client is built (#301).
313
+ # The idempotency key's rule (BaseCradle.header_value_defect), applied to the token: the
314
+ # caller's error, never stripped or repaired. Without it, a token read with its trailing
315
+ # newline built a client that failed every request with Net::HTTP's own ArgumentError,
316
+ # and every other such token was sent: a hop strips whitespace at either end, and the
317
+ # platform received the rest as given. The message says where the fault is and never
318
+ # shows the token, not even the character at fault.
319
+ def refuse_unsendable_token(token)
320
+ index, reason = BaseCradle.header_value_defect(token)
321
+ return if reason.nil?
322
+
323
+ where = index.nil? ? reason : "the character at index #{index} #{reason}"
324
+ # A token ending in CR or LF is the newline a file or an env file keeps (CRLF from
325
+ # Windows too); for any other fault, the token's source has to say. Read as bytes, so
326
+ # a token in an encoding that is not ASCII-compatible is answered rather than raised on.
327
+ hint = if token.b.end_with?("\n", "\r")
328
+ "A token read from a file or an env file often keeps its trailing newline."
329
+ else
330
+ "Check where the token was read from."
331
+ end
332
+ raise ArgumentError, "The token cannot be sent in an HTTP header: #{where}. It is not shown " \
333
+ "here, as it is a credential. #{hint} Pass it exactly as minted."
334
+ end
335
+
294
336
  # Record what the mint response said about the credential just issued. Private: only
295
337
  # +.login+ calls it, on a client it has just built.
296
338
  def minted(body)
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BaseCradle
4
+ # What stops HTTP carrying +value+ exactly as given, or +nil+ when nothing does. It is the
5
+ # one rule for a value the SDK sends in a header as the caller gave it, never stripped or
6
+ # repaired: an idempotency key (#295) and the bearer token (#301) alike. The rule lives
7
+ # here, below the client and the resources, so both can ask it.
8
+ #
9
+ # The value must be printable ASCII (+!+ to +~+), with spaces or tabs allowed only between
10
+ # those characters. Net::HTTP refuses only CR and LF. It sends NUL, every other control
11
+ # character and non-ASCII bytes. A hop that receives whitespace at either end of a value
12
+ # strips it, so the platform would read a different value. The rule every SDK shares
13
+ # refuses all of them, not whatever one HTTP library tolerates. A string in an encoding
14
+ # that is not ASCII-compatible (UTF-16, say) is refused as a whole: its bytes are not the
15
+ # characters it holds.
16
+ #
17
+ # The answer is <tt>[index, reason]</tt>: the index of the character at fault, or +nil+
18
+ # where no one character is (whitespace at either end, an encoding). The character itself
19
+ # is never part of the answer, so a caller describing the token can say where the fault
20
+ # is without showing any of it.
21
+ def self.header_value_defect(value)
22
+ unless value.encoding.ascii_compatible?
23
+ return [ nil, "its encoding, #{value.encoding}, is not ASCII-compatible; pass an ASCII or UTF-8 string" ]
24
+ end
25
+
26
+ value.each_char.with_index do |char, index|
27
+ # ascii_only? first: an invalid byte is not ASCII, and +ord+ would raise on it.
28
+ return [ index, "is not ASCII" ] unless char.ascii_only?
29
+ return [ index, "is a control character" ] unless char == " " || char == "\t" || (0x21..0x7E).cover?(char.ord)
30
+ end
31
+ return unless value.start_with?(" ", "\t") || value.end_with?(" ", "\t")
32
+
33
+ [ nil, "it begins or ends with whitespace, which a header value cannot" ]
34
+ end
35
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BaseCradle
4
- VERSION = "0.14.3"
4
+ VERSION = "0.15.0"
5
5
  end
@@ -10,10 +10,15 @@ module BaseCradle
10
10
  # --- models ---------------------------------------------------------------------------
11
11
 
12
12
  # Whether inbound deliveries must be signed, and how.
13
+ #
14
+ # +enabled+ is true once a signing secret is set. The secret itself is write-only: the
15
+ # API never returns it, so it is never here. Set it, the scheme and the header with
16
+ # +endpoint.configure_verification+; turn verification off with
17
+ # +endpoint.clear_verification+.
13
18
  class WebhookVerification < ApiObject
14
19
  attribute :enabled
15
- attribute :signature_header
16
- attribute :verifier # "hmac_sha256_hex"
20
+ attribute :signature_header # for a "github" endpoint, always "X-Hub-Signature-256"
21
+ attribute :verifier # "hmac_sha256_hex" or "github" — an open set; read it as a string
17
22
  end
18
23
 
19
24
  # An endpoint's content: identity, state, and the rotatable ingest URL.
@@ -27,7 +32,10 @@ module BaseCradle
27
32
 
28
33
  # An inbound webhook URL on a timeline. An endpoint is authored: +user+ is the peer who
29
34
  # created it (nested-actor form), and every event delivered to it inherits that author.
30
- # Verbs update this object from the full endpoint the API returns (live objects).
35
+ # Verbs update this object from the full endpoint the API returns (live objects). The
36
+ # API models enablement and rotation as singular state resources, and signature
37
+ # verification as a +PATCH+ of the endpoint itself; the SDK expresses all of them as
38
+ # verbs here, never as raw paths.
31
39
  #
32
40
  # The endpoint's identity is +content.uuid+ — the wire carries no top-level +uuid+, and
33
41
  # the SDK does not invent one. +BaseCradle.uuid_of(endpoint)+ yields it too, which is
@@ -54,13 +62,75 @@ module BaseCradle
54
62
  # Regenerate the ingest URL. The old URL dies immediately; the uuid is unchanged.
55
63
  # Use this when an ingest URL leaks. Recorded events are preserved.
56
64
  def rotate
57
- adopt(require_client.request("POST", "/webhook_endpoints/#{content.uuid}/rotation"))
65
+ adopt(require_client.request("POST", "#{endpoint_path}/rotation"))
66
+ end
67
+
68
+ # Set how inbound deliveries must be signed: the scheme, the secret, the header.
69
+ #
70
+ # Every keyword is optional and only the ones you pass are sent, so anything left out
71
+ # stays as it is; pass at least one, or this raises ArgumentError before any request
72
+ # is made.
73
+ #
74
+ # - +verifier+ is the scheme: "hmac_sha256_hex" (the default) or "github". It is
75
+ # passed through as given, so a scheme the platform adds later works here unchanged;
76
+ # one it does not know raises ValidationError.
77
+ # - +credential+ is the signing secret, sent as +verification_credential+: setting it
78
+ # turns verification on. A blank one is sent as given and the API treats it as no
79
+ # change: the current secret, if any, stays, and +content.verification.enabled+
80
+ # cannot tell that skipped rotation from a real one, so make sure a secret read from
81
+ # the environment is not empty before passing it. It is sent as its bytes, which
82
+ # must be UTF-8 (a BINARY string read from a file is fine); a credential that is not
83
+ # a String, or whose bytes are not UTF-8, raises ArgumentError before any request,
84
+ # and the error never shows it. It is write-only: the API never returns it, and this
85
+ # SDK keeps no copy and never puts it in an inspect or an error message.
86
+ # - +signature_header+ is the header the signature is read from, for
87
+ # "hmac_sha256_hex" only; a "github" endpoint always reads X-Hub-Signature-256.
88
+ #
89
+ # Any viewer of the endpoint's timeline may configure it, locked timeline or not.
90
+ # Returns this endpoint, which adopts the one the API returns, so
91
+ # +content.verification+ shows the new state.
92
+ def configure_verification(verifier: nil, credential: nil, signature_header: nil)
93
+ given = { "verifier" => verifier, "verification_credential" => credential,
94
+ "signature_header" => signature_header }.compact
95
+ if given.empty?
96
+ raise ArgumentError, "configure_verification needs at least one of verifier:, credential: " \
97
+ "or signature_header:. Nothing was sent."
98
+ end
99
+ given["verification_credential"] = sendable_secret(credential) unless credential.nil?
100
+
101
+ adopt(require_client.request("PATCH", endpoint_path, json: { "webhook_endpoint" => given }))
102
+ end
103
+
104
+ # Remove the signing secret and turn verification off: deliveries are accepted
105
+ # unsigned again, and +content.verification.enabled+ reads false. Returns this
106
+ # endpoint, which adopts the one the API returns.
107
+ def clear_verification
108
+ adopt(require_client.request("PATCH", endpoint_path,
109
+ json: { "webhook_endpoint" => { "clear_credential" => true } }))
58
110
  end
59
111
 
60
112
  private
61
113
 
114
+ # The credential's bytes as given, read as UTF-8, or ArgumentError. JSON refuses a
115
+ # string it cannot read as UTF-8, and its error holds the whole string, so a secret
116
+ # with the wrong bytes would ride out on an exception's +invalid_object+. Refusing it
117
+ # here, with a message that names the fault and never the value, closes that door.
118
+ def sendable_secret(credential)
119
+ if credential.is_a?(String)
120
+ secret = credential.b.force_encoding(Encoding::UTF_8)
121
+ return secret if secret.valid_encoding?
122
+ end
123
+
124
+ raise ArgumentError, "credential: must be a String whose bytes are UTF-8. It is not shown. " \
125
+ "Nothing was sent."
126
+ end
127
+
128
+ def endpoint_path
129
+ "/webhook_endpoints/#{content.uuid}"
130
+ end
131
+
62
132
  def enablement_path
63
- "/webhook_endpoints/#{content.uuid}/enablement"
133
+ "#{endpoint_path}/enablement"
64
134
  end
65
135
 
66
136
  # Live-object update: the API returned the complete endpoint, so this object points at
@@ -113,7 +183,8 @@ module BaseCradle
113
183
  attribute :timeline, wrap: Reference
114
184
  # The endpoint this arrived on, embedded in full (its own subject form) rather than by
115
185
  # reference — so its current state reads without a second request, and its verbs
116
- # (disable / enable / rotate) are reachable straight off the event. Its uuid is
186
+ # (disable / enable / rotate / configure_verification / clear_verification) are
187
+ # reachable straight off the event. Its uuid is
117
188
  # +event.webhook_endpoint.content.uuid+, or +BaseCradle.uuid_of(...)+.
118
189
  attribute :webhook_endpoint, wrap: WebhookEndpoint
119
190
  attribute :content, wrap: WebhookEventContent
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: basecradle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.3
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Drawk Kwast
@@ -25,6 +25,7 @@ files:
25
25
  - lib/basecradle/contact_messages.rb
26
26
  - lib/basecradle/dashboard.rb
27
27
  - lib/basecradle/errors.rb
28
+ - lib/basecradle/header_value.rb
28
29
  - lib/basecradle/headers.rb
29
30
  - lib/basecradle/items.rb
30
31
  - lib/basecradle/pagination.rb