1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.
11 Fetch scopes from request params.
13 Note: `scopes` is used by Mastodon — supporting it but sticking to
14 OAuth's standard `scope` wherever we control it
16 @spec fetch_scopes(map(), list()) :: list()
17 def fetch_scopes(params, default) do
18 parse_scopes(params["scope"] || params["scopes"], default)
21 def parse_scopes(scopes, _default) when is_list(scopes) do
22 Enum.filter(scopes, &(&1 not in [nil, ""]))
25 def parse_scopes(scopes, default) when is_binary(scopes) do
28 |> parse_scopes(default)
31 def parse_scopes(_, default) do
36 Convert scopes string to list
38 @spec to_list(binary()) :: [binary()]
39 def to_list(nil), do: []
44 |> String.split(~r/[\s,]+/)
48 Convert scopes list to string
50 @spec to_string(list()) :: binary()
51 def to_string(scopes), do: Enum.join(scopes, " ")
56 @spec validate(list() | nil, list()) ::
57 {:ok, list()} | {:error, :missing_scopes | :unsupported_scopes}
58 def validate([], _app_scopes), do: {:error, :missing_scopes}
59 def validate(nil, _app_scopes), do: {:error, :missing_scopes}
61 def validate(scopes, app_scopes) do
62 case Pleroma.Plugs.OAuthScopesPlug.filter_descendants(scopes, app_scopes) do
63 ^scopes -> {:ok, scopes}
64 _ -> {:error, :unsupported_scopes}