Accept API authentication with keys token
This commit is contained in:
parent
0f1f7106b2
commit
ba3c0380d0
@ -37,6 +37,9 @@ module API
|
|||||||
end
|
end
|
||||||
|
|
||||||
def authenticate!
|
def authenticate!
|
||||||
|
if key = authenticate_with_http_token { |t| Key.authenticate(t) }
|
||||||
|
self.current_user = key.user
|
||||||
|
end
|
||||||
head :unauthorized if current_user.nil?
|
head :unauthorized if current_user.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -8,19 +8,30 @@ describe 'API application' do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe 'authenticated ping endpoint' do
|
describe 'authenticated ping endpoint' do
|
||||||
before { get api_ping_auth_path, format: :json }
|
let(:headers) { {} }
|
||||||
|
subject { response }
|
||||||
|
|
||||||
|
before { get api_ping_auth_path, { format: :json }, headers }
|
||||||
|
|
||||||
it 'requests authentication' do
|
it 'requests authentication' do
|
||||||
expect(response).to have_http_status 401
|
expect(response).to have_http_status 401
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when session is authenticated' do
|
context 'when session is authenticated' do
|
||||||
subject { response }
|
|
||||||
|
|
||||||
before { api_sign_in }
|
before { api_sign_in }
|
||||||
|
|
||||||
it { is_expected.to have_http_status 200 }
|
it { is_expected.to have_http_status 200 }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when requests has a valid authentication token' do
|
||||||
|
let(:key) { create :key }
|
||||||
|
let(:headers) do {
|
||||||
|
'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Token
|
||||||
|
.encode_credentials(key.token)
|
||||||
|
} end
|
||||||
|
|
||||||
|
it { is_expected.to have_http_status 200 }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'formats handling' do
|
describe 'formats handling' do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user