Files
archived-hipudding-teslamate/lib/teslamate/auth.ex
Adrian Kumpf 0d6e288c70 Encrypt API tokens (#2360)
Store API tokens encrypted in the database.

During the database migration a randomly generated key will be used encrypt the tokens if no ENCRYPTION_KEY environment variable was provided.

If the application is started without the presence of an ENCRYPTION_KEY (or if the key failed to decrypt the existing tokens), the UI will display a warning with further instructions.
2022-02-18 17:03:13 +01:00

72 lines
1.3 KiB
Elixir

defmodule TeslaMate.Auth do
@moduledoc """
The Auth context.
"""
import Ecto.Query, warn: false
require Logger
alias TeslaMate.Repo
### Tokens
alias TeslaMate.Auth.Tokens
def change_tokens(attrs \\ %{}) do
%Tokens{} |> Tokens.changeset(attrs)
end
def can_decrypt_tokens? do
case get_tokens() do
%Tokens{} = tokens ->
is_binary(tokens.access) and is_binary(tokens.refresh)
nil ->
true
end
end
def get_tokens do
case Repo.all(Tokens) do
[%Tokens{} = tokens] ->
tokens
[_ | _] = tokens ->
raise """
Found #{length(tokens)} token pairs!
Make sure that there is no more than ONE token pair in the table 'tokens'.
"""
[] ->
nil
end
end
def save(%{token: access, refresh_token: refresh}) do
attrs = %{access: access, refresh: refresh}
maybe_created_or_updated =
case get_tokens() do
nil -> create_tokens(attrs)
tokens -> update_tokens(tokens, attrs)
end
with {:ok, _tokens} <- maybe_created_or_updated do
:ok
end
end
defp create_tokens(attrs) do
%Tokens{}
|> Tokens.changeset(attrs)
|> Repo.insert()
end
defp update_tokens(%Tokens{} = tokens, attrs) do
tokens
|> Tokens.changeset(attrs)
|> Repo.update()
end
end