Files
simple-aircraft-manager-rails/app/models/webhook_endpoint.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

29 lines
978 B
Ruby

# Where an integration wants to hear about changes (squawks, signed entries):
# SAM posts JSON there, signed with the shared secret (see WebhookDeliveryJob).
class WebhookEndpoint < ApplicationRecord
EVENTS = LabelSet.new("webhook_endpoint.events", %w[squawk.created squawk.updated squawk.resolved entry.signed])
belongs_to :api_token
has_many :deliveries, class_name: "WebhookDelivery", dependent: :delete_all
encrypts :secret
normalizes :url, with: ->(u) { u.to_s.strip }
validates :url, presence: true, format: { with: %r{\Ahttps?://[^\s/]+(:\d+)?(/\S*)?\z}i }
validates :secret, presence: true, length: { minimum: 16 }
validate :events_known
scope :enabled, -> { where(enabled: true) }
def self.new_secret = SecureRandom.hex(32)
def subscribed?(event) = events.include?(event.to_s)
private
def events_known
errors.add(:events, :blank) if events.blank?
errors.add(:events, :inclusion) if (Array(events) - EVENTS.keys).any?
end
end