[#1234] Mastodon 2.4.3 hierarchical scopes initial support (WIP).

This commit is contained in:
Ivan Tashkinov 2019-09-08 15:00:03 +03:00
commit b63faf9819
8 changed files with 113 additions and 30 deletions

View file

@ -13,15 +13,16 @@ defmodule Pleroma.Plugs.OAuthScopesPlug do
def call(%Plug.Conn{assigns: assigns} = conn, %{scopes: scopes} = options) do
op = options[:op] || :|
token = assigns[:token]
matched_scopes = token && filter_descendants(scopes, token.scopes)
cond do
is_nil(token) ->
conn
op == :| && scopes -- token.scopes != scopes ->
op == :| && Enum.any?(matched_scopes) ->
conn
op == :& && scopes -- token.scopes == [] ->
op == :& && matched_scopes == scopes ->
conn
options[:fallback] == :proceed_unauthenticated ->
@ -30,7 +31,7 @@ defmodule Pleroma.Plugs.OAuthScopesPlug do
|> assign(:token, nil)
true ->
missing_scopes = scopes -- token.scopes
missing_scopes = scopes -- matched_scopes
permissions = Enum.join(missing_scopes, " #{op} ")
error_message =
@ -42,4 +43,17 @@ defmodule Pleroma.Plugs.OAuthScopesPlug do
|> halt()
end
end
@doc "Filters descendants of supported scopes"
def filter_descendants(scopes, supported_scopes) do
Enum.filter(
scopes,
fn scope ->
Enum.find(
supported_scopes,
&(scope == &1 || String.starts_with?(scope, &1 <> ":"))
)
end
)
end
end