OpenPayments.Interaction (open_payments_elixir v1.0.0)

Copy Markdown View Source

Pure SHA-256 helpers for an interaction finish callback hash.

Supply stored client_nonce, stored server_nonce (the initial response's interact.finish), callback interact_ref and the initial grant_endpoint as keyword options. The four ASCII values are joined in that order with single newline separators and no trailing newline. URI spelling is preserved: use the exact grant endpoint used for initiation, not a callback or continuation URI.

Default encoding: :base64 uses canonical padded Base64, matching the deployed Open Payments implementation and its JavaScript documentation example. Explicit encoding: :base64url uses canonical unpadded base64url from GNAP RFC 9635 section 4.2.3. There is no format detection or fallback. Only SHA-256 is supported; do not negotiate another hash method for use with these helpers.

Callback inputs are deliberately stricter than the general JSON data codecs: nonces/reference must be nonempty printable ASCII without leading/trailing whitespace. Internal spaces are preserved. Controls and non-ASCII values are rejected rather than converted. Endpoint must be an absolute HTTP(S) request URL without user-info/fragment; validation does not normalize its hash input.

The caller binds these values to the correct session, generates fresh unpredictable client nonces, checks callback result/error fields and prevents replay. These helpers perform no HTTP requests, session management, entropy assessment, waiting or automatic continuation. A matching hash does not itself establish granted access. Errors retain safe field names, not input values.

Summary

Functions

Calculates an interaction callback hash using SHA-256 and an explicit encoding profile.

Verifies a canonical callback hash against the supplied stored interaction context.

Types

hash_options()

@type hash_options() :: [
  client_nonce: String.t(),
  server_nonce: String.t(),
  interact_ref: String.t(),
  grant_endpoint: String.t(),
  encoding: :base64 | :base64url
]

Functions

hash(opts)

@spec hash(hash_options()) :: {:ok, String.t()} | {:error, OpenPayments.Error.t()}

Calculates an interaction callback hash using SHA-256 and an explicit encoding profile.

verify_hash(received, opts)

@spec verify_hash(String.t(), hash_options()) ::
  :ok | {:error, OpenPayments.Error.t()}

Verifies a canonical callback hash against the supplied stored interaction context.

Returns :ok on a match, otherwise a validation error with operation :verify_interaction_hash. Equal-length canonical hash strings are compared using OTP's constant-time comparison. Received hash must already be extracted and URL-decoded once by the caller's callback handler; no decoding is performed here. Only submit interact_ref to continuation after verification succeeds.