Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change API authentication to use hashed token with unique salts #131

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/controllers/api/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ class Api::ApplicationController < ApplicationController
private

def authenticate!
head 401 unless ActiveSupport::SecurityUtils.secure_compare params[:access_token] || '', ENV.fetch('API_ACCESS_TOKEN')
head 401 unless BCrypt::Password.valid_hash?(params[:access_token]) && valid_access_token?
end

def valid_access_token?
password = BCrypt::Password.new params[:access_token]

password.is_password?(ENV['API_ACCESS_TOKEN']) && $redis_workers.with { _1.set password.salt, nil, nx: true, ex: 24.hours.seconds }
end

def current_user
Expand Down
4 changes: 4 additions & 0 deletions config/initializers/redis.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
$redis_standings = ConnectionPool.new do
Redis::Namespace.new "#{ Rails.env }_standings", redis: Redis.new(driver: :hiredis)
end

$redis_workers = ConnectionPool.new do
Redis::Namespace.new "#{ Rails.env }_workers", redis: Redis.new(driver: :hiredis)
end
56 changes: 52 additions & 4 deletions spec/controllers/api/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
its(:default_url_options) { should eq({}) }

describe '#authenticate!' do
let(:uuid) { SecureRandom.uuid }

before { ENV['API_ACCESS_TOKEN'] = uuid }
let(:hash) { '$2a$12$P9FY1uI4pexijYtWXIW2pufjvBs/Om4uz9Gf4lDbuj8E8oUrFZ/OG' }

after { subject.send :authenticate! }

Expand All @@ -25,12 +23,62 @@
end

context do
before { expect(subject).to receive(:params).and_return(access_token: uuid) }
before { expect(subject).to receive(:params).and_return(access_token: hash) }

before { expect(subject).to receive(:valid_access_token?).and_return(false) }

it { expect(subject).to receive(:head).with(401) }
end

context do
before { expect(subject).to receive(:params).and_return(access_token: hash) }

before { expect(subject).to receive(:valid_access_token?).and_return(true) }

it { expect(subject).to_not receive(:head) }
end
end

describe '#valid_access_token?' do
let(:uuid) { SecureRandom.uuid }

let(:corrent_hash) { BCrypt::Password.create uuid }

let(:wrong_hash) { BCrypt::Password.create 'xxxx' }

before { ENV['API_ACCESS_TOKEN'] = uuid }

after { $redis_workers.keys('*').each { |k| $redis_workers.del k } }

context do
before { expect(subject).to receive(:params).and_return(access_token: wrong_hash.to_s) }

after { expect($redis_workers.keys '*').to eq([]) }

its(:valid_access_token?) { should be false }
end

context do
before { expect(subject).to receive(:params).and_return(access_token: corrent_hash.to_s) }

before { $redis_workers.set corrent_hash.salt, nil }

after { expect($redis_workers.keys '*').to eq([corrent_hash.salt]) }

its(:valid_access_token?) { should be false }
end

context do
before { expect(subject).to receive(:params).and_return(access_token: corrent_hash.to_s) }

after { expect($redis_workers.keys '*').to eq([corrent_hash.salt]) }

after { expect($redis_workers.ttl corrent_hash.salt).to eq(86400) }

its(:valid_access_token?) { should be true }
end
end

describe '#set_locale' do
after { I18n.locale = I18n.default_locale }

Expand Down