Powered by AppSignal & Oban Pro

Service integration

service-integration.livemd

Service integration

Use this notebook to practice the trust, key rotation, and replay checks a host runs around verification.

Download this .livemd file, open it in Livebook, and choose Open → Import from file, then Run all. Use a supported Elixir and Erlang/OTP toolchain with access to Hex. No secrets or external services are needed.

Rotate trusted issuer keys, select keys using an untrusted hint, require exact nonces, check proof time boundaries, diagnose mismatches, and retain invocation replay identities through the grant lifetime.

A successful verifier result is not an execution decision. The host owns authenticated trust configuration, revocation, durable replay checks, and authorization. The replay map below explains ordering and retention only; it is not a concurrent or durable store. Keys and maps stay in memory. No processes, ETS tables, databases, or files are created.

Open the published notebook in Livebook.

For exact-action approval, run the offline SQLite example to see durable claims and restart recovery.

Setup

Install the exact published package. Fixed evaluation times keep the results reproducible; only safe semantic values are persisted, never keys, signatures, tokens, or nonces.

Mix.install([{:bounded_authority_protocol, "== 2.0.0"}])
:ok

Generate keys for a rotation overlap

During rotation the host may trust both issuer keys while old grants remain live. Generate two issuer keys and one holder key, then build this host-owned trust set.

alias BoundedAuthorityProtocol.V1
alias BoundedAuthorityProtocol.V1.{Grant, Operation, TrustedIssuer, ExpectedGrant}

{old_public, old_private} = :crypto.generate_key(:eddsa, :ed25519)
{new_public, new_private} = :crypto.generate_key(:eddsa, :ed25519)
{holder_public, holder_private} = :crypto.generate_key(:eddsa, :ed25519)
{:ok, holder_thumbprint} = V1.Jwk.public_key_thumbprint_raw(holder_public, %{})

trusted_keys = %{"issuer-2026-04" => old_public, "issuer-2026-10" => new_public}

map_size(trusted_keys)
2

Build and sign grants with each issuer key

Both grants permit the same build operation and branch selectors. Only the issuer key id, signing key, and grant id differ.

sign_grant = fn key_id, private_key, grant_id ->
  grant = %Grant{
    key_id: key_id,
    issuer: "https://issuer.example",
    grant_id: grant_id,
    audiences: ["https://builds.example"],
    issued_at: 1_800_000_000,
    not_before: 1_800_000_000,
    expires_at: 1_800_086_400,
    holder_thumbprint: holder_thumbprint,
    operations: [
      %Operation{
        name: "trigger_build",
        selectors: [{:one_of, ["branch"], [{:string, "main"}, {:string, "release"}]}]
      }
    ]
  }

  {:ok, input} = V1.grant_signing_input(grant, %{})
  signature = :crypto.sign(:eddsa, :none, input.message, [private_key, :ed25519])
  {:ok, compact} = V1.assemble_compact(input, signature)
  compact
end

old_grant = sign_grant.("issuer-2026-04", old_private, "grant-build-001")
new_grant = sign_grant.("issuer-2026-10", new_private, "grant-build-002")

:rotation_grants_signed
:rotation_grants_signed

Select only from the trusted set

The protected-header kid is an untrusted lookup hint, never a source of trust. After lookup, verify the signature against exactly that key and the expected issuer and audience.

expected_grant = %ExpectedGrant{
  issuer: "https://issuer.example",
  audience: "https://builds.example",
  evaluation_time: 1_800_001_000,
  clock_skew: 60,
  bounds: %{}
}

verify_with = fn compact, keys ->
  with {:ok, locator} <- V1.untrusted_key_locator(compact, %{}),
       {:ok, public_key} <- Map.fetch(keys, locator.kid),
       trusted = %TrustedIssuer{key_id: locator.kid, public_key: public_key},
       {:ok, facts} <- V1.verify_grant(compact, trusted, expected_grant) do
    {:ok, facts.grant_id}
  else
    :error -> {:error, :unknown_key}
    {:error, :invalid} -> {:error, :invalid}
  end
end

{:ok, "grant-build-001"} = verify_with.(old_grant, trusted_keys)
{:ok, "grant-build-002"} = verify_with.(new_grant, trusted_keys)

[:old_key_verified, :new_key_verified]
[:old_key_verified, :new_key_verified]

Refuse a mislabeled or retired key

A grant signed by the old key still fails when labeled with the new key id. Removing the old key from host trust produces the host-defined :unknown_key, not a package error.

# A grant signed by the old key but labeled with the new key's id:
mislabeled = sign_grant.("issuer-2026-10", old_private, "grant-build-003")

# After the rotation completes, the host drops the old key from its set:
after_rotation = Map.delete(trusted_keys, "issuer-2026-04")

