1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Config do
6 @behaviour Pleroma.Config.Getting
8 defexception [:message]
12 def get(key), do: get(key, nil)
15 def get([key], default), do: get(key, default)
18 def get([_ | _] = path, default) do
26 def get(key, default) do
27 Application.get_env(:pleroma, key, default)
34 raise(Error, message: "Missing configuration value: #{inspect(key)}")
40 def fetch(key) when is_atom(key), do: fetch([key])
42 def fetch([root_key | keys]) do
43 Enum.reduce_while(keys, Application.fetch_env(:pleroma, root_key), fn
44 key, {:ok, config} when is_map(config) or is_list(config) ->
45 case Access.fetch(config, key) do
58 def put([key], value), do: put(key, value)
60 def put([parent_key | keys], value) do
62 Application.get_env(:pleroma, parent_key, [])
63 |> put_in(keys, value)
65 Application.put_env(:pleroma, parent_key, parent)
68 def put(key, value) do
69 Application.put_env(:pleroma, key, value)
72 def delete([key]), do: delete(key)
74 def delete([parent_key | keys] = path) do
75 with {:ok, _} <- fetch(path) do
79 |> get_and_update_in(keys, fn _ -> :pop end)
81 Application.put_env(:pleroma, parent_key, parent)
86 Application.delete_env(:pleroma, key)
89 def restrict_unauthenticated_access?(resource, kind) do
90 setting = get([:restrict_unauthenticated, resource, kind])
92 if setting in [nil, :if_instance_is_private] do
93 !get!([:instance, :public])
99 def oauth_consumer_strategies, do: get([:auth, :oauth_consumer_strategies], [])
101 def oauth_consumer_enabled?, do: oauth_consumer_strategies() != []
103 def enforce_oauth_admin_scope_usage?, do: !!get([:auth, :enforce_oauth_admin_scope_usage])
105 def oauth_admin_scopes(scopes) when is_list(scopes) do
109 ["admin:#{scope}"] ++
110 if enforce_oauth_admin_scope_usage?(), do: [], else: [scope]