Tweaks to OAuth entities expiration: changed default to 30 days, removed hardcoded...
authorIvan Tashkinov <ivantashkinov@gmail.com>
Wed, 9 Dec 2020 18:14:39 +0000 (21:14 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Wed, 9 Dec 2020 18:14:39 +0000 (21:14 +0300)
config/config.exs
config/description.exs
lib/pleroma/mfa/token.ex
lib/pleroma/web/o_auth/authorization.ex
lib/pleroma/web/o_auth/o_auth_view.ex
lib/pleroma/web/o_auth/token.ex
test/pleroma/web/o_auth/mfa_controller_test.exs
test/pleroma/web/o_auth/o_auth_controller_test.exs

index f7455cf97917fbb8072e0a9774de4ed60bebcee7..c7ac0d22c28293b8b55c6caa4af8c2b233efb8cd 100644 (file)
@@ -648,7 +648,7 @@ config :pleroma, :email_notifications,
   }
 
 config :pleroma, :oauth2,
-  token_expires_in: 600,
+  token_expires_in: 3600 * 24 * 30,
   issue_new_refresh_token: true,
   clean_expired_tokens: false
 
index a663d812760962306f38ca16a9325efc17b79616..f4b8768da5c0b6b4bdc076f683a56af9d0278e25 100644 (file)
@@ -2540,7 +2540,7 @@ config :pleroma, :config_description, [
         key: :token_expires_in,
         type: :integer,
         description: "The lifetime in seconds of the access token",
-        suggestions: [600]
+        suggestions: [2_592_000]
       },
       %{
         key: :issue_new_refresh_token,
index 82d3817ccfaca12e014ca0c848d5c1778371df2d..69b64c0e8ed73087a1912bbdf2312407382b2e91 100644 (file)
@@ -11,7 +11,7 @@ defmodule Pleroma.MFA.Token do
   alias Pleroma.User
   alias Pleroma.Web.OAuth.Authorization
 
-  @expires 3600 * 24 * 30
+  @expires 300
 
   @type t() :: %__MODULE__{}
 
index 268ee5b634d110bd01d929839d27682c54a50b68..e766dcada6c584cedf5842a6e0e121efebf6f933 100644 (file)
@@ -9,6 +9,7 @@ defmodule Pleroma.Web.OAuth.Authorization do
   alias Pleroma.User
   alias Pleroma.Web.OAuth.App
   alias Pleroma.Web.OAuth.Authorization
+  alias Pleroma.Web.OAuth.Token
 
   import Ecto.Changeset
   import Ecto.Query
@@ -53,7 +54,8 @@ defmodule Pleroma.Web.OAuth.Authorization do
   end
 
   defp add_lifetime(changeset) do
-    put_change(changeset, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
+    lifespan = Token.lifespan()
+    put_change(changeset, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), lifespan))
   end
 
   @spec use_changeset(Authtorizatiton.t(), map()) :: Changeset.t()
index f55247ebd75df014224e401924cf05cf886e7193..d22b2f7fe56f3e22a5b1cc74127cb280ab9a0e51 100644 (file)
@@ -13,7 +13,7 @@ defmodule Pleroma.Web.OAuth.OAuthView do
       token_type: "Bearer",
       access_token: token.token,
       refresh_token: token.refresh_token,
-      expires_in: expires_in(),
+      expires_in: NaiveDateTime.diff(token.valid_until, NaiveDateTime.utc_now()),
       scope: Enum.join(token.scopes, " "),
       created_at: Utils.format_created_at(token)
     }
@@ -25,6 +25,4 @@ defmodule Pleroma.Web.OAuth.OAuthView do
       response
     end
   end
-
-  defp expires_in, do: Pleroma.Config.get([:oauth2, :token_expires_in], 600)
 end
index 9170a7ec7daaf4fba673656f62d4e63d3da75a51..886117d155fe5b1ed2d2cd908fc2994db8d0cb15 100644 (file)
@@ -27,6 +27,10 @@ defmodule Pleroma.Web.OAuth.Token do
     timestamps()
   end
 
+  def lifespan do
+    Pleroma.Config.get!([:oauth2, :token_expires_in])
+  end
+
   @doc "Gets token by unique access token"
   @spec get_by_token(String.t()) :: {:ok, t()} | {:error, :not_found}
   def get_by_token(token) do
@@ -83,11 +87,11 @@ defmodule Pleroma.Web.OAuth.Token do
   end
 
   defp put_valid_until(changeset, attrs) do
-    expires_in =
-      Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), expires_in()))
+    valid_until =
+      Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), lifespan()))
 
     changeset
-    |> change(%{valid_until: expires_in})
+    |> change(%{valid_until: valid_until})
     |> validate_required([:valid_until])
   end
 
@@ -138,6 +142,4 @@ defmodule Pleroma.Web.OAuth.Token do
   end
 
   def is_expired?(_), do: false
-
-  defp expires_in, do: Pleroma.Config.get([:oauth2, :token_expires_in], 600)
 end
index 3c341facdc284c0f9b507ad2de38c732b467622a..6ecd0f6c9223524fe76d14c2620a18849d921e2b 100644 (file)
@@ -171,7 +171,6 @@ defmodule Pleroma.Web.OAuth.MFAControllerTest do
       assert match?(
                %{
                  "access_token" => _,
-                 "expires_in" => 600,
                  "me" => ^ap_id,
                  "refresh_token" => _,
                  "scope" => "write",
@@ -280,7 +279,6 @@ defmodule Pleroma.Web.OAuth.MFAControllerTest do
       assert match?(
                %{
                  "access_token" => _,
-                 "expires_in" => 600,
                  "me" => ^ap_id,
                  "refresh_token" => _,
                  "scope" => "write",
index 3221af223fa38acea21a97974d784c5b04a8af1c..ac22856eae99dffb93aa7d53a4d001f40334f08d 100644 (file)
@@ -1105,7 +1105,6 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
                %{
                  "scope" => "write",
                  "token_type" => "Bearer",
-                 "expires_in" => 600,
                  "access_token" => _,
                  "refresh_token" => _,
                  "me" => ^ap_id
@@ -1145,7 +1144,6 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
                %{
                  "scope" => "write",
                  "token_type" => "Bearer",
-                 "expires_in" => 600,
                  "access_token" => _,
                  "refresh_token" => _,
                  "me" => ^ap_id
@@ -1228,7 +1226,6 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
                %{
                  "scope" => "write",
                  "token_type" => "Bearer",
-                 "expires_in" => 600,
                  "access_token" => _,
                  "refresh_token" => _,
                  "me" => ^ap_id