Add keys to instance_settings
This commit is contained in:
parent
da4cdc612a
commit
f3c35e8e55
5 changed files with 52 additions and 10 deletions
16
lib/nulla/key_gen.ex
Normal file
16
lib/nulla/key_gen.ex
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
defmodule Nulla.KeyGen do
|
||||||
|
def generate_keys do
|
||||||
|
rsa_key = :public_key.generate_key({:rsa, 2048, 65537})
|
||||||
|
|
||||||
|
{:RSAPrivateKey, :"two-prime", n, e, _d, _p, _q, _dp, _dq, _qi, _other} = rsa_key
|
||||||
|
public_key = {:RSAPublicKey, n, e}
|
||||||
|
|
||||||
|
private_entry = {:PrivateKeyInfo, :public_key.der_encode(:RSAPrivateKey, rsa_key), :not_encrypted}
|
||||||
|
public_entry = {:SubjectPublicKeyInfo, :public_key.der_encode(:RSAPublicKey, public_key), :not_encrypted}
|
||||||
|
|
||||||
|
private_pem = :public_key.pem_encode([private_entry])
|
||||||
|
public_pem = :public_key.pem_encode([public_entry])
|
||||||
|
|
||||||
|
{public_pem, private_pem}
|
||||||
|
end
|
||||||
|
end
|
|
@ -11,7 +11,7 @@ defmodule Nulla.Models.InstanceSettings do
|
||||||
field :registration, :boolean, default: false
|
field :registration, :boolean, default: false
|
||||||
field :max_characters, :integer, default: 5000
|
field :max_characters, :integer, default: 5000
|
||||||
field :max_upload_size, :integer, default: 50
|
field :max_upload_size, :integer, default: 50
|
||||||
field :offset, :integer, default: 100
|
field :api_offset, :integer, default: 100
|
||||||
field :public_key, :string
|
field :public_key, :string
|
||||||
field :private_key, :string
|
field :private_key, :string
|
||||||
end
|
end
|
||||||
|
@ -26,7 +26,7 @@ defmodule Nulla.Models.InstanceSettings do
|
||||||
:registration,
|
:registration,
|
||||||
:max_characters,
|
:max_characters,
|
||||||
:max_upload_size,
|
:max_upload_size,
|
||||||
:offset,
|
:api_offset,
|
||||||
:public_key,
|
:public_key,
|
||||||
:private_key
|
:private_key
|
||||||
])
|
])
|
||||||
|
@ -37,7 +37,7 @@ defmodule Nulla.Models.InstanceSettings do
|
||||||
:registration,
|
:registration,
|
||||||
:max_characters,
|
:max_characters,
|
||||||
:max_upload_size,
|
:max_upload_size,
|
||||||
:offset,
|
:api_offset,
|
||||||
:public_key,
|
:public_key,
|
||||||
:private_key
|
:private_key
|
||||||
])
|
])
|
||||||
|
|
|
@ -28,7 +28,7 @@ defmodule Nulla.Utils do
|
||||||
|
|
||||||
%User{id: user_id} ->
|
%User{id: user_id} ->
|
||||||
instance_settings = InstanceSettings.get_instance_settings!()
|
instance_settings = InstanceSettings.get_instance_settings!()
|
||||||
per_page = instance_settings.offset
|
per_page = instance_settings.api_offset
|
||||||
offset = (page - 1) * per_page
|
offset = (page - 1) * per_page
|
||||||
|
|
||||||
query =
|
query =
|
||||||
|
@ -67,7 +67,7 @@ defmodule Nulla.Utils do
|
||||||
|
|
||||||
%User{id: user_id} ->
|
%User{id: user_id} ->
|
||||||
instance_settings = InstanceSettings.get_instance_settings!()
|
instance_settings = InstanceSettings.get_instance_settings!()
|
||||||
per_page = instance_settings.offset
|
per_page = instance_settings.api_offset
|
||||||
offset = (page - 1) * per_page
|
offset = (page - 1) * per_page
|
||||||
|
|
||||||
query =
|
query =
|
||||||
|
|
|
@ -4,16 +4,42 @@ defmodule Nulla.Repo.Migrations.CreateInstanceSettings do
|
||||||
def change do
|
def change do
|
||||||
create table(:instance_settings) do
|
create table(:instance_settings) do
|
||||||
add :name, :string, default: "Nulla", null: false
|
add :name, :string, default: "Nulla", null: false
|
||||||
add :description, :string, default: "Freedom Social Network", null: false
|
add :description, :text, default: "Freedom Social Network", null: false
|
||||||
add :domain, :string, default: "localhost", null: false
|
add :domain, :string, default: "localhost", null: false
|
||||||
add :registration, :boolean, default: false, null: false
|
add :registration, :boolean, default: false, null: false
|
||||||
add :max_characters, :integer, default: 5000, null: false
|
add :max_characters, :integer, default: 5000, null: false
|
||||||
add :max_upload_size, :integer, default: 50, null: false
|
add :max_upload_size, :integer, default: 50, null: false
|
||||||
add :offset, :integer, default: 100, null: false
|
add :api_offset, :integer, default: 100, null: false
|
||||||
add :public_key, :string
|
add :public_key, :text
|
||||||
add :private_key, :string
|
add :private_key, :text
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
flush()
|
||||||
|
|
||||||
|
execute(fn ->
|
||||||
|
{public_key, private_key} = Nulla.KeyGen.generate_keys()
|
||||||
|
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
||||||
|
domain =
|
||||||
|
Application.get_env(:nulla, NullaWeb.Endpoint, [])
|
||||||
|
|> Keyword.get(:url, [])
|
||||||
|
|> Keyword.get(:host, "localhost")
|
||||||
|
esc = fn str -> "'#{String.replace(str, "'", "''")}'" end
|
||||||
|
sql = """
|
||||||
|
INSERT INTO instance_settings (
|
||||||
|
name, description, domain, registration,
|
||||||
|
max_characters, max_upload_size, api_offset,
|
||||||
|
public_key, private_key, inserted_at, updated_at
|
||||||
|
) VALUES (
|
||||||
|
'Nulla', 'Freedom Social Network', '#{domain}', false,
|
||||||
|
5000, 50, 100,
|
||||||
|
#{esc.(public_key)}, #{esc.(private_key)},
|
||||||
|
'#{now}', '#{now}'
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
|
||||||
|
Ecto.Adapters.SQL.query!(Nulla.Repo, sql, [])
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,7 @@ defmodule Nulla.Repo.Migrations.CreateNotes do
|
||||||
|
|
||||||
def change do
|
def change do
|
||||||
create table(:notes) do
|
create table(:notes) do
|
||||||
add :content, :string
|
add :content, :text
|
||||||
add :visibility, :string, default: "public"
|
add :visibility, :string, default: "public"
|
||||||
add :sensitive, :boolean, default: false
|
add :sensitive, :boolean, default: false
|
||||||
add :language, :string
|
add :language, :string
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue