mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
- Sign an entry: review exactly what's certified, choose a current certificate (eligibility checked: pilot only for preventive maintenance, inspections need their result and time), re-enter the password or sign in again with SSO (prompt=login, within 5 minutes), and confirm the "Certified by" statement (43.9 return to service, 43.11(a)(5) for inspections, preventive maintenance for pilots) - The signature stores signer, certificate, statement, UTC time, how they authenticated, IP/browser, a SHA-256 fingerprint of the entry's content and a readable snapshot; an HMAC seal covers the signature record. Status shows Valid / Superseded / Altered since signing. The signer is emailed - Signed entries (and their sub-components) can't be edited or deleted; a correction entry, once signed, supersedes the original (kept) - Owners invite an outside mechanic by email to sign one entry: one-time, 14-day link; they create or use their own account and certificate and see only that entry - Admins can disable accounts (signed out, can't sign in or sign; past signatures kept); users with signatures can't be deleted - Printouts show the electronic signature and fingerprint; archives carry signatures (marked imported, with their origin) and corrections Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
48 lines
2.3 KiB
Ruby
48 lines
2.3 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
|
|
AUTH_METHODS = { "password" => "password re-entered", "sso" => "single sign-on, signed in again" }.freeze
|
|
# "Certified by" conveys the intent to sign (2.2.2).
|
|
LABEL = "Certified by".freeze
|
|
|
|
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? }
|
|
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 short_id = content_digest.first(12)
|
|
|
|
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
|
|
{ valid: "Valid", superseded: "Superseded by a correction", altered: "Altered since signing" }[status]
|
|
end
|
|
end
|