Skip to content
Merged
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
10 changes: 8 additions & 2 deletions app/controllers/concerns/identifiable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ module Identifiable
end

def load_current_user
token = request.headers['Authorization']
return if token.blank?
header = request.headers['Authorization']&.strip
return if header.blank?

token = extract_token(header)

@current_user = User.from_token(token:)
return if @current_user.blank?
Expand All @@ -19,4 +21,8 @@ def load_current_user
RequestStore.store[:safeguarding_flag_users_by_token] ||= {}
RequestStore.store[:safeguarding_flag_users_by_token][token] = @current_user
end

def extract_token(header)
header.sub(/^Bearer\s+/i, '')
end
Comment thread
cursor[bot] marked this conversation as resolved.
end
33 changes: 33 additions & 0 deletions spec/controllers/concerns/identifiable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Identifiable do
subject(:extract_token) { identifiable.extract_token(header) }

let(:identifiable) { Class.new(ActionController::API) { include Identifiable }.new }

context 'when the header is a raw token' do
let(:header) { 'secret-token' }

it { is_expected.to eq('secret-token') }
end

context 'when the header is Bearer-prefixed' do
let(:header) { 'Bearer secret-token' }

it { is_expected.to eq('secret-token') }
end

context 'when the header uses a lowercase bearer prefix' do
let(:header) { 'bearer secret-token' }

it { is_expected.to eq('secret-token') }
end

context 'when the header is Bearer with no token' do
let(:header) { 'Bearer ' }

it { is_expected.to eq('') }
end
end
Loading