client_authenticator 1.0.0 → 1.0.1
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 +4 -4
- data/.travis.yml +0 -3
- data/Gemfile +2 -0
- data/README.md +5 -2
- data/client_authenticator.gemspec +0 -2
- data/lib/client_authenticator/api_authenticable.rb +9 -1
- data/lib/client_authenticator/configuration.rb +2 -1
- data/lib/client_authenticator/version.rb +1 -1
- data/spec/client_authenticator_spec.rb +11 -2
- data/spec/spec_helper.rb +4 -0
- metadata +2 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fc9b51913b6cd5ffd49892766c365909bf8dbeb
|
4
|
+
data.tar.gz: ed240716c5b7e7998f79ec736058a91ba8dbd5fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec186fb050688ccc32d3fd350cbebc3025cad2e8a87c1c79601569ef7945863eb8ede6ccc4aa1645ea79e6714f2fb37386084b566556394089a0c48e2a9ffbac
|
7
|
+
data.tar.gz: a43323c521ef2e409c9bfb188a145e388eae599580b5feff07975be242027f5d5b6912e0e7c647da3862d8270f46a40865f28ca5a4444af5f5d9bbbb86097eec
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
-
#
|
1
|
+
# Client Authenticator
|
2
|
+
[](https://travis-ci.org/gojek-engineering/client-authenticator-rb)
|
3
|
+
|
4
|
+
A gem to authenticate your service clients via headers `client-id, pass-key` before processing api requests.
|
2
5
|
|
3
6
|
## Installation
|
4
7
|
|
@@ -12,7 +15,7 @@ And then execute:
|
|
12
15
|
|
13
16
|
Or install it yourself as:
|
14
17
|
|
15
|
-
$ gem install
|
18
|
+
$ gem install client_authenticator
|
16
19
|
|
17
20
|
## Usage
|
18
21
|
|
@@ -20,8 +20,6 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.add_development_dependency "rake", "~> 10.0"
|
21
21
|
spec.add_development_dependency "rspec"
|
22
22
|
spec.add_development_dependency "rspec-rails"
|
23
|
-
spec.add_development_dependency "factory_girl_rails"
|
24
|
-
spec.add_development_dependency "shoulda-matchers"
|
25
23
|
spec.add_development_dependency "activerecord"
|
26
24
|
spec.add_development_dependency "generator_spec"
|
27
25
|
end
|
@@ -7,9 +7,17 @@ module ClientAuthenticator
|
|
7
7
|
def authenticate_client!
|
8
8
|
client_id = request.headers['client-id']
|
9
9
|
pass_key = request.headers['pass-key']
|
10
|
-
if client_id.nil? || pass_key.nil? ||
|
10
|
+
if client_id.nil? || pass_key.nil? || unauthorized?(client_id, pass_key)
|
11
11
|
render json: {'error' => 'unauthorized'}, status: :unauthorized
|
12
12
|
end
|
13
13
|
end
|
14
|
+
|
15
|
+
def unauthorized?(client_id, pass_key)
|
16
|
+
ttl = ClientAuthenticator.configuration.cache_expiry_duration
|
17
|
+
Rails.cache.fetch("#{client_id}_#{pass_key}", expires_in: ttl) do
|
18
|
+
not ApiClient.authenticated?(client_id, pass_key)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
14
22
|
end
|
15
23
|
end
|
@@ -5,9 +5,10 @@ module ClientAuthenticator
|
|
5
5
|
@table_name = 'whitelisted_clients'
|
6
6
|
@client_id_field = 'client_id'
|
7
7
|
@password_field = 'pass_key'
|
8
|
+
@cache_expiry_duration = 12.hours
|
8
9
|
end
|
9
10
|
|
10
|
-
attr_accessor :table_name, :client_id_field, :password_field
|
11
|
+
attr_accessor :table_name, :client_id_field, :password_field, :cache_expiry_duration
|
11
12
|
end
|
12
13
|
|
13
14
|
def self.configuration
|
@@ -19,16 +19,25 @@ RSpec.describe ClientAuthenticator do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
context 'client authentication' do
|
22
|
-
let(:client_id) { 'clientid' }
|
23
|
-
let(:pass_key) { 'pass_key' }
|
22
|
+
let!(:client_id) { 'clientid' }
|
23
|
+
let!(:pass_key) { 'pass_key' }
|
24
24
|
let(:header) { {'client-id': client_id, 'pass-key': pass_key}.with_indifferent_access }
|
25
25
|
let(:request) { Request.new(header) }
|
26
26
|
let(:auth) { auth = Authorizer.new
|
27
27
|
auth.request = request
|
28
28
|
auth
|
29
29
|
}
|
30
|
+
let(:cache) { double('cache') }
|
31
|
+
|
30
32
|
|
31
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
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
32
41
|
it 'when authorised, should not render 401' do
|
33
42
|
expect(ClientAuthenticator::ApiClient).to receive(:authenticated?).with(client_id, pass_key).and_return(true)
|
34
43
|
expect(auth).not_to receive(:render)
|
data/spec/spec_helper.rb
CHANGED
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.
|
4
|
+
version: 1.0.1
|
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-
|
12
|
+
date: 2017-04-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -67,34 +67,6 @@ dependencies:
|
|
67
67
|
- - ">="
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
|
-
- !ruby/object:Gem::Dependency
|
71
|
-
name: factory_girl_rails
|
72
|
-
requirement: !ruby/object:Gem::Requirement
|
73
|
-
requirements:
|
74
|
-
- - ">="
|
75
|
-
- !ruby/object:Gem::Version
|
76
|
-
version: '0'
|
77
|
-
type: :development
|
78
|
-
prerelease: false
|
79
|
-
version_requirements: !ruby/object:Gem::Requirement
|
80
|
-
requirements:
|
81
|
-
- - ">="
|
82
|
-
- !ruby/object:Gem::Version
|
83
|
-
version: '0'
|
84
|
-
- !ruby/object:Gem::Dependency
|
85
|
-
name: shoulda-matchers
|
86
|
-
requirement: !ruby/object:Gem::Requirement
|
87
|
-
requirements:
|
88
|
-
- - ">="
|
89
|
-
- !ruby/object:Gem::Version
|
90
|
-
version: '0'
|
91
|
-
type: :development
|
92
|
-
prerelease: false
|
93
|
-
version_requirements: !ruby/object:Gem::Requirement
|
94
|
-
requirements:
|
95
|
-
- - ">="
|
96
|
-
- !ruby/object:Gem::Version
|
97
|
-
version: '0'
|
98
70
|
- !ruby/object:Gem::Dependency
|
99
71
|
name: activerecord
|
100
72
|
requirement: !ruby/object:Gem::Requirement
|