Skip to content

Commit

Permalink
feature(api): add ensure_token_is_not_expired to api_controller v1 an…
Browse files Browse the repository at this point in the history
…d v2
  • Loading branch information
LeSim committed Jan 25, 2024
1 parent 28e4e1b commit 7e85559
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/controllers/api/v2/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class API::V2::BaseController < ApplicationController
skip_before_action :setup_tracking
before_action :authenticate_from_token
before_action :ensure_authorized_network, if: -> { @api_token.present? }
before_action :ensure_token_is_not_expired, if: -> { @api_token.present? }

before_action do
Current.browser = 'api'
Expand Down Expand Up @@ -54,4 +55,10 @@ def ensure_authorized_network
render json: { errors: ["request issued from a forbidden network. Add #{address.to_string}/#{address.prefix} to your allowed adresses in your /profil"] }, status: :forbidden
end
end

def ensure_token_is_not_expired
if @api_token.expired?
render json: { errors: ['token expired'] }, status: :unauthorized
end
end
end
7 changes: 7 additions & 0 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class APIController < ApplicationController
before_action :default_format_json
before_action :authenticate_from_token
before_action :ensure_authorized_network, if: -> { @api_token.present? }
before_action :ensure_token_is_not_expired, if: -> { @api_token.present? }

before_action do
Current.browser = 'api'
Expand Down Expand Up @@ -41,4 +42,10 @@ def ensure_authorized_network
render json: { errors: ["request issued from a forbidden network. Add #{address.to_string}/#{address.prefix} to your allowed adresses in your /profil"] }, status: :forbidden
end
end

def ensure_token_is_not_expired
if @api_token.expired?
render json: { errors: ['token expired'] }, status: :unauthorized
end
end
end
4 changes: 4 additions & 0 deletions app/models/api_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def forbidden_network?(ip)
authorized_networks.none? { |range| range.include?(ip) }
end

def expired?
expires_at&.past?
end

class << self
def generate(administrateur)
plain_token = generate_unique_secure_token
Expand Down
20 changes: 18 additions & 2 deletions spec/controllers/api/v2/base_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe API::V2::BaseController, type: :controller do
describe 'ensure_authorized_network' do
describe 'ensure_authorized_network and token_is_not_expired' do
let(:admin) { create(:administrateur) }
let(:token_bearer_couple) { APIToken.generate(admin) }
let(:token) { token_bearer_couple[0] }
Expand All @@ -18,7 +18,23 @@
describe 'GET #index' do
subject { get :fake_action }

context 'when no authorized networks are defined' do
context 'when no authorized networks are defined and the token is not expired' do
it { is_expected.to have_http_status(:ok) }
end

context 'when the token is expired' do
before do
token.update!(expires_at: 1.day.ago)
end

it { is_expected.to have_http_status(:unauthorized) }
end

context 'when this is precisely the day the token expires' do
before do
token.update!(expires_at: Time.zone.today)
end

it { is_expected.to have_http_status(:ok) }
end

Expand Down
12 changes: 10 additions & 2 deletions spec/controllers/api_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
end
end

describe 'ensure_authorized_network' do
describe 'ensure_authorized_network and token is not expired' do
let(:admin) { create(:administrateur) }
let(:token_bearer_couple) { APIToken.generate(admin) }
let(:token) { token_bearer_couple[0] }
Expand All @@ -59,10 +59,18 @@
describe 'GET #index' do
subject { get :fake_action }

context 'when no authorized networks are defined' do
context 'when no authorized networks are defined and the token is not expired' do
it { is_expected.to have_http_status(:ok) }
end

context 'when the token is expired' do
before do
token.update!(expires_at: 1.day.ago)
end

it { is_expected.to have_http_status(:unauthorized) }
end

context 'when a single authorized network is defined' do
before do
token.update!(authorized_networks: [IPAddr.new('192.168.1.0/24')])
Expand Down

0 comments on commit 7e85559

Please sign in to comment.