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.
29 lines
723 B
Elixir
29 lines
723 B
Elixir
defmodule Asciinema.ApiToken do
|
|
use Asciinema.Web, :model
|
|
alias Asciinema.{ApiToken, User}
|
|
|
|
schema "api_tokens" do
|
|
field :token, :string
|
|
field :revoked_at, Timex.Ecto.DateTime
|
|
|
|
timestamps(inserted_at: :created_at)
|
|
|
|
belongs_to :user, Asciinema.User
|
|
end
|
|
|
|
@uuid4 ~r/\A[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\z/
|
|
|
|
def create_changeset(%User{id: user_id}, token) do
|
|
%ApiToken{user_id: user_id}
|
|
|> change(%{token: token})
|
|
|> validate_format(:token, @uuid4)
|
|
end
|
|
|
|
def revoke_changeset(%ApiToken{revoked_at: nil} = api_token) do
|
|
change(api_token, %{revoked_at: Timex.now()})
|
|
end
|
|
def revoke_changeset(%ApiToken{} = api_token) do
|
|
change(api_token)
|
|
end
|
|
end
|