Add extraction process for oauth scopes
This commit is contained in:
parent
d97425d49e
commit
85bdbb102e
3 changed files with 148 additions and 0 deletions
82
lib/pleroma/web/api_spec/scopes/compiler.ex
Normal file
82
lib/pleroma/web/api_spec/scopes/compiler.ex
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.ApiSpec.Scopes.Compiler do
|
||||||
|
defmacro __before_compile__(_env) do
|
||||||
|
strings = __MODULE__.extract_all_scopes()
|
||||||
|
|
||||||
|
quote do
|
||||||
|
def placeholder do
|
||||||
|
unquote do
|
||||||
|
Enum.map(
|
||||||
|
strings,
|
||||||
|
fn string ->
|
||||||
|
quote do
|
||||||
|
Pleroma.Web.Gettext.dgettext_noop(
|
||||||
|
"oauth_scopes",
|
||||||
|
unquote(string)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def extract_all_scopes do
|
||||||
|
extract_all_scopes_from(Pleroma.Web.ApiSpec.spec())
|
||||||
|
end
|
||||||
|
|
||||||
|
def extract_all_scopes_from(specs) do
|
||||||
|
specs.paths
|
||||||
|
|> Enum.reduce([], fn
|
||||||
|
{_path, %{} = path_item}, acc ->
|
||||||
|
extract_routes(path_item)
|
||||||
|
|> Enum.flat_map(fn operation -> process_operation(operation) end)
|
||||||
|
|> Kernel.++(acc)
|
||||||
|
|
||||||
|
{_, _}, acc ->
|
||||||
|
acc
|
||||||
|
end)
|
||||||
|
|> Enum.uniq()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp extract_routes(path_item) do
|
||||||
|
path_item
|
||||||
|
|> Map.from_struct()
|
||||||
|
|> Enum.map(fn {_method, path_item} -> path_item end)
|
||||||
|
|> Enum.filter(fn
|
||||||
|
%OpenApiSpex.Operation{} = _operation -> true
|
||||||
|
_ -> false
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp process_operation(operation) do
|
||||||
|
operation.security
|
||||||
|
|> Kernel.||([])
|
||||||
|
|> Enum.flat_map(fn
|
||||||
|
%{"oAuth" => scopes} -> process_scopes(scopes)
|
||||||
|
_ -> []
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp process_scopes(scopes) do
|
||||||
|
scopes
|
||||||
|
|> Enum.flat_map(fn scope ->
|
||||||
|
process_scope(scope)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def process_scope(scope) do
|
||||||
|
hierarchy = String.split(scope, ":")
|
||||||
|
|
||||||
|
{_, list} =
|
||||||
|
Enum.reduce(hierarchy, {"", []}, fn comp, {cur, list} ->
|
||||||
|
{cur <> comp <> ":", [cur <> comp | list]}
|
||||||
|
end)
|
||||||
|
|
||||||
|
list
|
||||||
|
end
|
||||||
|
end
|
10
lib/pleroma/web/api_spec/scopes/translator.ex
Normal file
10
lib/pleroma/web/api_spec/scopes/translator.ex
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.ApiSpec.Scopes.Translator do
|
||||||
|
require Pleroma.Web.ApiSpec.Scopes.Compiler
|
||||||
|
require Pleroma.Web.Gettext
|
||||||
|
|
||||||
|
@before_compile Pleroma.Web.ApiSpec.Scopes.Compiler
|
||||||
|
end
|
56
test/pleroma/web/api_spec/scopes/compiler_test.exs
Normal file
56
test/pleroma/web/api_spec/scopes/compiler_test.exs
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.ApiSpec.Scopes.CompilerTest do
|
||||||
|
use ExUnit.Case, async: true
|
||||||
|
|
||||||
|
alias Pleroma.Web.ApiSpec.Scopes.Compiler
|
||||||
|
|
||||||
|
@dummy_response %{}
|
||||||
|
|
||||||
|
@data %{
|
||||||
|
paths: %{
|
||||||
|
"/mew" => %OpenApiSpex.PathItem{
|
||||||
|
post: %OpenApiSpex.Operation{
|
||||||
|
security: [%{"oAuth" => ["a:b:c"]}],
|
||||||
|
responses: @dummy_response
|
||||||
|
},
|
||||||
|
get: %OpenApiSpex.Operation{security: nil, responses: @dummy_response}
|
||||||
|
},
|
||||||
|
"/mew2" => %OpenApiSpex.PathItem{
|
||||||
|
post: %OpenApiSpex.Operation{
|
||||||
|
security: [%{"oAuth" => ["d:e", "f:g"]}],
|
||||||
|
responses: @dummy_response
|
||||||
|
},
|
||||||
|
get: %OpenApiSpex.Operation{security: nil, responses: @dummy_response}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe "process_scope/1" do
|
||||||
|
test "gives all higher-level scopes" do
|
||||||
|
scopes = Compiler.process_scope("admin:read:accounts")
|
||||||
|
|
||||||
|
assert [_, _, _] = scopes
|
||||||
|
assert "admin" in scopes
|
||||||
|
assert "admin:read" in scopes
|
||||||
|
assert "admin:read:accounts" in scopes
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "extract_all_scopes_from/1" do
|
||||||
|
test "extracts scopes" do
|
||||||
|
scopes = Compiler.extract_all_scopes_from(@data)
|
||||||
|
|
||||||
|
assert [_, _, _, _, _, _, _] = scopes
|
||||||
|
assert "a" in scopes
|
||||||
|
assert "a:b" in scopes
|
||||||
|
assert "a:b:c" in scopes
|
||||||
|
assert "d" in scopes
|
||||||
|
assert "d:e" in scopes
|
||||||
|
assert "f" in scopes
|
||||||
|
assert "f:g" in scopes
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue