Class: ActionAgent::ApiKey

Inherits:
ApplicationRecord show all
Includes:
Ownable
Defined in:
app/models/action_agent/api_key.rb

Overview

API key generated from Settings -> API Keys. Authenticates requests to the telemetry ingest endpoint as a Bearer token.

The token is generated server-side (never accepted from user input) and encrypted at rest with Active Record Encryption, so the host app needs rails db:encryption:init before creating keys. Deterministic encryption keeps find_by(token:) lookups working against the ciphertext. Set ActionAgent.encrypt_credentials = false to store tokens in plain text instead; that is a downgrade, not a default.

Constant Summary collapse

TOKEN_PREFIX =
"aa_"

Constants included from Ownable

Ownable::CLASS_FOR

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ownable

#owner, #owner=

Methods inherited from ApplicationRecord

for_owner, owner_association

Class Method Details

.authenticate(token) ⇒ Object

Finds the key for a presented bearer token. Returns nil for blank or unknown tokens.



28
29
30
31
32
# File 'app/models/action_agent/api_key.rb', line 28

def self.authenticate(token)
  return nil if token.blank?

  find_by(token: token)
end

Instance Method Details

#masked_tokenObject

Safe to display in the dashboard key list: "aa_3xam…k9Q2"



40
41
42
# File 'app/models/action_agent/api_key.rb', line 40

def masked_token
  "#{token_prefix}#{token.last(4)}"
end

#touch_last_used!Object



34
35
36
37
# File 'app/models/action_agent/api_key.rb', line 34

def touch_last_used!
  # Throttled to avoid a write per ingest request.
  update_column(:last_used_at, Time.current) if last_used_at.nil? || last_used_at < 1.minute.ago
end