forked from AkkomaGang/akkoma
Add OAuth tokens endpoint
This commit is contained in:
parent
33f8f3c33a
commit
61a4bc5095
6 changed files with 75 additions and 0 deletions
|
@ -52,4 +52,12 @@ def delete_user_tokens(%User{id: user_id}) do
|
||||||
)
|
)
|
||||||
|> Repo.delete_all()
|
|> Repo.delete_all()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_user_tokens(%User{id: user_id}) do
|
||||||
|
from(
|
||||||
|
t in Pleroma.Web.OAuth.Token,
|
||||||
|
where: t.user_id == ^user_id
|
||||||
|
)
|
||||||
|
|> Repo.all()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -389,6 +389,8 @@ defmodule Pleroma.Web.Router do
|
||||||
get("/qvitter/mutes", TwitterAPI.Controller, :raw_empty_array)
|
get("/qvitter/mutes", TwitterAPI.Controller, :raw_empty_array)
|
||||||
|
|
||||||
get("/externalprofile/show", TwitterAPI.Controller, :external_profile)
|
get("/externalprofile/show", TwitterAPI.Controller, :external_profile)
|
||||||
|
|
||||||
|
get("/oauth_tokens", TwitterAPI.Controller, :oauth_tokens)
|
||||||
end
|
end
|
||||||
|
|
||||||
pipeline :ap_relay do
|
pipeline :ap_relay do
|
||||||
|
|
|
@ -8,6 +8,10 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
||||||
import Pleroma.Web.ControllerHelper, only: [json_response: 3]
|
import Pleroma.Web.ControllerHelper, only: [json_response: 3]
|
||||||
|
|
||||||
alias Ecto.Changeset
|
alias Ecto.Changeset
|
||||||
|
alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView, NotificationView, TokenView}
|
||||||
|
alias Pleroma.Web.CommonAPI
|
||||||
|
alias Pleroma.{Repo, Activity, Object, User, Notification}
|
||||||
|
alias Pleroma.Web.OAuth.Token
|
||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
alias Pleroma.Web.ActivityPub.Utils
|
alias Pleroma.Web.ActivityPub.Utils
|
||||||
alias Pleroma.Web.CommonAPI
|
alias Pleroma.Web.CommonAPI
|
||||||
|
@ -542,6 +546,14 @@ def friends(%{assigns: %{user: for_user}} = conn, params) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def oauth_tokens(%{assigns: %{user: user}} = conn, _params) do
|
||||||
|
with oauth_tokens <- Token.get_user_tokens(user) do
|
||||||
|
conn
|
||||||
|
|> put_view(TokenView)
|
||||||
|
|> render("index.json", %{tokens: oauth_tokens})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def blocks(%{assigns: %{user: user}} = conn, _params) do
|
def blocks(%{assigns: %{user: user}} = conn, _params) do
|
||||||
with blocked_users <- User.blocked_users(user) do
|
with blocked_users <- User.blocked_users(user) do
|
||||||
conn
|
conn
|
||||||
|
|
22
lib/pleroma/web/twitter_api/views/token_view.ex
Normal file
22
lib/pleroma/web/twitter_api/views/token_view.ex
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.TwitterAPI.TokenView do
|
||||||
|
use Pleroma.Web, :view
|
||||||
|
|
||||||
|
def render("index.json", %{tokens: tokens}) do
|
||||||
|
tokens
|
||||||
|
|> render_many(Pleroma.Web.TwitterAPI.TokenView, "show.json")
|
||||||
|
|> Enum.filter(&Enum.any?/1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def render("show.json", %{token: token_entry}) do
|
||||||
|
%{
|
||||||
|
id: token_entry.id,
|
||||||
|
token: token_entry.token,
|
||||||
|
refresh_token: token_entry.refresh_token,
|
||||||
|
valid_until: token_entry.valid_until
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
|
@ -227,4 +227,17 @@ def instance_factory do
|
||||||
unreachable_since: nil
|
unreachable_since: nil
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def oauth_token_factory do
|
||||||
|
user = insert(:user)
|
||||||
|
oauth_app = insert(:oauth_app)
|
||||||
|
|
||||||
|
%Pleroma.Web.OAuth.Token{
|
||||||
|
token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
|
||||||
|
refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
|
||||||
|
user_id: user.id,
|
||||||
|
app_id: oauth_app.id,
|
||||||
|
valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1876,4 +1876,22 @@ test "with credentials", %{conn: conn, user: user} do
|
||||||
ActivityRepresenter.to_map(activity, %{user: user, for: user})
|
ActivityRepresenter.to_map(activity, %{user: user, for: user})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "GET /api/oauth_tokens" do
|
||||||
|
test "renders list" do
|
||||||
|
token = insert(:oauth_token)
|
||||||
|
|
||||||
|
response =
|
||||||
|
build_conn()
|
||||||
|
|> assign(:user, Repo.get(User, token.user_id))
|
||||||
|
|> get("/api/oauth_tokens")
|
||||||
|
|
||||||
|
keys =
|
||||||
|
json_response(response, 200)
|
||||||
|
|> hd()
|
||||||
|
|> Map.keys()
|
||||||
|
|
||||||
|
assert keys -- ["id", "refresh_token", "token", "valid_until"] == []
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue