65 lines
1.8 KiB
Elixir
65 lines
1.8 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.
|
|
"""
|
|
|
|
use Phoenix.VerifiedRoutes, endpoint: PostlandWeb.Endpoint, router: PostlandWeb.Router
|
|
|
|
require Logger
|
|
|
|
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
|
|
File.mkdir_p!(Postland.Uploads.upload_path())
|
|
Logger.info("File upload path: #{Postland.Uploads.upload_path()}")
|
|
|
|
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
|
|
|
|
def my_actor_id do
|
|
url(~p"/actor")
|
|
end
|
|
end
|