zaikio-jwt_auth 0.4.3 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ac13b8d1a727e9aefeef7e1da082a3eec4bf48ac8f149ef4cc89ac44ee35d49
4
- data.tar.gz: 323355b6f5180bf9662b715c8d2cf8ed94311fa1095e5da2627c8a71cd0f442a
3
+ metadata.gz: 4103bf1e59a9fa8a68a868c33d785082993255fe24616a01c1113a5a8026dcfe
4
+ data.tar.gz: e0be8641749a07f97620c73a56a97211ba51e9fe4f9dc6e1bdbaa5dd0422b0e3
5
5
  SHA512:
6
- metadata.gz: 386ec7f2a8f1bc0a0d2f29495cc4b5754cd2eadce857555eb68a72f9546f76095ba2f0f0c3fdbaa5982a2503ee400480e81418dea5a06e85265380384843e350
7
- data.tar.gz: 044a91585bf67a5ba411716ea361a1c85eeab91ef06cbe17b1cd1f03c83a261c0a4e8ef5ebe1e87e3c9e550678fdbcae45859448031c5e5b2983affbb829fd0f
6
+ metadata.gz: 28c8d6ea0b394450f7ae0027e9388fca1a9bd449213b6825356c65452e8a3efa4ab670802537cf9e08146d8a6980b5cbb22f2efa5cdec4c4b271b4c02b759fcf
7
+ data.tar.gz: 647aa957f6e7d82ca26b50169fd265b30e144dcef5a26a8273e786c388569ac09babed909a1ddf295255df404f097bc5a867ff5520920d81f37ae4a48a5c60da
@@ -5,6 +5,15 @@ require "logger"
5
5
  module Zaikio
6
6
  module JWTAuth
7
7
  class DirectoryCache
8
+ class UpdateJob < ::ActiveJob::Base
9
+ def perform(directory_path)
10
+ DirectoryCache.fetch(directory_path)
11
+ true # This job will always re-queue until it succeeds.
12
+ end
13
+ end
14
+
15
+ BadResponseError = Class.new(StandardError)
16
+
8
17
  class << self
9
18
  def fetch(directory_path, options = {})
10
19
  cache = Zaikio::JWTAuth.configuration.redis.get("zaikio::jwt_auth::#{directory_path}")
@@ -12,7 +21,8 @@ module Zaikio
12
21
  json = Oj.load(cache) if cache
13
22
 
14
23
  if !cache || options[:invalidate] || cache_expired?(json, options[:expires_after])
15
- return reload(directory_path)
24
+ new_values = reload_or_enqueue(directory_path)
25
+ return new_values || json["data"]
16
26
  end
17
27
 
18
28
  json["data"]
@@ -37,29 +47,29 @@ module Zaikio
37
47
  DateTime.strptime(json["fetched_at"].to_s, "%s") < Time.now.utc - (expires_after || 1.hour)
38
48
  end
39
49
 
40
- def reload(directory_path)
41
- retries = 0
42
-
43
- begin
44
- data = fetch_from_directory(directory_path)
45
- Zaikio::JWTAuth.configuration.redis.set("zaikio::jwt_auth::#{directory_path}", {
46
- fetched_at: Time.now.to_i,
47
- data: data
48
- }.to_json)
49
-
50
- data
51
- rescue Errno::ECONNREFUSED, Net::ReadTimeout => e
52
- raise unless (retries += 1) <= 3
50
+ def reload_or_enqueue(directory_path)
51
+ data = fetch_from_directory(directory_path)
52
+ Zaikio::JWTAuth.configuration.redis.set("zaikio::jwt_auth::#{directory_path}", {
53
+ fetched_at: Time.now.to_i,
54
+ data: data
55
+ }.to_json)
53
56
 
54
- Zaikio::JWTAuth.configuration.logger.log("Timeout (#{e}), retrying in 1 second...")
55
- sleep(1)
56
- retry
57
- end
57
+ data
58
+ rescue Errno::ECONNREFUSED, Net::ReadTimeout, BadResponseError
59
+ Zaikio::JWTAuth.configuration.logger.info("Error updating DirectoryCache(#{directory_path}), enqueueing job to update")
60
+ UpdateJob.set(wait: 10.seconds).perform_later(directory_path)
61
+ nil
58
62
  end
59
63
 
60
64
  def fetch_from_directory(directory_path)
61
65
  uri = URI("#{Zaikio::JWTAuth.configuration.host}/#{directory_path}")
62
- Oj.load(Net::HTTP.get(uri))
66
+ http = Net::HTTP.new(uri.host, uri.port)
67
+ http.use_ssl = uri.scheme == "https"
68
+ response = http.request(Net::HTTP::Get.new(uri.request_uri))
69
+ raise BadResponseError unless (200..299).cover?(response.code.to_i)
70
+ raise BadResponseError unless response["content-type"].to_s.include?("application/json")
71
+
72
+ Oj.load(response.body)
63
73
  end
64
74
  end
65
75
  end
@@ -1,5 +1,5 @@
1
1
  module Zaikio
2
2
  module JWTAuth
3
- VERSION = "0.4.3".freeze
3
+ VERSION = "1.0.1".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zaikio-jwt_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - crispymtn
@@ -10,8 +10,22 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-03-17 00:00:00.000000000 Z
13
+ date: 2021-04-28 00:00:00.000000000 Z
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activejob
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
15
29
  - !ruby/object:Gem::Dependency
16
30
  name: oj
17
31
  requirement: !ruby/object:Gem::Requirement
@@ -27,7 +41,7 @@ dependencies:
27
41
  - !ruby/object:Gem::Version
28
42
  version: 3.0.0
29
43
  - !ruby/object:Gem::Dependency
30
- name: rails
44
+ name: railties
31
45
  requirement: !ruby/object:Gem::Requirement
32
46
  requirements:
33
47
  - - ">="