2019-05-08 10:52:13 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-05-08 10:52:13 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.OAuth.Scopes do
|
|
|
|
@moduledoc """
|
|
|
|
Functions for dealing with scopes.
|
|
|
|
"""
|
|
|
|
|
2020-06-24 06:57:27 +00:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
2019-11-29 15:57:19 +00:00
|
|
|
|
2019-05-08 10:52:13 +00:00
|
|
|
@doc """
|
2019-09-08 12:00:03 +00:00
|
|
|
Fetch scopes from request params.
|
2019-05-08 10:52:13 +00:00
|
|
|
|
|
|
|
Note: `scopes` is used by Mastodon — supporting it but sticking to
|
|
|
|
OAuth's standard `scope` wherever we control it
|
|
|
|
"""
|
2020-04-01 19:00:59 +00:00
|
|
|
@spec fetch_scopes(map() | struct(), list()) :: list()
|
|
|
|
|
2019-05-08 10:52:13 +00:00
|
|
|
def fetch_scopes(params, default) do
|
2020-04-24 10:46:59 +00:00
|
|
|
parse_scopes(params["scope"] || params["scopes"] || params[:scopes], default)
|
2019-05-08 10:52:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_scopes(scopes, _default) when is_list(scopes) do
|
|
|
|
Enum.filter(scopes, &(&1 not in [nil, ""]))
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_scopes(scopes, default) when is_binary(scopes) do
|
|
|
|
scopes
|
|
|
|
|> to_list
|
|
|
|
|> parse_scopes(default)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_scopes(_, default) do
|
|
|
|
default
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Convert scopes string to list
|
|
|
|
"""
|
|
|
|
@spec to_list(binary()) :: [binary()]
|
|
|
|
def to_list(nil), do: []
|
|
|
|
|
|
|
|
def to_list(str) do
|
|
|
|
str
|
|
|
|
|> String.trim()
|
|
|
|
|> String.split(~r/[\s,]+/)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Convert scopes list to string
|
|
|
|
"""
|
|
|
|
@spec to_string(list()) :: binary()
|
|
|
|
def to_string(scopes), do: Enum.join(scopes, " ")
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Validates scopes.
|
|
|
|
"""
|
2022-12-16 03:25:14 +00:00
|
|
|
@spec validate(list() | nil, list(), Pleroma.User.t()) ::
|
|
|
|
{:ok, list()} | {:error, :missing_scopes | :unsupported_scopes, :user_is_not_an_admin}
|
|
|
|
def validate(blank_scopes, _app_scopes, _user) when blank_scopes in [nil, []],
|
2019-11-29 15:57:19 +00:00
|
|
|
do: {:error, :missing_scopes}
|
2019-05-08 10:52:13 +00:00
|
|
|
|
2022-12-19 20:41:48 +00:00
|
|
|
def validate(scopes, app_scopes, _user) do
|
2022-12-17 23:21:24 +00:00
|
|
|
validate_scopes_are_supported(scopes, app_scopes)
|
2022-12-16 03:25:14 +00:00
|
|
|
end
|
|
|
|
|
2022-12-17 23:14:49 +00:00
|
|
|
@spec filter_admin_scopes([String.t()], Pleroma.User.t()) :: [String.t()]
|
|
|
|
@doc """
|
|
|
|
Remove admin scopes for non-admins
|
|
|
|
"""
|
|
|
|
def filter_admin_scopes(scopes, %Pleroma.User{is_admin: true}), do: scopes
|
|
|
|
|
2023-03-08 17:39:35 +00:00
|
|
|
def filter_admin_scopes(scopes, %Pleroma.User{is_moderator: true}), do: scopes
|
|
|
|
|
2022-12-17 23:14:49 +00:00
|
|
|
def filter_admin_scopes(scopes, _user) do
|
|
|
|
drop_scopes = OAuthScopesPlug.filter_descendants(scopes, ["admin"])
|
|
|
|
Enum.reject(scopes, fn scope -> Enum.member?(drop_scopes, scope) end)
|
|
|
|
end
|
|
|
|
|
2022-12-16 03:25:14 +00:00
|
|
|
defp validate_scopes_are_supported(scopes, app_scopes) do
|
2019-11-29 15:57:19 +00:00
|
|
|
case OAuthScopesPlug.filter_descendants(scopes, app_scopes) do
|
2019-09-08 12:00:03 +00:00
|
|
|
^scopes -> {:ok, scopes}
|
2019-05-08 10:52:13 +00:00
|
|
|
_ -> {:error, :unsupported_scopes}
|
|
|
|
end
|
|
|
|
end
|
2019-11-29 15:57:19 +00:00
|
|
|
|
2019-12-05 21:25:44 +00:00
|
|
|
def contains_admin_scopes?(scopes) do
|
2019-11-29 15:57:19 +00:00
|
|
|
scopes
|
|
|
|
|> OAuthScopesPlug.filter_descendants(["admin"])
|
|
|
|
|> Enum.any?()
|
|
|
|
end
|
2019-05-08 10:52:13 +00:00
|
|
|
end
|