move to 2.5.5
[anni] / lib / mix / tasks / pleroma / app.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.App do
6   @moduledoc File.read!("docs/administration/CLI_tasks/oauth_app.md")
7   use Mix.Task
8
9   import Mix.Pleroma
10
11   @shortdoc "Creates trusted OAuth App"
12
13   def run(["create" | options]) do
14     start_pleroma()
15
16     {opts, _} =
17       OptionParser.parse!(options,
18         strict: [name: :string, redirect_uri: :string, scopes: :string],
19         aliases: [n: :name, r: :redirect_uri, s: :scopes]
20       )
21
22     scopes =
23       if opts[:scopes] do
24         String.split(opts[:scopes], ",")
25       else
26         ["read", "write", "follow", "push"]
27       end
28
29     params = %{
30       client_name: opts[:name],
31       redirect_uris: opts[:redirect_uri],
32       trusted: true,
33       scopes: scopes
34     }
35
36     with {:ok, app} <- Pleroma.Web.OAuth.App.create(params) do
37       shell_info("#{app.client_name} successfully created:")
38       shell_info("App client_id: " <> app.client_id)
39       shell_info("App client_secret: " <> app.client_secret)
40     else
41       {:error, changeset} ->
42         shell_error("Creating failed:")
43
44         Enum.each(Pleroma.Web.OAuth.App.errors(changeset), fn {key, error} ->
45           shell_error("#{key}: #{error}")
46         end)
47     end
48   end
49 end