{:error, :invalid} = verify_with.(mislabeled, trusted_keys)
{:error, :unknown_key} = verify_with.(old_grant, after_rotation)

[:mislabeled_key, :retired_key]
[:mislabeled_key, :retired_key]

Sign holder proofs for the build request

Use one proof producer with explicit field overrides to exercise nonce and time policy. A production holder signs through its own key custody; no private key enters a protocol call.

alias BoundedAuthorityProtocol.V1.{Proof, Credentials, ExpectedRequest}

arguments = {:object, [{"branch", {:string, "main"}}, {"clean", {:boolean, true}}]}
issued_at = 1_800_001_000

sign_proof = fn overrides ->
  proof =
    struct!(
      %Proof{
        holder_public_key: holder_public,
        proof_id: "proof-build-001",
        method: "POST",
        target_uri: "https://builds.example/pipelines/42/runs",
        issued_at: issued_at,
        nonce: nil,
        invocation_id: "0f8fad5b-d9cb-469f-a165-70867728950e",
        operation: "trigger_build",
        grant_compact: new_grant,
        cast_arguments: arguments
      },
      overrides
    )

  {:ok, input} = V1.proof_signing_input(proof, %{})
  signature = :crypto.sign(:eddsa, :none, input.message, [holder_private, :ed25519])
  {:ok, compact} = V1.assemble_compact(input, signature)
  %Credentials{grant: new_grant, proof: compact}
end

:proof_signer_ready
:proof_signer_ready

Verify against host-derived request context

Supply the trusted issuer and the method, target, invocation, operation, and tagged arguments derived by the service. Bind the typed arguments once. Use arguments in ExpectedRequest and pass that binding to dispatch after host admission. Display only the operation and non-authorization marker here.

expected = %ExpectedRequest{
  trusted_issuer: %TrustedIssuer{key_id: "issuer-2026-10", public_key: new_public},
  issuer: "https://issuer.example",
  audience: "https://builds.example",
  method: "POST",
  target_uri: "https://builds.example/pipelines/42/runs",
  invocation_id: "0f8fad5b-d9cb-469f-a165-70867728950e",
  operation: "trigger_build",
  cast_arguments: arguments,
  evaluation_time: issued_at,
  clock_skew: 60,
  proof_max_age: 300,
  nonce: :not_required,
  bounds: %{}
}

verdict = fn credentials, expected ->
  case V1.check_envelope(credentials, expected) do
    {:ok, facts} -> {:ok, facts.operation}
    {:error, :invalid} -> {:error, :invalid}
  end
end

credentials = sign_proof.([])
{:ok, facts} = V1.check_envelope(credentials, expected)
Map.take(facts, [:operation, :authorization])
%{operation: "trigger_build", authorization: :not_evaluated}

Verify the two nonce modes

:not_required accepts only a proof without a nonce. Required mode matches the exact challenge the host issued; the verifier does not issue or consume it.

issued_nonce = "challenge-build-1"
nonce_credentials = sign_proof.(nonce: issued_nonce)
required = %{expected | nonce: {:required, issued_nonce}}

{:ok, no_nonce_facts} = V1.check_envelope(credentials, expected)
{:ok, required_facts} = V1.check_envelope(nonce_credentials, required)

%{without_nonce: no_nonce_facts.operation, required_nonce: required_facts.operation}
%{without_nonce: "trigger_build", required_nonce: "trigger_build"}

Refuse unexpected, different, or missing nonces

Nonce presence alone is insufficient, and optional mode does not mean ignore. The host also scopes and consumes each challenge in its authoritative state.

{:error, :invalid} = V1.check_envelope(nonce_credentials, expected)
{:error, :invalid} = V1.check_envelope(sign_proof.(nonce: "different-challenge"), required)
{:error, :invalid} = V1.check_envelope(credentials, required)

[:unexpected_nonce, :different_nonce, :missing_nonce]
[:unexpected_nonce, :different_nonce, :missing_nonce]

Check proof age and skew boundaries

Proof time is inclusive at both permitted endpoints. The host supplies Unix seconds; skew above 60 or maximum age above 300 is itself invalid.

at = fn seconds -> verdict.(credentials, %{expected | evaluation_time: issued_at + seconds}) end

{:error, :invalid} = at.(-61)
{:ok, "trigger_build"} = at.(-60)
{:ok, "trigger_build"} = at.(0)
{:ok, "trigger_build"} = at.(360)
{:error, :invalid} = at.(361)
{:error, :invalid} = verdict.(credentials, %{expected | clock_skew: 61})
{:error, :invalid} = verdict.(credentials, %{expected | proof_max_age: 301})

[:early, :stale, :skew_above_limit, :max_age_above_limit]
[:early, :stale, :skew_above_limit, :max_age_above_limit]

