Restrict mastodon api announcements to logged-in users only

This commit is contained in:
Tusooa Zhu 2022-04-02 02:25:13 -04:00
commit 7d1dae3bef
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
4 changed files with 38 additions and 21 deletions

View file

@ -18,6 +18,7 @@ defmodule Pleroma.Web.ApiSpec.AnnouncementOperation do
tags: ["Announcement"],
summary: "Retrieve a list of announcements",
operationId: "MastodonAPI.AnnouncementController.index",
security: [%{"oAuth" => []}],
responses: %{
200 => Operation.response("Response", "application/json", list_of_announcements()),
403 => Operation.response("Forbidden", "application/json", ApiError)

View file

@ -15,20 +15,18 @@ defmodule Pleroma.Web.MastodonAPI.AnnouncementController do
plug(Pleroma.Web.ApiSpec.CastAndValidate)
# MastodonAPI specs do not have oauth requirements for showing
# announcements, but we have "private instance" options. When that
# is set, require read:accounts scope, symmetric to write:accounts
# for `mark_read`.
# Mastodon docs say this only requires a user token, no scopes needed
# As the op `|` requires at least one scope to be present, we use `&` here.
plug(
OAuthScopesPlug,
%{fallback: :proceed_unauthenticated, scopes: ["read:accounts"]}
when action in [:show, :index]
%{scopes: [], op: :&}
when action in [:index]
)
# Same as in MastodonAPI specs
plug(
OAuthScopesPlug,
%{fallback: :proceed_unauthenticated, scopes: ["write:accounts"]}
%{scopes: ["write:accounts"]}
when action in [:mark_read]
)

View file

@ -582,6 +582,7 @@ defmodule Pleroma.Web.Router do
get("/timelines/direct", TimelineController, :direct)
get("/timelines/list/:list_id", TimelineController, :list)
get("/announcements", AnnouncementController, :index)
post("/announcements/:id/dismiss", AnnouncementController, :mark_read)
end
@ -627,8 +628,6 @@ defmodule Pleroma.Web.Router do
get("/polls/:id", PollController, :show)
get("/directory", DirectoryController, :index)
get("/announcements", AnnouncementController, :index)
end
scope "/api/v2", Pleroma.Web.MastodonAPI do