client_authenticator 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 538d337270dbb6eeb08602ec7ffc64e0d90a833b
4
- data.tar.gz: 1db4f00a00a933ae46009be19b4e97a16a9ee653
3
+ metadata.gz: 647a3876c77adf4fb5a788b3013688241a32359d
4
+ data.tar.gz: 47bb9d1587594e05fc012442726f99318fb0e5f1
5
5
  SHA512:
6
- metadata.gz: 6d84563b688750675673911e4613a5f95705b37eaf12cd65b804c7f1ddfb546a9990db55ec858bee3b4a4540ebe952b5901e6fc3329effea20e10fc5c76a809b
7
- data.tar.gz: 69027ca6009c7643f51c6fff22e9978e4f37c47a9807a10de38222739707f4bac314bc79425d2b8efc26cdf8dd8db6b2e36dd623ebe0cd3a2b72c1dc88750f1c
6
+ metadata.gz: 9dcc583fb26cefde33fb22e3a91cc9b3c64503e0c0dcc0d149399701000e9e6ff1b1f2a353859c0799bbc8166713918f29c9f560cce3e400acf75fc48cdfb509
7
+ data.tar.gz: 3110c39b5eae6bd26bc52860a887b33ca56b232a1f49f4d437ab79ea2a7c2ca9fc23a044f9a0902fc918d39562114d398a9ad08d2bfee70506671bdaa48b7b8f
@@ -5,8 +5,8 @@ module ClientAuthenticator
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  def authenticate_client!
8
- client_id = request.headers['client-id']
9
- pass_key = request.headers['pass-key']
8
+ client_id = request.headers[ClientAuthenticator.configuration.client_id_header]
9
+ pass_key = request.headers[ClientAuthenticator.configuration.pass_key_header]
10
10
  if client_id.nil? || pass_key.nil? || unauthorized?(client_id, pass_key)
11
11
  render json: {'error' => 'unauthorized'}, status: :unauthorized
12
12
  end
@@ -6,9 +6,12 @@ module ClientAuthenticator
6
6
  @client_id_field = 'client_id'
7
7
  @password_field = 'pass_key'
8
8
  @cache_expiry_duration = 12.hours
9
+ @client_id_header = 'client-id'
10
+ @pass_key_header = 'pass-key'
9
11
  end
10
12
 
11
- attr_accessor :table_name, :client_id_field, :password_field, :cache_expiry_duration
13
+ attr_accessor :table_name, :client_id_field, :password_field,
14
+ :cache_expiry_duration, :client_id_header, :pass_key_header
12
15
  end
13
16
 
14
17
  def self.configuration
@@ -18,4 +21,8 @@ module ClientAuthenticator
18
21
  def self.configure
19
22
  yield(configuration)
20
23
  end
24
+
25
+ def self.reset
26
+ @config = Configuration.new
27
+ end
21
28
  end
@@ -3,13 +3,13 @@ require 'client_authenticator'
3
3
  module ClientAuthenticator
4
4
  module TestHelpers
5
5
  def stub_valid_client_credentials
6
- expect(ClientAuthenticator::ApiClient).to receive(:authenticate_client!).with('client_id', 'valid_pass_key').and_return(true)
6
+ expect(ClientAuthenticator::ApiClient).to receive(:authenticated?).with('client_id', 'valid_pass_key').and_return(true)
7
7
  @request.headers['client-id'] = 'client_id'
8
8
  @request.headers['pass-key'] = 'valid_pass_key'
9
9
  end
10
10
 
11
11
  def stub_invalid_client_credentials
12
- expect(ClientAuthenticator::ApiClient).to receive(:authenticate_client!).with('client_id', 'invalid_pass_key').and_return(false)
12
+ expect(ClientAuthenticator::ApiClient).to receive(:authenticated?).with('client_id', 'invalid_pass_key').and_return(false)
13
13
  @request.headers['client-id'] = 'client_id'
14
14
  @request.headers['pass-key'] = 'invalid_pass_key'
15
15
  end
@@ -1,3 +1,3 @@
1
1
  module ClientAuthenticator
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -18,25 +18,25 @@ RSpec.describe ClientAuthenticator do
18
18
  end
19
19
  end
20
20
 
