postland/lib/postland.ex
2024-09-24 08:16:54 -05:00

54 lines
1.5 KiB
Elixir

defmodule Postland do
@moduledoc """
Postland keeps the contexts that define your domain
and business logic.
Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.
"""
alias Postland.Accounts
def temporary_password() do
:crypto.strong_rand_bytes(12) |> Base.encode64()
end
def generate_keys(bits \\ 4096) do
{:RSAPrivateKey, _, modulus, publicExponent, _, _, _, _exponent1, _, _, _otherPrimeInfos} =
rsa_private_key = :public_key.generate_key({:rsa, bits, 65537})
rsa_public_key = {:RSAPublicKey, modulus, publicExponent}
pem_entry = :public_key.pem_entry_encode(:RSAPrivateKey, rsa_private_key)
private_key = :public_key.pem_encode([pem_entry])
pem_entry = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, rsa_public_key)
public_key = :public_key.pem_encode([pem_entry])
%{public: public_key, private: private_key}
end
def on_boot_setup() do
if !setup?() do
# TODO: Require the DB have restrictive permissions
%{public: public_key, private: private_key} = generate_keys()
attrs = %{
username: "welcome",
password: temporary_password(),
private_key: private_key,
public_key: public_key
}
{:ok, _user} = Accounts.register_user(attrs)
IO.puts(
"Your temporary username is #{attrs.username} and your temporary password is #{attrs.password}"
)
end
end
def setup?() do
Accounts.solo_user() != nil
end
end