Files
ubicloud/model/api_key.rb
Jeremy Evans 30247a3800 Include model annotations at the bottom of all model files
This makes it easier for developers new to the codebase to
easily get important information on the model's table in the
same file as the model code.

To ensure the model annotations stay accurate, run them on
test_up/test_down.  In CI, regenerate the annotations, and
check for no changes, similar to how the linters work.
2024-11-13 09:13:30 -08:00

44 lines
1.3 KiB
Ruby

# frozen_string_literal: true
require_relative "../model"
class ApiKey < Sequel::Model
include ResourceMethods
plugin :column_encryption do |enc|
enc.column :key
end
def self.ubid_type
UBID::TYPE_ETC
end
def self.create_with_id(owner_table:, owner_id:, used_for:)
unless ["project", "inference_endpoint"].include?(owner_table.to_s)
fail "Invalid owner_table: #{owner_table}"
end
key = SecureRandom.alphanumeric(32)
super(owner_table: owner_table, owner_id: owner_id, key: key, used_for: used_for)
end
def rotate
new_key = SecureRandom.alphanumeric(32)
update(key: new_key, updated_at: Time.now)
end
end
# Table: api_key
# Columns:
# id | uuid | PRIMARY KEY
# created_at | timestamp with time zone | NOT NULL DEFAULT now()
# updated_at | timestamp with time zone | NOT NULL DEFAULT now()
# owner_table | text | NOT NULL
# owner_id | uuid | NOT NULL
# used_for | text | NOT NULL
# key | text | NOT NULL
# is_valid | boolean | NOT NULL DEFAULT true
# Indexes:
# api_key_pkey | PRIMARY KEY btree (id)
# api_key_owner_table_owner_id_index | btree (owner_table, owner_id)