Merge branch 'openapi/use-boolean-like-in-requests' into 'develop'
[akkoma] / lib / pleroma / web / oauth / token / response.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.OAuth.Token.Response do
6 @moduledoc false
7
8 alias Pleroma.MFA
9 alias Pleroma.User
10 alias Pleroma.Web.OAuth.Token.Utils
11
12 @doc false
13 def build(%User{} = user, token, opts \\ %{}) do
14 %{
15 token_type: "Bearer",
16 access_token: token.token,
17 refresh_token: token.refresh_token,
18 expires_in: expires_in(),
19 scope: Enum.join(token.scopes, " "),
20 me: user.ap_id
21 }
22 |> Map.merge(opts)
23 end
24
25 def build_for_client_credentials(token) do
26 %{
27 token_type: "Bearer",
28 access_token: token.token,
29 refresh_token: token.refresh_token,
30 created_at: Utils.format_created_at(token),
31 expires_in: expires_in(),
32 scope: Enum.join(token.scopes, " ")
33 }
34 end
35
36 def build_for_mfa_token(user, mfa_token) do
37 %{
38 error: "mfa_required",
39 mfa_token: mfa_token.token,
40 supported_challenge_types: MFA.supported_methods(user)
41 }
42 end
43
44 defp expires_in, do: Pleroma.Config.get([:oauth2, :token_expires_in], 600)
45 end