2024-09-20 19:30:46 +00:00
|
|
|
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
|
|
|
|
|
|
2024-09-21 22:10:43 +00:00
|
|
|
def temporary_password() do
|
|
|
|
|
:crypto.strong_rand_bytes(12) |> Base.encode64()
|
|
|
|
|
end
|
|
|
|
|
|
2024-09-22 00:16:10 +00:00
|
|
|
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
|
|
|
|
|
|
2024-09-21 22:10:43 +00:00
|
|
|
def on_boot_setup() do
|
|
|
|
|
if !setup?() do
|
2024-09-24 13:16:54 +00:00
|
|
|
# TODO: Require the DB have restrictive permissions
|
2024-09-22 00:16:10 +00:00
|
|
|
%{public: public_key, private: private_key} = generate_keys()
|
|
|
|
|
|
2024-09-21 22:10:43 +00:00
|
|
|
attrs = %{
|
|
|
|
|
username: "welcome",
|
2024-09-22 00:16:10 +00:00
|
|
|
password: temporary_password(),
|
|
|
|
|
private_key: private_key,
|
|
|
|
|
public_key: public_key
|
2024-09-21 22:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{:ok, _user} = Accounts.register_user(attrs)
|
|
|
|
|
|
|
|
|
|
IO.puts(
|
|
|
|
|
"Your temporary username is #{attrs.username} and your temporary password is #{attrs.password}"
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-09-20 19:30:46 +00:00
|
|
|
def setup?() do
|
|
|
|
|
Accounts.solo_user() != nil
|
|
|
|
|
end
|
|
|
|
|
end
|