21
- context 'client authentication' do
22
- let!(:client_id) { 'clientid' }
23
- let!(:pass_key) { 'pass_key' }
24
- let(:header) { {'client-id': client_id, 'pass-key': pass_key}.with_indifferent_access }
25
- let(:request) { Request.new(header) }
26
- let(:auth) { auth = Authorizer.new
27
- auth.request = request
28
- auth
29
- }
30
- let(:cache) { double('cache') }
21
+ let!(:client_id) { 'clientid' }
22
+ let!(:pass_key) { 'pass_key' }
23
+ let(:header) { {'client-id': client_id, 'pass-key': pass_key}.with_indifferent_access }
24
+ let(:request) { Request.new(header) }
25
+ let(:auth) { auth = Authorizer.new
26
+ auth.request = request
27
+ auth
28
+ }
29
+ let(:cache) { double('cache') }
31
30
 
31
+ context 'client authentication with default headers' do
32
32
 
33
33
  context 'when client id and pass key is sent' do
34
- before(:each) do
35
- expect(Rails).to receive(:cache) { cache }
36
- expect(cache).to receive(:fetch).with("#{client_id}_#{pass_key}", { expires_in: 12.hours}) do |&block|
37
- block.call
34
+ before(:each) do
35
+ expect(Rails).to receive(:cache) { cache }
36
+ expect(cache).to receive(:fetch).with("#{client_id}_#{pass_key}", { expires_in: 12.hours}) do |&block|
37
+ block.call
38
+ end
38
39
  end
39
- end
40
40
 
41
41
  it 'when authorised, should not render 401' do
42
42
  expect(ClientAuthenticator::ApiClient).to receive(:authenticated?).with(client_id, pass_key).and_return(true)
@@ -63,7 +63,7 @@ RSpec.describe ClientAuthenticator do
63
63
  end
64
64
  end
65
65
 
66
- context 'when client id is not passed' do
66
+ context 'when pass key is not passed' do
67
67
  let(:pass_key) { nil }
68
68
  it 'should render 401' do
69
69
  expect(ClientAuthenticator::ApiClient).not_to receive(:authenticated?)
@@ -72,4 +72,179 @@ RSpec.describe ClientAuthenticator do
72
72
  end
73
73
 
74
74
  end
