pansophy_authenticator 0.1.0 → 0.2.0

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: 5389e1b9e496f50d0e02ff349030429e5a05afc2
4
- data.tar.gz: 51e58d9bd27a7b1870723408686ec61c0f248e4e
3
+ metadata.gz: 7af7e60f259d600f0f144d92093cf6e72281e89f
4
+ data.tar.gz: 19fcf4ca69a64465d75e2a963744ccff2d3738a3
5
5
  SHA512:
6
- metadata.gz: ba533aef72cc47c5c1cb4aae7d76e158f3d0a6eff8ece870ebed26b62a563fb5b3b6ffdc244ab4ab80e855898f21b515d546cd1ad28bdf3bdb16b7dd689c489e
7
- data.tar.gz: 2616febd95f06d566a2a652f89eeed0644e2c8323167f6811ef39716423a779a58471067888be394fa9eccd5a30f5cd7e785ba262dc01c04f3312e8cf2961774
6
+ metadata.gz: 1aa802262c4c530e651a9a3c32b5237827181c54c550802f3087f902828ad232ac5c0ea8efb0f967d04e6c09e315b38409289b720c383f5877b483cbbf236537
7
+ data.tar.gz: 617e24f95dec81945c00dd7363351b1c557fcf5a8827e6dd54550699d0c5ed7244f15de7340848e345a525998414765e4ea41c54dcfc65e591e8751a5ee5cf97
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ ## 0.2.0 (2016-02-02)
2
+
3
+ Features:
4
+
5
+ - caching via cache store
6
+
7
+ ## 0.1.0 (2016-02-01)
8
+
9
+ Initial release
data/README.md CHANGED
@@ -90,6 +90,48 @@ AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
90
90
  AWS_REGION=ap-southeast-2