Diagnose a request-context mismatch

The service accidentally lowercases the method and changes the target host casing. Decode only to compare untrusted claims with host inputs; decoding never checks a signature.

derived = %{expected | method: "post", target_uri: "https://Builds.example/pipelines/42/runs"}

{:error, :invalid} = V1.check_envelope(credentials, derived)

diagnose = fn %Credentials{grant: grant, proof: proof}, %ExpectedRequest{} = expected ->
  {:ok, decoded_grant} = V1.decode_grant(grant, %{})
  {:ok, decoded_proof} = V1.decode_proof(proof, %{})
  {:ok, digest} = V1.request_digest(expected.operation, expected.cast_arguments, %{})

  checks = [
    issuer: decoded_grant.issuer == expected.issuer,
    audience: expected.audience in decoded_grant.audiences,
    key_id: decoded_grant.key_id == expected.trusted_issuer.key_id,
    method: decoded_proof.method == expected.method,
    target_uri: decoded_proof.target_uri == expected.target_uri,
    invocation_id: decoded_proof.invocation_id == expected.invocation_id,
    operation: decoded_proof.operation == expected.operation,
    cast_arguments: Base.url_encode64(decoded_proof.request_hash, padding: false) == digest,
    nonce:
      (expected.nonce == :not_required and decoded_proof.nonce == nil) or
        expected.nonce == {:required, decoded_proof.nonce},
    proof_window:
      expected.evaluation_time >= decoded_proof.issued_at - expected.clock_skew and
        expected.evaluation_time <=
          decoded_proof.issued_at + expected.proof_max_age + expected.clock_skew
  ]

  for {field, false} <- checks, do: field
end

%{verdict: verdict.(credentials, derived), mismatches: diagnose.(credentials, derived)}
%{verdict: {:error, :invalid}, mismatches: [:method, :target_uri]}

Correct the host-derived context

Restore the exact signed method and canonical target. The same credentials then verify, without issuing a new grant or proof.

{:ok, corrected_facts} =
  V1.check_envelope(credentials, %{derived | method: "POST", target_uri: expected.target_uri})

Map.take(corrected_facts, [:operation, :authorization])
%{operation: "trigger_build", authorization: :not_evaluated}

Observe that verification reserves nothing

The same credentials verify twice because the package has no replay store. Reserve the issuer/grant/invocation identity after verification and before any effect.

{:ok, first} = V1.check_envelope(credentials, expected)
{:ok, second} = V1.check_envelope(credentials, expected)

{first.authorization, second.authorization}
{:not_evaluated, :not_evaluated}

Define the host retention rule

This sequential map sketch retains execution identity through grant expiry plus permitted skew. A real host implements atomically tracked invocation identities in durable state shared by every instance.

accept = fn credentials, expected, replay_log ->
  case V1.check_envelope(credentials, expected) do
    {:ok, facts} ->
      key = {facts.issuer, facts.grant_id, facts.invocation_id}

      replay_log =
        Map.reject(replay_log, fn {_key, until} -> until <= expected.evaluation_time end)

      if Map.has_key?(replay_log, key) do
        {{:rejected, :replayed}, replay_log}
      else
        until = facts.expires_at + expected.clock_skew
        {{:accepted, facts.operation}, Map.put(replay_log, key, until)}
      end

    {:error, :invalid} ->
      {{:rejected, :invalid}, replay_log}
  end
end

:retention_rule_ready
:retention_rule_ready

Reserve the first invocation and refuse replay

The first presentation records the execution identity. Repeating it is refused by host policy even though its cryptographic proof is still valid.

{{:accepted, "trigger_build"}, replay_log} = accept.(credentials, expected, %{})
{{:rejected, :replayed}, replay_log} = accept.(credentials, expected, replay_log)

map_size(replay_log)
1

Refuse a fresh proof for the same invocation

After the first proof expires, the holder can sign another under the still-valid grant. Grant-lifetime retention must refuse that invocation again; proof-lifetime retention would accept it and fail the result match below.

later = %{expected | evaluation_time: issued_at + 400}
fresh = sign_proof.(proof_id: "proof-build-002", issued_at: later.evaluation_time)

{:error, :invalid} = V1.check_envelope(credentials, later)
{:ok, fresh_facts} = V1.check_envelope(fresh, later)
{{:rejected, :replayed}, _replay_log} = accept.(fresh, later, replay_log)

%{operation: fresh_facts.operation, fresh_proof: :verified, invocation: :replayed}
%{operation: "trigger_build", fresh_proof: :verified, invocation: :replayed}

Use a real clock and an authenticated trust configuration in a host. Test its transaction, restart, and concurrency behavior against its real replay store; see testing your integration and operations.