You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
analytics/lib/plausible/auth/api_key.ex

50 lines
1.1 KiB
Elixir

defmodule Plausible.Auth.ApiKey do
use Ecto.Schema
import Ecto.Changeset
@required [:user_id, :key, :name]
@optional [:scopes, :hourly_request_limit]
schema "api_keys" do
field :name, :string
field :scopes, {:array, :string}, default: ["stats:read:*"]
field :hourly_request_limit, :integer
field :key, :string, virtual: true
field :key_hash, :string
field :key_prefix, :string
belongs_to :user, Plausible.Auth.User
timestamps()
end
def changeset(schema, attrs \\ %{}) do
schema
|> cast(attrs, @required ++ @optional)
|> validate_required(@required)
|> process_key
end
def do_hash(key) do
:crypto.hash(:sha256, [secret_key_base(), key])
|> Base.encode16()
|> String.downcase()
end
def process_key(%{errors: [], changes: changes} = changeset) do
prefix = binary_part(changes[:key], 0, 6)
change(changeset,
key_hash: do_hash(changes[:key]),
key_prefix: prefix
)
end
def process_key(changeset), do: changeset
defp secret_key_base() do
Application.get_env(:plausible, PlausibleWeb.Endpoint)
|> Keyword.fetch!(:secret_key_base)
end
end