Fix revoked at on old token

This commit is contained in:
Simon Prévost 2023-11-29 15:40:57 -05:00
parent fa539362e4
commit bcab4527f2
3 changed files with 10 additions and 11 deletions

View File

@ -1,5 +1,7 @@
defmodule Accent.UserRemote.TokenGiver do
@moduledoc false
import Ecto.Query
alias Accent.Repo
alias Accent.Utils.SecureRandom
@ -17,9 +19,9 @@ defmodule Accent.UserRemote.TokenGiver do
end
defp invalidate_tokens(user) do
user
|> Ecto.assoc(:private_access_tokens)
|> Repo.update_all(set: [revoked_at: DateTime.utc_now(), updated_at: DateTime.utc_now()])
query = from(access_tokens in Ecto.assoc(user, :private_access_tokens), where: is_nil(access_tokens.revoked_at))
Repo.update_all(query, set: [revoked_at: DateTime.utc_now(), updated_at: DateTime.utc_now()])
end
defp create_token(user) do

View File

@ -1,12 +1,4 @@
defmodule Accent.Repo do
use Ecto.Repo, otp_app: :accent, adapter: Ecto.Adapters.Postgres
use Scrivener, page_size: 30, max_page_size: 10_000
@doc """
Dynamically loads the repository url from the
DATABASE_URL environment variable.
"""
def init(_, opts) do
{:ok, Keyword.put(opts, :url, Application.get_env(:accent, Accent.Repo)[:url])}
end
end

View File

@ -14,11 +14,16 @@ defmodule AccentTest.UserRemote.TokenGiver do
user = Repo.insert!(@user)
token = Repo.insert!(Map.merge(@token, %{user_id: user.id}))
existing_revoked_token =
Repo.insert!(%AccessToken{token: "revoked", revoked_at: NaiveDateTime.utc_now(:second), user_id: user.id})
TokenGiver.grant_token(user)
revoked_token = Repo.get_by!(AccessToken, token: token.token)
reload_existing_revoked_token = Repo.reload!(existing_revoked_token)
assert revoked_token.revoked_at !== nil
assert reload_existing_revoked_token.revoked_at === existing_revoked_token.revoked_at
end
test "create token" do