postland/lib/postland.ex

66 lines
1.8 KiB
Elixir
Raw Normal View History

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.
"""
2024-10-09 22:45:28 +00:00
use Phoenix.VerifiedRoutes, endpoint: PostlandWeb.Endpoint, router: PostlandWeb.Router
2024-10-26 21:51:05 +00:00
require Logger
2024-09-20 19:30:46 +00:00
alias Postland.Accounts
2024-09-21 22:10:43 +00:00
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
2024-09-21 22:10:43 +00:00
def on_boot_setup() do
2024-10-26 21:51:05 +00:00
File.mkdir_p!(Postland.Uploads.upload_path())
Logger.info("File upload path: #{Postland.Uploads.upload_path()}")
2024-09-21 22:10:43 +00:00
if !setup?() do
2024-09-24 13:16:54 +00:00
# TODO: Require the DB have restrictive permissions
%{public: public_key, private: private_key} = generate_keys()
2024-09-21 22:10:43 +00:00
attrs = %{
username: "welcome",
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
2024-10-09 22:45:28 +00:00
def my_actor_id do
url(~p"/actor")
end
2024-09-20 19:30:46 +00:00
end