1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OAuth.Scopes do
7 Functions for dealing with scopes.
10 alias Pleroma.Plugs.OAuthScopesPlug
13 Fetch scopes from request params.
15 Note: `scopes` is used by Mastodon — supporting it but sticking to
16 OAuth's standard `scope` wherever we control it
18 @spec fetch_scopes(map(), list()) :: list()
19 def fetch_scopes(params, default) do
20 parse_scopes(params["scope"] || params["scopes"], default)
23 def parse_scopes(scopes, _default) when is_list(scopes) do
24 Enum.filter(scopes, &(&1 not in [nil, ""]))
27 def parse_scopes(scopes, default) when is_binary(scopes) do
30 |> parse_scopes(default)
33 def parse_scopes(_, default) do
38 Convert scopes string to list
40 @spec to_list(binary()) :: [binary()]
41 def to_list(nil), do: []
46 |> String.split(~r/[\s,]+/)
50 Convert scopes list to string
52 @spec to_string(list()) :: binary()
53 def to_string(scopes), do: Enum.join(scopes, " ")
58 @spec validate(list() | nil, list()) ::
59 {:ok, list()} | {:error, :missing_scopes | :unsupported_scopes}
60 def validate(blank_scopes, _app_scopes) when blank_scopes in [nil, []],
61 do: {:error, :missing_scopes}
63 def validate(scopes, app_scopes) do
64 case OAuthScopesPlug.filter_descendants(scopes, app_scopes) do
65 ^scopes -> {:ok, scopes}
66 _ -> {:error, :unsupported_scopes}
70 def contains_admin_scopes?(scopes) do
72 |> OAuthScopesPlug.filter_descendants(["admin"])