Fix oauth2 (for real) (#179)
[akkoma] / lib / pleroma / web / o_auth / token / query.ex
index fd6d9b112509da33c741efbc271a58d6368ba166..acddf0533b7d1d431cc4ea2d9627126de734ad21 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Web.OAuth.Token.Query do
@@ -23,9 +23,19 @@ defmodule Pleroma.Web.OAuth.Token.Query do
     from(q in query, where: q.token == ^token)
   end
 
+  @spec get_unexpired_by_app_and_user(query, String.t()) :: query
+  def get_unexpired_by_app_and_user(query \\ Token, app_id, %Pleroma.User{id: user_id}) do
+    time = NaiveDateTime.utc_now()
+
+    from(q in query,
+      where: q.app_id == ^app_id and q.valid_until > ^time and q.user_id == ^user_id,
+      limit: 1
+    )
+  end
+
   @spec get_by_app(query, String.t()) :: query
   def get_by_app(query \\ Token, app_id) do
-    from(q in query, where: q.app_id == ^app_id)
+    from(q in query, where: q.app_id == ^app_id, limit: 1)
   end
 
   @spec get_by_id(query, String.t()) :: query