75
+
76
+ context 'client authentication with custom headers' do
77
+ context 'set custom header for client id' do
78
+ let(:header) { {'cid': client_id, 'pass-key': pass_key}.with_indifferent_access }
79
+ let(:request) { Request.new(header) }
80
+ let(:auth) { auth = Authorizer.new
81
+ auth.request = request
82
+ auth
83
+ }
84
+
85
+ before(:each) do
86
+ ClientAuthenticator.reset()
87
+ ClientAuthenticator.configure do |config|
88
+ config.client_id_header = 'cid'
89
+ end
90
+ end
91
+
92
+ context 'when client id and pass key is sent' do
93
+ before(:each) do
94
+ expect(Rails).to receive(:cache) { cache }
95
+ expect(cache).to receive(:fetch).with("#{client_id}_#{pass_key}", { expires_in: 12.hours}) do |&block|
96
+ block.call
97
+ end
98
+ end
99
+
100
+ it 'when authorised, should not render 401' do
101
+ expect(ClientAuthenticator::ApiClient).to receive(:authenticated?).with(client_id, pass_key).and_return(true)
102
+ expect(auth).not_to receive(:render)
103
+
104
+ auth.authenticate_client!
105
+ end
106
+
107
+ it 'when no authenticate fails' do
108
+ expected_opts = {:json=>{"error"=>"unauthorized"}, :status=>:unauthorized}
109
+ expect(ClientAuthenticator::ApiClient).to receive(:authenticated?).with(client_id, pass_key).and_return(false)
110
+ expect(auth).to receive(:render).with(expected_opts)
111
+
112
+ auth.authenticate_client!
113
+ end
114
+ end
115
+
116
+ context 'when client id is not passed' do
117
+ let(:client_id) { nil }
118
+ it 'should render 401' do
119
+ expect(ClientAuthenticator::ApiClient).not_to receive(:authenticated?)
120
+ auth.authenticate_client!
121
+ end
122
+ end
123
+
124
+ context 'when pass key is not passed' do
125
+ let(:pass_key) { nil }
126
+ it 'should render 401' do
127
+ expect(ClientAuthenticator::ApiClient).not_to receive(:authenticated?)
128
+ auth.authenticate_client!
129
+ end
130
+ end
131
+
132
+ end
133
+
134
+ context 'set custom header for pass key' do
135
+ let(:header) { {'client-id': client_id, 'pk': pass_key}.with_indifferent_access }
136
+ let(:request) { Request.new(header) }
137
+ let(:auth) { auth = Authorizer.new
138
+ auth.request = request
139
+ auth
140
+ }
141
+
142
+ before(:each) do
143
+ ClientAuthenticator.reset()
144
+ ClientAuthenticator.configure do |config|
145
+ config.pass_key_header = 'pk'
146
+ end
147
+ end
148
+
149
+ context 'when client id and pass key is sent' do
150
+ before(:each) do
151
+ expect(Rails).to receive(:cache) { cache }
152
+ expect(cache).to receive(:fetch).with("#{client_id}_#{pass_key}", { expires_in: 12.hours}) do |&block|
153
+ block.call
154
+ end
155
+ end
156
+
157
+ it 'when authorised, should not render 401' do
158
+ expect(ClientAuthenticator::ApiClient).to receive(:authenticated?).with(client_id, pass_key).and_return(true)
159
+ expect(auth).not_to receive(:render)
160
+
161
+ auth.authenticate_client!
162
+ end
163
+
164
+ it 'when no authenticate fails' do
165
+ expected_opts = {:json=>{"error"=>"unauthorized"}, :status=>:unauthorized}
166
+ expect(ClientAuthenticator::ApiClient).to receive(:authenticated?).with(client_id, pass_key).and_return(false)
167
+ expect(auth).to receive(:render).with(expected_opts)
168
+
169
+ auth.authenticate_client!
170
+ end
171
+ end
172
+
173
+ context 'when client id is not passed' do
174
+ let(:client_id) { nil }
175
+ it 'should render 401' do
176
+ expect(ClientAuthenticator::ApiClient).not_to receive(:authenticated?)
177
+ auth.authenticate_client!
178
+ end
179
+ end
180
+
181
+ context 'when pass key is not passed' do
182
+ let(:pass_key) { nil }
183
+ it 'should render 401' do
184
+ expect(ClientAuthenticator::ApiClient).not_to receive(:authenticated?)
185
+ auth.authenticate_client!
186
+ end
187
+ end
188
+
189
+ end
190
+
191
+ context 'set custom header for client id and pass key' do
192
+ let(:header) { {'cid': client_id, 'pk': pass_key}.with_indifferent_access }
193
+ let(:request) { Request.new(header) }
194
+ let(:auth) { auth = Authorizer.new
195
+ auth.request = request
196
+ auth
197
+ }
198
+
199
+ before(:each) do
200
+ ClientAuthenticator.reset()
201
+ ClientAuthenticator.configure do |config|
202
+ config.client_id_header = 'cid'
203
+ config.pass_key_header = 'pk'
204
+ end
205
+ end
206
+
207
+ context 'when client id and pass key is sent' do
208
+ before(:each) do
209
+ expect(Rails).to receive(:cache) { cache }
210
+ expect(cache).to receive(:fetch).with("#{client_id}_#{pass_key}", { expires_in: 12.hours}) do |&block|
211
+ block.call
212
+ end
213
+ end
214
+
215
+ it 'when authorised, should not render 401' do
216
+ expect(ClientAuthenticator::ApiClient).to receive(:authenticated?).with(client_id, pass_key).and_return(true)
217
+ expect(auth).not_to receive(:render)
218
+
219
+ auth.authenticate_client!
220
+ end
221
+
222
+ it 'when no authenticate fails' do
223
+ expected_opts = {:json=>{"error"=>"unauthorized"}, :status=>:unauthorized}
224
+ expect(ClientAuthenticator::ApiClient).to receive(:authenticated?).with(client_id, pass_key).and_return(false)
225
+ expect(auth).to receive(:render).with(expected_opts)
226
+
227
+ auth.authenticate_client!
228
+ end
229
+ end
230
+
231
+ context 'when client id is not passed' do
232
+ let(:client_id) { nil }
233
+ it 'should render 401' do
234
+ expect(ClientAuthenticator::ApiClient).not_to receive(:authenticated?)
235
+ auth.authenticate_client!
236
+ end
237
+ end
238
+
239
+ context 'when pass key is not passed' do
240
+ let(:pass_key) { nil }
241
+ it 'should render 401' do
242
+ expect(ClientAuthenticator::ApiClient).not_to receive(:authenticated?)
243
+ auth.authenticate_client!
244
+ end
245
+ end
246
+
247
+ end
248
+
249
+ end
75
250
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: client_authenticator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - manoharakshetty
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-04-14 00:00:00.000000000 Z
12
+ date: 2017-05-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler