Merge branch 'refactor/oauth_controller_response' into 'develop'
Refactoring oauth response. See merge request pleroma/pleroma!1149
This commit is contained in:
commit
37dc1cc431
@ -19,8 +19,6 @@ defmodule Pleroma.Web.OAuth.OAuthController do
|
|||||||
|
|
||||||
if Pleroma.Config.oauth_consumer_enabled?(), do: plug(Ueberauth)
|
if Pleroma.Config.oauth_consumer_enabled?(), do: plug(Ueberauth)
|
||||||
|
|
||||||
@expires_in Pleroma.Config.get([:oauth2, :token_expires_in], 600)
|
|
||||||
|
|
||||||
plug(:fetch_session)
|
plug(:fetch_session)
|
||||||
plug(:fetch_flash)
|
plug(:fetch_flash)
|
||||||
|
|
||||||
@ -144,14 +142,14 @@ defmodule Pleroma.Web.OAuth.OAuthController do
|
|||||||
@doc "Renew access_token with refresh_token"
|
@doc "Renew access_token with refresh_token"
|
||||||
def token_exchange(
|
def token_exchange(
|
||||||
conn,
|
conn,
|
||||||
%{"grant_type" => "refresh_token", "refresh_token" => token} = params
|
%{"grant_type" => "refresh_token", "refresh_token" => token} = _params
|
||||||
) do
|
) do
|
||||||
with %App{} = app <- get_app_from_request(conn, params),
|
with {:ok, app} <- Token.Utils.fetch_app(conn),
|
||||||
{:ok, %{user: user} = token} <- Token.get_by_refresh_token(app, token),
|
{:ok, %{user: user} = token} <- Token.get_by_refresh_token(app, token),
|
||||||
{:ok, token} <- RefreshToken.grant(token) do
|
{:ok, token} <- RefreshToken.grant(token) do
|
||||||
response_attrs = %{created_at: Token.Utils.format_created_at(token)}
|
response_attrs = %{created_at: Token.Utils.format_created_at(token)}
|
||||||
|
|
||||||
json(conn, response_token(user, token, response_attrs))
|
json(conn, Token.Response.build(user, token, response_attrs))
|
||||||
else
|
else
|
||||||
_error ->
|
_error ->
|
||||||
put_status(conn, 400)
|
put_status(conn, 400)
|
||||||
@ -160,14 +158,14 @@ defmodule Pleroma.Web.OAuth.OAuthController do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def token_exchange(conn, %{"grant_type" => "authorization_code"} = params) do
|
def token_exchange(conn, %{"grant_type" => "authorization_code"} = params) do
|
||||||
with %App{} = app <- get_app_from_request(conn, params),
|
with {:ok, app} <- Token.Utils.fetch_app(conn),
|
||||||
fixed_token = Token.Utils.fix_padding(params["code"]),
|
fixed_token = Token.Utils.fix_padding(params["code"]),
|
||||||
{:ok, auth} <- Authorization.get_by_token(app, fixed_token),
|
{:ok, auth} <- Authorization.get_by_token(app, fixed_token),
|
||||||
%User{} = user <- User.get_cached_by_id(auth.user_id),
|
%User{} = user <- User.get_cached_by_id(auth.user_id),
|
||||||
{:ok, token} <- Token.exchange_token(app, auth) do
|
{:ok, token} <- Token.exchange_token(app, auth) do
|
||||||
response_attrs = %{created_at: Token.Utils.format_created_at(token)}
|
response_attrs = %{created_at: Token.Utils.format_created_at(token)}
|
||||||
|
|
||||||
json(conn, response_token(user, token, response_attrs))
|
json(conn, Token.Response.build(user, token, response_attrs))
|
||||||
else
|
else
|
||||||
_error ->
|
_error ->
|
||||||
put_status(conn, 400)
|
put_status(conn, 400)
|
||||||
@ -179,14 +177,14 @@ defmodule Pleroma.Web.OAuth.OAuthController do
|
|||||||
conn,
|
conn,
|
||||||
%{"grant_type" => "password"} = params
|
%{"grant_type" => "password"} = params
|
||||||
) do
|
) do
|
||||||
with {_, {:ok, %User{} = user}} <- {:get_user, Authenticator.get_user(conn)},
|
with {:ok, %User{} = user} <- Authenticator.get_user(conn),
|
||||||
%App{} = app <- get_app_from_request(conn, params),
|
{:ok, app} <- Token.Utils.fetch_app(conn),
|
||||||
{:auth_active, true} <- {:auth_active, User.auth_active?(user)},
|
{:auth_active, true} <- {:auth_active, User.auth_active?(user)},
|
||||||
{:user_active, true} <- {:user_active, !user.info.deactivated},
|
{:user_active, true} <- {:user_active, !user.info.deactivated},
|
||||||
{:ok, scopes} <- validate_scopes(app, params),
|
{:ok, scopes} <- validate_scopes(app, params),
|
||||||
{:ok, auth} <- Authorization.create_authorization(app, user, scopes),
|
{:ok, auth} <- Authorization.create_authorization(app, user, scopes),
|
||||||
{:ok, token} <- Token.exchange_token(app, auth) do
|
{:ok, token} <- Token.exchange_token(app, auth) do
|
||||||
json(conn, response_token(user, token))
|
json(conn, Token.Response.build(user, token))
|
||||||
else
|
else
|
||||||
{:auth_active, false} ->
|
{:auth_active, false} ->
|
||||||
# Per https://github.com/tootsuite/mastodon/blob/
|
# Per https://github.com/tootsuite/mastodon/blob/
|
||||||
@ -218,21 +216,11 @@ defmodule Pleroma.Web.OAuth.OAuthController do
|
|||||||
token_exchange(conn, params)
|
token_exchange(conn, params)
|
||||||
end
|
end
|
||||||
|
|
||||||
def token_exchange(conn, %{"grant_type" => "client_credentials"} = params) do
|
def token_exchange(conn, %{"grant_type" => "client_credentials"} = _params) do
|
||||||
with %App{} = app <- get_app_from_request(conn, params),
|
with {:ok, app} <- Token.Utils.fetch_app(conn),
|
||||||
{:ok, auth} <- Authorization.create_authorization(app, %User{}),
|
{:ok, auth} <- Authorization.create_authorization(app, %User{}),
|
||||||
{:ok, token} <- Token.exchange_token(app, auth),
|
{:ok, token} <- Token.exchange_token(app, auth) do
|
||||||
{:ok, inserted_at} <- DateTime.from_naive(token.inserted_at, "Etc/UTC") do
|
json(conn, Token.Response.build_for_client_credentials(token))
|
||||||
response = %{
|
|
||||||
token_type: "Bearer",
|
|
||||||
access_token: token.token,
|
|
||||||
refresh_token: token.refresh_token,
|
|
||||||
created_at: DateTime.to_unix(inserted_at),
|
|
||||||
expires_in: 60 * 10,
|
|
||||||
scope: Enum.join(token.scopes, " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
json(conn, response)
|
|
||||||
else
|
else
|
||||||
_error ->
|
_error ->
|
||||||
put_status(conn, 400)
|
put_status(conn, 400)
|
||||||
@ -244,7 +232,7 @@ defmodule Pleroma.Web.OAuth.OAuthController do
|
|||||||
def token_exchange(conn, params), do: bad_request(conn, params)
|
def token_exchange(conn, params), do: bad_request(conn, params)
|
||||||
|
|
||||||
def token_revoke(conn, %{"token" => _token} = params) do
|
def token_revoke(conn, %{"token" => _token} = params) do
|
||||||
with %App{} = app <- get_app_from_request(conn, params),
|
with {:ok, app} <- Token.Utils.fetch_app(conn),
|
||||||
{:ok, _token} <- RevokeToken.revoke(app, params) do
|
{:ok, _token} <- RevokeToken.revoke(app, params) do
|
||||||
json(conn, %{})
|
json(conn, %{})
|
||||||
else
|
else
|
||||||
@ -427,33 +415,6 @@ defmodule Pleroma.Web.OAuth.OAuthController do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp get_app_from_request(conn, params) do
|
|
||||||
conn
|
|
||||||
|> fetch_client_credentials(params)
|
|
||||||
|> fetch_client
|
|
||||||
end
|
|
||||||
|
|
||||||
defp fetch_client({id, secret}) when is_binary(id) and is_binary(secret) do
|
|
||||||
Repo.get_by(App, client_id: id, client_secret: secret)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp fetch_client({_id, _secret}), do: nil
|
|
||||||
|
|
||||||
defp fetch_client_credentials(conn, params) do
|
|
||||||
# Per RFC 6749, HTTP Basic is preferred to body params
|
|
||||||
with ["Basic " <> encoded] <- get_req_header(conn, "authorization"),
|
|
||||||
{:ok, decoded} <- Base.decode64(encoded),
|
|
||||||
[id, secret] <-
|
|
||||||
Enum.map(
|
|
||||||
String.split(decoded, ":"),
|
|
||||||
fn s -> URI.decode_www_form(s) end
|
|
||||||
) do
|
|
||||||
{id, secret}
|
|
||||||
else
|
|
||||||
_ -> {params["client_id"], params["client_secret"]}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Special case: Local MastodonFE
|
# Special case: Local MastodonFE
|
||||||
defp redirect_uri(conn, "."), do: mastodon_api_url(conn, :login)
|
defp redirect_uri(conn, "."), do: mastodon_api_url(conn, :login)
|
||||||
|
|
||||||
@ -464,18 +425,6 @@ defmodule Pleroma.Web.OAuth.OAuthController do
|
|||||||
defp put_session_registration_id(conn, registration_id),
|
defp put_session_registration_id(conn, registration_id),
|
||||||
do: put_session(conn, :registration_id, registration_id)
|
do: put_session(conn, :registration_id, registration_id)
|
||||||
|
|
||||||
defp response_token(%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
|
|
||||||
|
|
||||||
@spec validate_scopes(App.t(), map()) ::
|
@spec validate_scopes(App.t(), map()) ::
|
||||||
{:ok, list()} | {:error, :missing_scopes | :unsupported_scopes}
|
{:ok, list()} | {:error, :missing_scopes | :unsupported_scopes}
|
||||||
defp validate_scopes(app, params) do
|
defp validate_scopes(app, params) do
|
||||||
|
32
lib/pleroma/web/oauth/token/response.ex
Normal file
32
lib/pleroma/web/oauth/token/response.ex
Normal file
@ -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
|
@ -3,6 +3,44 @@ defmodule Pleroma.Web.OAuth.Token.Utils do
|
|||||||
Auxiliary functions for dealing with tokens.
|
Auxiliary functions for dealing with tokens.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
alias Pleroma.Repo
|
||||||
|
alias Pleroma.Web.OAuth.App
|
||||||
|
|
||||||
|
@doc "Fetch app by client credentials from request"
|
||||||
|
@spec fetch_app(Plug.Conn.t()) :: {:ok, App.t()} | {:error, :not_found}
|
||||||
|
def fetch_app(conn) do
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> fetch_client_credentials()
|
||||||
|
|> fetch_client
|
||||||
|
|
||||||
|
case res do
|
||||||
|
%App{} = app -> {:ok, app}
|
||||||
|
_ -> {:error, :not_found}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp fetch_client({id, secret}) when is_binary(id) and is_binary(secret) do
|
||||||
|
Repo.get_by(App, client_id: id, client_secret: secret)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp fetch_client({_id, _secret}), do: nil
|
||||||
|
|
||||||
|
defp fetch_client_credentials(conn) do
|
||||||
|
# Per RFC 6749, HTTP Basic is preferred to body params
|
||||||
|
with ["Basic " <> encoded] <- Plug.Conn.get_req_header(conn, "authorization"),
|
||||||
|
{:ok, decoded} <- Base.decode64(encoded),
|
||||||
|
[id, secret] <-
|
||||||
|
Enum.map(
|
||||||
|
String.split(decoded, ":"),
|
||||||
|
fn s -> URI.decode_www_form(s) end
|
||||||
|
) do
|
||||||
|
{id, secret}
|
||||||
|
else
|
||||||
|
_ -> {conn.params["client_id"], conn.params["client_secret"]}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@doc "convert token inserted_at to unix timestamp"
|
@doc "convert token inserted_at to unix timestamp"
|
||||||
def format_created_at(%{inserted_at: inserted_at} = _token) do
|
def format_created_at(%{inserted_at: inserted_at} = _token) do
|
||||||
inserted_at
|
inserted_at
|
||||||
|
53
test/web/oauth/token/utils_test.exs
Normal file
53
test/web/oauth/token/utils_test.exs
Normal file
@ -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
Block a user