91
91
  ```
92
92
 
93
+ ### Caching
94
+
95
+ By default application keys are cached in memory
96
+
97
+ The cache can be cleared, forcing the keys to be fetched again on the next request, by calling
98
+
99
+ ```ruby
100
+ PansophyAuthenticator.clear_cached_keys
101
+ ```
102
+
103
+ A different cache store can be specified during initial configuration
104
+
105
+ ```ruby
106
+ PansophyAuthenticator.configure do |configuration|
107
+ # ... previous configuration options
108
+ configuration.cache_store = MyCacheStore.new
109
+ end
110
+ ```
111
+
112
+ A cache store must respond to the following methods:
113
+
114
+ ```ruby
115
+ class MyCacheStore
116
+ def read(key)
117
+ # Fetches data from the cache, using the given key.
118
+ # Returns the data if its in the cache with the given key, nil otherwise.
119
+ end
120
+
121
+ def write(key, value)
122
+ # Writes the value to the cache for the given key.
123
+ end
124
+
125
+ def delete(key)
126
+ # Deletes the entry in the cache for the given key.
127
+ end
128
+
129
+ def exist?(key)
130
+ # Returns true if the cache contains an entry for the given key, false otherwise.
131
+ end
132
+ end
133
+ ```
134
+
93
135
  ## Development
94
136
 
95
137
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -4,7 +4,7 @@ module PansophyAuthenticator
4
4
  class ApplicationKeys
5
5
  include Singleton
6
6
 
7
- %i(own key valid? validate!).each do |method|
7
+ %i(own key valid? validate! clear_cache).each do |method|
8
8
  define_singleton_method(method) { |*args| instance.send(method, *args) }
9
9
  end
10
10
 
@@ -24,17 +24,29 @@ module PansophyAuthenticator
24
24
  matcher(application).validate!(key)
25
25
  end
26
26
 
27
+ def clear_cache
28
+ cache.delete
29
+ end
30
+
27
31
  private
28
32
 
29
33
  def matcher(application)
30
- Matcher.new(retriever.instance.keys, application)
34
+ Matcher.new(keys, application)
35
+ end
36
+
37
+ def keys
38
+ cache.fetch { fetcher.keys }
39
+ end
40
+
41
+ def cache
42
+ Cache.new(configuration.cache_store)
31
43
  end
32
44
 
33
- def retriever
45
+ def fetcher
34
46
  if PansophyAuthenticator.remote?
35
- Remote::ApplicationKeys
47
+ Remote::Fetcher.new
36
48
  else
37
- Local::ApplicationKeys
49
+ Local::Fetcher.new
38
50
  end
39
51
  end
40
52
 
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ module PansophyAuthenticator
3
+ class Cache
4
+ CACHE_KEY = 'pansophy_authenticator_application_keys'.freeze
5
+
6
+ def initialize(cache_store)
7
+ @cache_store = cache_store
8
+ end
9
+
10
+ def read
11
+ @cache_store.read CACHE_KEY
12
+ end
13
+
14
+ def write(value)
15
+ @cache_store.write CACHE_KEY, value
16
+ end
17
+
18
+ def delete
19
+ @cache_store.delete CACHE_KEY
20
+ end
21
+
22
+ def exist?
23
+ @cache_store.exist? CACHE_KEY
24
+ end
25
+
26
+ def fetch
27
+ return read if exist?
28
+ return nil unless block_given?
29
+ yield(CACHE_KEY).tap do |value|
30
+ write(value)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ module PansophyAuthenticator
2
+ module CacheStores
3
+ class Memory
4
+ def initialize
5
+ @hash = {}
6
+ end
7
+
8
+ def read(key)
9
+ @hash[key.to_s]
10
+ end
11
+
12
+ def write(key, value)
13
+ @hash[key.to_s] = value
14
+ end
15
+
16
+ def delete(key)
17
+ @hash.delete(key.to_s)
18
+ end
19
+
20
+ def exist?(key)
21
+ @hash.key?(key.to_s)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -2,24 +2,43 @@ module PansophyAuthenticator
2
2
  module Configuration
3
3
  class Configurator
4
4
  attr_accessor :local, :bucket_name, :file_path, :configuration_path, :application
5
+ attr_writer :cache_store
5
6
 
6
7
  def configuration
7
- from_env
8
+ build_configuration
9
+ end
10
+
11
+ def cache_store
12
+ @cache_store ||= CacheStores::Memory.new
8
13
  end
9
14
 
10
15
  private
11
16
 
17
+ def build_configuration
18
+ Instance.new(
19
+ local: config_values.local,
20
+ bucket_name: config_values.bucket_name,
21
+ file_path: config_values.file_path,
22
+ application: config_values.application,
23
+ cache_store: cache_store
24
+ )
25
+ end
26
+
27
+ def config_values
28
+ @config_values ||= from_env
29
+ end
30
+
12
31
  def base_config
13
32
  return self if @configuration_path.nil?
14
33
  from_file
15
34
  end
16
35
 
17
36
  def from_file
18
- FromFile.new(self).configuration
37
+ FromFile.new(self)
19
38
  end
20
39
 
21
40
  def from_env
22
- FromEnv.new(base_config).configuration
41
+ FromEnv.new(base_config)
23
42
  end
24
43
  end
25
44
  end
@@ -5,17 +5,6 @@ module PansophyAuthenticator
5
5
  @base_configuration = base_configuration
6
6
  end
7
7
 
8
- def configuration
9
- Instance.new(
10
- local: local,
11
- bucket_name: bucket_name,
12
- file_path: file_path,
13
- application: application
14
- )
15
- end
16
-
17
- private
18
-
19
8
  def local
20
9
  local = ENV.fetch('PANSOPHY_AUTHENTICATOR_LOCAL') { @base_configuration.local }
21
10
  local.to_s == 'true'
@@ -13,17 +13,6 @@ module PansophyAuthenticator
13
13
  @base_configuration = base_configuration
14
14
  end
15
15
 
16
- def configuration
17
- Instance.new(
18
- local: local,
19
- bucket_name: bucket_name,
20
- file_path: file_path,
21
- application: application
22
- )
23
- end
24
-
25
- private
26
-
27
16
  def local
28
17
  content.fetch('local') { @base_configuration.local }
29
18
  end
@@ -40,6 +29,8 @@ module PansophyAuthenticator
40
29
  content.fetch('application') { @base_configuration.application }
41
30
  end
42
31
 
32
+ private
33
+
43
34
  def pathname
44
35
  Pathname.new(@base_configuration.configuration_path)
45
36
  end
@@ -3,7 +3,7 @@ require 'anima'
3
3
  module PansophyAuthenticator
4
4
  module Configuration
5
5
  class Instance
6
- include Anima.new(:local, :bucket_name, :file_path, :application)
6
+ include Anima.new :local, :bucket_name, :file_path, :application, :cache_store
7
7
 
8
8
  def local?
9
9
  @local
@@ -1,15 +1,13 @@
1
- require 'singleton'
2
1
  require 'memoizable'
3
2
  require 'yamload'
4
3
 
5
4
  module PansophyAuthenticator
6
5
  module Local
7
- class ApplicationKeys
8
- include Singleton
6
+ class BuildLoader
9
7
  include Memoizable
10
8
 
11
- def keys
12
- content
9
+ def call
10
+ Yamload::Loader.new(filename, dirname)
13
11
  end
14
12
 
15
13
  private
@@ -27,15 +25,6 @@ module PansophyAuthenticator
27
25
  pathname.basename('.yml')
28
26
  end
29
27
 
30
- def content
31
- loader.content
32
- end
33
- memoize :content
34
-
35
- def loader
36
- Yamload::Loader.new(filename, dirname)
37
- end
38
-
39
28
  def configuration
40
29
  PansophyAuthenticator.configuration
41
30
  end
@@ -0,0 +1,15 @@
1
+ module PansophyAuthenticator
2
+ module Local
3
+ class Fetcher
4
+ def keys
5
+ loader.content
6
+ end
7
+
8
+ private
9
+
10
+ def loader
11
+ @loader ||= BuildLoader.new.call
12
+ end
13
+ end
14
+ end
15
+ end
@@ -3,4 +3,5 @@ module PansophyAuthenticator
3
3
  end
4
4
  end
5
5
 
6
- require 'pansophy_authenticator/local/application_keys'
6
+ require 'pansophy_authenticator/local/build_loader'
7
+ require 'pansophy_authenticator/local/fetcher'
@@ -1,20 +1,18 @@
1
- require 'singleton'
2
- require 'memoizable'
3
1
  require 'pansophy'
4
2
 
5
3
  module PansophyAuthenticator
6
4
  module Remote
7
- class ApplicationKeys
8
- include Singleton
9
- include Memoizable
10
-
5
+ class Fetcher
11
6
  def keys
12
- Pansophy.read(configuration.bucket_name, configuration.file_path)
7
+ @keys ||= fetch
13
8
  end
14
- memoize :keys
15
9
 
16
10
  private
17
11
 
12
+ def fetch
13
+ Pansophy.read(configuration.bucket_name, configuration.file_path)
14
+ end
15
+
18
16
  def configuration
19
17
  PansophyAuthenticator.configuration
20
18
  end
@@ -3,4 +3,4 @@ module PansophyAuthenticator
3
3
  end
4
4
  end
5
5
 
6
- require 'pansophy_authenticator/remote/application_keys'
6
+ require 'pansophy_authenticator/remote/fetcher'
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module PansophyAuthenticator
3
- VERSION = '0.1.0'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
@@ -23,6 +23,10 @@ module PansophyAuthenticator
23
23
  ApplicationKeys.validate!(application, key)
24
24
  end
25
25
 
26
+ def self.clear_cached_keys
27
+ ApplicationKeys.clear_cache
28
+ end
29
+
26
30
  %i(key valid?).each do |method|
27
31
  define_singleton_method(method) { |*args| ApplicationKeys.send(method, *args) }
28
32
  end
@@ -35,3 +39,5 @@ require 'pansophy_authenticator/application_keys'
35
39
  require 'pansophy_authenticator/matcher'
36
40
  require 'pansophy_authenticator/local'
37
41
  require 'pansophy_authenticator/remote'
42
+ require 'pansophy_authenticator/cache_stores/memory'
43
+ require 'pansophy_authenticator/cache'
@@ -28,15 +28,16 @@ Gem::Specification.new do |spec|
28
28
 
29
29
  spec.add_dependency 'memoize', '~> 1.3'
30
30
  spec.add_dependency 'anima', '~> 0.3'
31
- spec.add_dependency 'pansophy', '~> 0.2.6'
32
- spec.add_dependency 'yamload', '~> 0.2.1'
31
+ spec.add_dependency 'pansophy', '~> 0.3'
32
+ spec.add_dependency 'yamload', '~> 0.2'
33
33
 
34
34
  spec.add_development_dependency 'bundler', '~> 1.11'
35
35
  spec.add_development_dependency 'rake', '~> 10.0'
36
- spec.add_development_dependency 'rspec', '~> 3.0'
37
- spec.add_development_dependency 'simplecov'
38
- spec.add_development_dependency 'simplecov-rcov'
39
- spec.add_development_dependency 'coveralls'
40
- spec.add_development_dependency 'rubocop'
41
- spec.add_development_dependency 'travis'
36
+ spec.add_development_dependency 'rspec', '~> 3.4'
37
+ spec.add_development_dependency 'timecop', '~> 0.8'
38
+ spec.add_development_dependency 'simplecov', '~> 0.11'
39
+ spec.add_development_dependency 'simplecov-rcov', '~> 0.2'
40
+ spec.add_development_dependency 'coveralls', '~> 0.8'
41
+ spec.add_development_dependency 'rubocop', '~> 0.36'
42
+ spec.add_development_dependency 'travis', '~> 1.8'
42
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pansophy_authenticator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Berardi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-01 00:00:00.000000000 Z
11
+ date: 2016-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: memoize
@@ -44,28 +44,28 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.2.6
47
+ version: '0.3'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.2.6
54
+ version: '0.3'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: yamload
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 0.2.1
61
+ version: '0.2'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 0.2.1
68
+ version: '0.2'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -100,84 +100,98 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '3.0'
103
+ version: '3.4'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '3.0'
110
+ version: '3.4'
111
+ - !ruby/object:Gem::Dependency
112
+ name: timecop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.8'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.8'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: simplecov
113
127
  requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
- - - ">="
129
+ - - "~>"
116
130
  - !ruby/object:Gem::Version
117
- version: '0'
131
+ version: '0.11'
118
132
  type: :development
119
133
  prerelease: false
120
134
  version_requirements: !ruby/object:Gem::Requirement
121
135
  requirements:
122
- - - ">="
136
+ - - "~>"
123
137
  - !ruby/object:Gem::Version
124
- version: '0'
138
+ version: '0.11'
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: simplecov-rcov
127
141
  requirement: !ruby/object:Gem::Requirement
128
142
  requirements:
129
- - - ">="
143
+ - - "~>"
130
144
  - !ruby/object:Gem::Version
131
- version: '0'
145
+ version: '0.2'
132
146
  type: :development
133
147
  prerelease: false
134
148
  version_requirements: !ruby/object:Gem::Requirement
135
149
  requirements:
136
- - - ">="
150
+ - - "~>"
137
151
  - !ruby/object:Gem::Version
138
- version: '0'
152
+ version: '0.2'
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: coveralls
141
155
  requirement: !ruby/object:Gem::Requirement
142
156
  requirements:
143
- - - ">="
157
+ - - "~>"
144
158
  - !ruby/object:Gem::Version
145
- version: '0'
159
+ version: '0.8'
146
160
  type: :development
147
161
  prerelease: false
148
162
  version_requirements: !ruby/object:Gem::Requirement
149
163
  requirements:
150
- - - ">="
164
+ - - "~>"
151
165
  - !ruby/object:Gem::Version
152
- version: '0'
166
+ version: '0.8'
153
167
  - !ruby/object:Gem::Dependency
154
168
  name: rubocop
155
169
  requirement: !ruby/object:Gem::Requirement
156
170
  requirements:
157
- - - ">="
171
+ - - "~>"
158
172
  - !ruby/object:Gem::Version
159
- version: '0'
173
+ version: '0.36'
160
174
  type: :development
161
175
  prerelease: false
162
176
  version_requirements: !ruby/object:Gem::Requirement
163
177
  requirements:
164
- - - ">="
178
+ - - "~>"
165
179
  - !ruby/object:Gem::Version
166
- version: '0'
180
+ version: '0.36'
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: travis
169
183
  requirement: !ruby/object:Gem::Requirement
170
184
  requirements:
171
- - - ">="
185
+ - - "~>"
172
186
  - !ruby/object:Gem::Version
173
- version: '0'
187
+ version: '1.8'
174
188
  type: :development
175
189
  prerelease: false
176
190
  version_requirements: !ruby/object:Gem::Requirement
177
191
  requirements:
178
- - - ">="
192
+ - - "~>"
179
193
  - !ruby/object:Gem::Version
180
- version: '0'
194
+ version: '1.8'
181
195
  description: By configuring a set of applications authentication keys in a filestored
182
196
  in an S3 bucket, applications can authenticate with each otherby submitting their
183
197
  authentication key, which the receiver can matchagainst the key stored in S3.S3
@@ -193,6 +207,7 @@ files:
193
207
  - ".rubocop.yml"
194
208
  - ".ruby-version"
195
209
  - ".travis.yml"
210
+ - CHANGELOG.md
196
211
  - CODE_OF_CONDUCT.md
197
212
  - Gemfile
198
213
  - LICENSE
@@ -202,16 +217,19 @@ files:
202
217
  - bin/setup
203
218
  - lib/pansophy_authenticator.rb
204
219
  - lib/pansophy_authenticator/application_keys.rb
220
+ - lib/pansophy_authenticator/cache.rb
221
+ - lib/pansophy_authenticator/cache_stores/memory.rb
205
222
  - lib/pansophy_authenticator/configuration.rb
206
223
  - lib/pansophy_authenticator/configuration/configurator.rb
207
224
  - lib/pansophy_authenticator/configuration/from_env.rb
208
225
  - lib/pansophy_authenticator/configuration/from_file.rb
209
226
  - lib/pansophy_authenticator/configuration/instance.rb
210
227
  - lib/pansophy_authenticator/local.rb
211
- - lib/pansophy_authenticator/local/application_keys.rb
228
+ - lib/pansophy_authenticator/local/build_loader.rb
229
+ - lib/pansophy_authenticator/local/fetcher.rb
212
230
  - lib/pansophy_authenticator/matcher.rb
213
231
  - lib/pansophy_authenticator/remote.rb
214
- - lib/pansophy_authenticator/remote/application_keys.rb
232
+ - lib/pansophy_authenticator/remote/fetcher.rb
215
233
  - lib/pansophy_authenticator/version.rb
216
234
  - pansophy_authenticator.gemspec
217
235
  homepage: https://github.com/sealink/pansophy_authenticator