defmodule Mixer.Accounts.ApiKey do use Ash.Resource, otp_app: :mixer, domain: Mixer.Accounts, data_layer: AshPostgres.DataLayer, authorizers: [Ash.Policy.Authorizer] postgres do table "api_keys" repo Mixer.Repo end actions do defaults [:read, :destroy] create :create do primary? true accept [:user_id, :expires_at] change {AshAuthentication.Strategy.ApiKey.GenerateApiKey, prefix: :mixer, hash: :api_key_hash} end end policies do bypass AshAuthentication.Checks.AshAuthenticationInteraction do authorize_if always() end end attributes do uuid_primary_key :id attribute :api_key_hash, :binary do allow_nil? false sensitive? true end attribute :expires_at, :utc_datetime_usec do allow_nil? false end end relationships do belongs_to :user, Mixer.Accounts.User end calculations do calculate :valid, :boolean, expr(expires_at > now()) end identities do identity :unique_api_key, [:api_key_hash] end end