2019-05-08 10:52:13 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 05:08:45 +00:00
|
|
|
# Copyright © 2017-2020 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.
|
|
|
|
"""
|
|
|
|
|
2019-11-29 15:57:19 +00:00
|
|
|
alias Pleroma.Plugs.OAuthScopesPlug
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
def fetch_scopes(%Pleroma.Web.ApiSpec.Schemas.AppCreateRequest{scopes: scopes}, default) do
|
|
|
|
parse_scopes(scopes, default)
|
|
|
|
end
|
|
|
|
|
2019-05-08 10:52:13 +00:00
|
|
|
def fetch_scopes(params, default) do
|
|
|
|
parse_scopes(params["scope"] || params["scopes"], default)
|
|
|
|
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.
|
|
|
|
"""
|
2020-01-10 07:52:21 +00:00
|
|
|
@spec validate(list() | nil, list()) ::
|
2019-05-08 10:52:13 +00:00
|
|
|
{:ok, list()} | {:error, :missing_scopes | :unsupported_scopes}
|
2020-01-10 07:52:21 +00:00
|
|
|
def validate(blank_scopes, _app_scopes) 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
|
|
|
|
2020-01-10 07:52:21 +00:00
|
|
|
def validate(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
|