Files
simple-aircraft-manager-rails/app/models/entry_signature.rb
T
rickyandClaude Opus 5.5 bffdea937c REST API v1: tokens, read endpoints, squawks, webhooks
- Personal tokens (Profile → API tokens) act as their owner; integration
  tokens (Admin → Integrations) reach chosen aircraft and scopes, and their
  writes name the person they're for (SAM-On-Behalf-Of). Tokens are stored
  hashed, shown once, revocable, with last use recorded.
- GET aircraft, components, due list, inspections, ADs, logbook entries and
  squawks; POST/PATCH squawks with external_ref links to the other system.
- Idempotency-Key for safe retries, paging, JSON errors, rate limits.
- Record history keeps the token with each change (in the hashed payload).
- Webhooks (squawk created/updated/resolved, entry signed), HMAC-signed,
  retried, never sent to the server itself; deliveries listed for admins.
- The outbound address check is shared by AirTrail and webhooks; reporting a
  squawk is announced the same way from the form and the API.
- docs/guide/api.md: the API reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 19:05:10 -05:00

54 lines
2.5 KiB
Ruby

# An electronic signature on a logbook entry (AC 120-78B chapter 2).
#
# It records the signer, the certificate they signed with, the statement they
# certified, when, and how they proved who they were. content_digest is a
# SHA-256 of the entry's content at signing (content_snapshot is that content),
# so any later change shows; seal is an HMAC over the signature itself, keyed
# by this server, so the signature record can't be altered unnoticed either.
class EntrySignature < ApplicationRecord
include Versioned
AUTH_METHODS = LabelSet.new("entry_signature.auth_methods", %w[password password_totp sso])
# "Certified by" conveys the intent to sign (2.2.2).
def self.label = I18n.t("labels.entry_signature.certified_by")
belongs_to :logbook_entry
belongs_to :user, optional: true
validates :signer_name, :certificate_kind, :certificate_number, :statement, :signed_at, :content_digest, :content_snapshot, presence: true
validates :auth_method, inclusion: { in: AUTH_METHODS.keys + [ "imported" ] }
validates :logbook_entry_id, uniqueness: true
before_create { self.seal ||= compute_seal unless imported? }
# Integrations hear about it (webhooks); not for signatures carried over by an import.
after_create_commit do
next if imported?
entry = logbook_entry
Webhooks.publish("entry.signed", aircraft: entry.aircraft, data: ApiPresenter.logbook_entry(entry))
end
before_update { throw :abort } # signatures are permanent (2.1.2.7)
before_destroy { throw :abort unless destroyed_by_association }
def self.seal_key = Rails.application.key_generator.generate_key("entry-signature-seal", 32)
def imported? = origin.present?
def certificate_label = UserCertificate::SHORT_LABELS.fetch(certificate_kind, certificate_kind)
def compute_seal
payload = [ logbook_entry_id, user_id, signer_name, certificate_kind, certificate_number, statement,
signed_at.utc.iso8601(6), content_digest, auth_method ].join("\u001f")
OpenSSL::HMAC.hexdigest("SHA256", self.class.seal_key, payload)
end
def seal_valid? = imported? || (seal.present? && ActiveSupport::SecurityUtils.secure_compare(seal, compute_seal))
def content_matches? = content_digest == logbook_entry.content_digest
# :valid, :superseded, :altered (content or signature changed since signing)
def status
return :altered unless seal_valid? && content_matches?
logbook_entry.superseded_at ? :superseded : :valid
end
def status_label
I18n.t(status, scope: "models.entry_signature.statuses")
end
end