Add revoke token

This commit is contained in:
Maxim Filippov 2019-02-11 00:49:56 +03:00
parent 61a4bc5095
commit 62a45bdc11
4 changed files with 37 additions and 4 deletions

View File

@ -53,9 +53,18 @@ def delete_user_tokens(%User{id: user_id}) do
|> Repo.delete_all()
end
def get_user_tokens(%User{id: user_id}) do
def delete_user_token(%User{id: user_id}, token_id) do
from(
t in Pleroma.Web.OAuth.Token,
where: t.user_id == ^user_id,
where: t.id == ^token_id
)
|> Repo.delete_all()
end
def get_user_tokens(%User{id: user_id}) do
from(
t in Token,
where: t.user_id == ^user_id
)
|> Repo.all()

View File

@ -391,6 +391,7 @@ defmodule Pleroma.Web.Router do
get("/externalprofile/show", TwitterAPI.Controller, :external_profile)
get("/oauth_tokens", TwitterAPI.Controller, :oauth_tokens)
delete("/oauth_tokens/:id", TwitterAPI.Controller, :revoke_token)
end
pipeline :ap_relay do

View File

@ -554,6 +554,12 @@ def oauth_tokens(%{assigns: %{user: user}} = conn, _params) do
end
end
def revoke_token(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
Token.delete_user_token(user, id)
json_reply(conn, 201, "")
end
def blocks(%{assigns: %{user: user}} = conn, _params) do
with blocked_users <- User.blocked_users(user) do
conn

View File

@ -8,6 +8,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
alias Pleroma.{Repo, Activity, User, Object, Notification}
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.OAuth.Token
alias Pleroma.Web.TwitterAPI.UserView
alias Pleroma.Web.TwitterAPI.NotificationView
alias Pleroma.Web.CommonAPI
@ -1878,12 +1879,16 @@ test "with credentials", %{conn: conn, user: user} do
end
describe "GET /api/oauth_tokens" do
test "renders list" do
token = insert(:oauth_token)
setup do
token = insert(:oauth_token) |> Repo.preload(:user)
%{token: token}
end
test "renders list", %{token: token} do
response =
build_conn()
|> assign(:user, Repo.get(User, token.user_id))
|> assign(:user, token.user)
|> get("/api/oauth_tokens")
keys =
@ -1893,5 +1898,17 @@ test "renders list" do
assert keys -- ["id", "refresh_token", "token", "valid_until"] == []
end
test "revoke token", %{token: token} do
response =
build_conn()
|> assign(:user, token.user)
|> delete("/api/oauth_tokens/#{token.id}")
tokens = Token.get_user_tokens(token.user)
assert tokens == []
assert response.status == 201
end
end
end