Skip to content

Commit

Permalink
Merge pull request #9880 from demarches-simplifiees/api_token_store_l…
Browse files Browse the repository at this point in the history
…ast_used_ips

API: stocke les ips utilisées pour accéder à l'API
  • Loading branch information
colinux authored Dec 21, 2023
2 parents 1ecbddc + afb6eac commit 6f7d786
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/api/v2/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def authenticate_from_token

if @api_token.present?
@api_token.touch(:last_v2_authenticated_at)
@api_token.store_new_ip(request.remote_ip)
@current_user = @api_token.administrateur.user
Current.user = @current_user
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def authenticate_from_token

if @api_token.present?
@api_token.touch(:last_v1_authenticated_at)
@api_token.store_new_ip(request.remote_ip)
@current_user = @api_token.administrateur.user
end
end
Expand Down
7 changes: 7 additions & 0 deletions app/models/api_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ def prefix
Base64.urlsafe_encode64(id).slice(0, 5)
end

def store_new_ip(ip)
set = Set.new(stored_ips)
if set.add?(IPAddr.new(ip))
update!(stored_ips: set.to_a)
end
end

class << self
def generate(administrateur)
plain_token = generate_unique_secure_token
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLastUsedIpsColumnToAPIToken < ActiveRecord::Migration[7.0]
def change
add_column :api_tokens, :stored_ips, :inet, array: true, default: []
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_11_28_071043) do
ActiveRecord::Schema[7.0].define(version: 2023_12_21_142727) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -98,6 +98,7 @@
t.datetime "last_v1_authenticated_at"
t.datetime "last_v2_authenticated_at"
t.string "name", null: false
t.inet "stored_ips", default: [], array: true
t.datetime "updated_at", null: false
t.integer "version", default: 3, null: false
t.boolean "write_access", default: true, null: false
Expand Down
20 changes: 20 additions & 0 deletions spec/models/api_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,24 @@
it { is_expected.to be_nil }
end
end

describe '#store_new_ip' do
let(:api_token) { APIToken.generate(administrateur).first }
let(:ip) { '192.168.0.1' }

subject do
api_token.store_new_ip(ip)
api_token.stored_ips
end

context 'when none ip is stored' do
it { is_expected.to eq([IPAddr.new(ip)]) }
end

context 'when the ip is already stored' do
before { api_token.update!(stored_ips: [ip]) }

it { is_expected.to eq([IPAddr.new(ip)]) }
end
end
end

0 comments on commit 6f7d786

Please sign in to comment.