parent
4e69d1239a
commit
52297920e7
@ -0,0 +1,32 @@
|
||||
defmodule Pleroma.Web.OAuth.Token.Response do
|
||||
@moduledoc false
|
||||
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.OAuth.Token.Utils
|
||||
|
||||
@expires_in Pleroma.Config.get([:oauth2, :token_expires_in], 600)
|
||||
|
||||
@doc false
|
||||
def build(%User{} = user, token, opts \\ %{}) do
|
||||
%{
|
||||
token_type: "Bearer",
|
||||
access_token: token.token,
|
||||
refresh_token: token.refresh_token,
|
||||
expires_in: @expires_in,
|
||||
scope: Enum.join(token.scopes, " "),
|
||||
me: user.ap_id
|
||||
}
|
||||
|> Map.merge(opts)
|
||||
end
|
||||
|
||||
def build_for_client_credentials(token) do
|
||||
%{
|
||||
token_type: "Bearer",
|
||||
access_token: token.token,
|
||||
refresh_token: token.refresh_token,
|
||||
created_at: Utils.format_created_at(token),
|
||||
expires_in: @expires_in,
|
||||
scope: Enum.join(token.scopes, " ")
|
||||
}
|
||||
end
|
||||
end
|
@ -0,0 +1,53 @@
|
||||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.OAuth.Token.UtilsTest do
|
||||
use Pleroma.DataCase
|
||||
alias Pleroma.Web.OAuth.Token.Utils
|
||||
import Pleroma.Factory
|
||||
|
||||
describe "fetch_app/1" do
|
||||
test "returns error when credentials is invalid" do
|
||||
assert {:error, :not_found} =
|
||||
Utils.fetch_app(%Plug.Conn{params: %{"client_id" => 1, "client_secret" => "x"}})
|
||||
end
|
||||
|
||||
test "returns App by params credentails" do
|
||||
app = insert(:oauth_app)
|
||||
|
||||
assert {:ok, load_app} =
|
||||
Utils.fetch_app(%Plug.Conn{
|
||||
params: %{"client_id" => app.client_id, "client_secret" => app.client_secret}
|
||||
})
|
||||
|
||||
assert load_app == app
|
||||
end
|
||||
|
||||
test "returns App by header credentails" do
|
||||
app = insert(:oauth_app)
|
||||
header = "Basic " <> Base.encode64("#{app.client_id}:#{app.client_secret}")
|
||||
|
||||
conn =
|
||||
%Plug.Conn{}
|
||||
|> Plug.Conn.put_req_header("authorization", header)
|
||||
|
||||
assert {:ok, load_app} = Utils.fetch_app(conn)
|
||||
assert load_app == app
|
||||
end
|
||||
end
|
||||
|
||||
describe "format_created_at/1" do
|
||||
test "returns formatted created at" do
|
||||
token = insert(:oauth_token)
|
||||
date = Utils.format_created_at(token)
|
||||
|
||||
token_date =
|
||||
token.inserted_at
|
||||
|> DateTime.from_naive!("Etc/UTC")
|
||||
|> DateTime.to_unix()
|
||||
|
||||
assert token_date == date
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue