Merge branch 'bugfix/oauth2-param-name' into 'develop'
[akkoma] / lib / pleroma / web / oauth / oauth_controller.ex
index bc6c365c9e2a3e93ab679dbae31d16accf10d5b7..a5fb32a4e774752c27537598f135470e71d59482 100644 (file)
@@ -71,7 +71,9 @@ defmodule Pleroma.Web.OAuth.OAuthController do
 
       json(conn, response)
     else
-      _error -> json(conn, %{error: "Invalid credentials"})
+      _error ->
+        put_status(conn, 400)
+        |> json(%{error: "Invalid credentials"})
     end
   end
 
@@ -79,10 +81,10 @@ defmodule Pleroma.Web.OAuth.OAuthController do
   # - investigate a way to verify the user wants to grant read/write/follow once scope handling is done
   def token_exchange(
         conn,
-        %{"grant_type" => "password", "name" => name, "password" => password} = params
+        %{"grant_type" => "password", "username" => name, "password" => password} = params
       ) do
     with %App{} = app <- get_app_from_request(conn, params),
-         %User{} = user <- User.get_cached_by_nickname(name),
+         %User{} = user <- User.get_by_nickname_or_email(name),
          true <- Pbkdf2.checkpw(password, user.password_hash),
          {:ok, auth} <- Authorization.create_authorization(app, user),
          {:ok, token} <- Token.exchange_token(app, auth) do
@@ -96,10 +98,24 @@ defmodule Pleroma.Web.OAuth.OAuthController do
 
       json(conn, response)
     else
-      _error -> json(conn, %{error: "Invalid credentials"})
+      _error ->
+        put_status(conn, 400)
+        |> json(%{error: "Invalid credentials"})
     end
   end
 
+  def token_exchange(
+        conn,
+        %{"grant_type" => "password", "name" => name, "password" => password} = params
+      ) do
+    params =
+      params
+      |> Map.delete("name")
+      |> Map.put("username", name)
+
+    token_exchange(conn, params)
+  end
+
   defp fix_padding(token) do
     token
     |> Base.url_decode64!(padding: false)