From 7929b7d0d74c9bfe0b396b34e886432594d995b3 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 7 Dec 2022 18:56:06 +0100 Subject: [PATCH] clean(api_token): remove administrateur token support --- app/models/administrateur.rb | 2 +- app/models/api_token.rb | 15 ++------------- spec/models/api_token_spec.rb | 27 --------------------------- 3 files changed, 3 insertions(+), 41 deletions(-) diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index 63a4d369c..8d7832e9f 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -9,7 +9,7 @@ # user_id :bigint not null # class Administrateur < ApplicationRecord - self.ignored_columns = [:active] + self.ignored_columns = [:active, :encrypted_token] UNUSED_ADMIN_THRESHOLD = 6.months diff --git a/app/models/api_token.rb b/app/models/api_token.rb index 8ee587620..bfce400e6 100644 --- a/app/models/api_token.rb +++ b/app/models/api_token.rb @@ -38,25 +38,14 @@ class APIToken < ApplicationRecord # the migration to the APIToken model set `version: 1` for all the v1 and v2 token # this is the only place where we can fix the version where(administrateur_id:, version: 1).update_all(version: 2) # update to v2 - find_by(administrateur_id:, version: 2)&.then(&ensure_valid_token(plain_token)) || - find_with_administrateur_encrypted_token(plain_token, administrateurs) # before migration + find_by(administrateur_id:, version: 2)&.then(&ensure_valid_token(plain_token)) in { plain_token: } # token v1 - where(administrateur: administrateurs, version: 1).find(&ensure_valid_token(plain_token)) || - find_with_administrateur_encrypted_token(plain_token, administrateurs) # before migration + where(administrateur: administrateurs, version: 1).find(&ensure_valid_token(plain_token)) end end private - # FIXME remove after migration - def find_with_administrateur_encrypted_token(plain_token, administrateurs) - administrateurs - .lazy - .filter { _1.encrypted_token.present? } - .map { APIToken.new(administrateur: _1, encrypted_token: _1.encrypted_token, version: 1) } - .find(&ensure_valid_token(plain_token)) - end - UUID_SIZE = SecureRandom.uuid.size def unpack(maybe_packed_token) case message_verifier.verified(maybe_packed_token) diff --git a/spec/models/api_token_spec.rb b/spec/models/api_token_spec.rb index 8f96edc3f..3a895ae8a 100644 --- a/spec/models/api_token_spec.rb +++ b/spec/models/api_token_spec.rb @@ -121,23 +121,6 @@ describe APIToken, type: :model do it { expect(result).to be_truthy } end end - - context 'with plain token (before migration)' do - before do - administrateur.update(encrypted_token: api_token.encrypted_token) - other_administrateur.update(encrypted_token: other_api_token.encrypted_token) - api_token.destroy - other_api_token.destroy - end - - let(:token) { plain_token } - it { expect(result).to be_truthy } - - context 'with other plain token' do - let(:token) { other_plain_token } - it { expect(result).to be_truthy } - end - end end context 'with packed token' do @@ -158,16 +141,6 @@ describe APIToken, type: :model do it { expect(result).to be_truthy } end - context 'with plain token (before migration)' do - before do - administrateur.update(encrypted_token: api_token.encrypted_token) - api_token.destroy - end - - let(:token) { plain_token } - it { expect(result).to be_truthy } - end - context "with valid garbage base64" do before { api_token.update(version: 1, encrypted_token: BCrypt::Password.create(token)) }