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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e4428f1bb86beaa8e564058808065147864c854
4
- data.tar.gz: f44ce71992a1839af175abeb7f42e1844650ac2b
3
+ metadata.gz: 2fc9b51913b6cd5ffd49892766c365909bf8dbeb
4
+ data.tar.gz: ed240716c5b7e7998f79ec736058a91ba8dbd5fa
5
5
  SHA512:
6
- metadata.gz: 2647045f499caf50754429913d90bb63d22567847400e40b696b2b95186b75f05116f50f1871512ab390cef44595600e00ba33e7a0d4c867040c1204583b0735
7
- data.tar.gz: c95736663a89cc03ddf72eab7758c04af18e66ed6739efe3339f71797311d73c2c905199fefc484d16cc8798b2f513f09f706884fbb192b61724729e45bebf73
6
+ metadata.gz: ec186fb050688ccc32d3fd350cbebc3025cad2e8a87c1c79601569ef7945863eb8ede6ccc4aa1645ea79e6714f2fb37386084b566556394089a0c48e2a9ffbac
7
+ data.tar.gz: a43323c521ef2e409c9bfb188a145e388eae599580b5feff07975be242027f5d5b6912e0e7c647da3862d8270f46a40865f28ca5a4444af5f5d9bbbb86097eec
data/.travis.yml CHANGED
@@ -1,4 +1 @@
1
1
  language: ruby
2
- deploy:
3
- provider: rubygems
4
- gemspec: client_authenticator.gemspec
data/Gemfile CHANGED
@@ -5,4 +5,6 @@ gemspec
5
5
 
6
6
  group :test do
7
7
  gem 'pry'
8
+ gem 'simplecov'
9
+ gem 'rubocop'
8
10
  end
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
- # ClientAuthenticator
1
+ # Client Authenticator
2
+ [![Build Status](https://travis-ci.org/gojek-engineering/client-authenticator-rb.svg?branch=master)](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 client_authenticatore
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? || (not ApiClient.authenticated?(client_id, pass_key))
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
@@ -1,3 +1,3 @@
1
1
  module ClientAuthenticator
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -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
@@ -1,3 +1,7 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+ SimpleCov.minimum_coverage 88
4
+
1
5
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
6
  require 'generator_spec'
3
7
  require 'client_authenticator'
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.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-08 00:00:00.000000000 Z
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