usps-support 0.2.37 → 0.2.39

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
  SHA256:
3
- metadata.gz: d61c4a69572f5f429e1695b6cf279d2307736b0b17aa0a7b2712a00ae995903c
4
- data.tar.gz: 363f61ca6ada0f8d0420eedeb7f489c71b5b61a121638e1363b19fed6265f5fa
3
+ metadata.gz: 4b5af1f3316c237eb45e235fd0d096456f088d85726a708687ad201fb3e3a8aa
4
+ data.tar.gz: d51a0b3a79f5251b535c054c2bc27a8c43c8dd223e7dd43c93d476d56b0e211e
5
5
  SHA512:
6
- metadata.gz: 62156ff57b04bc404b5233e4027e4384da1b13ad0f72e9b46d64a64dde59cf76dac87ba0ea188f6339dc8337f0feacf0f54bbb05984ef38d128afcc71ecef83f
7
- data.tar.gz: 22d8b7c9f23afafd350f06a06ff90f3a84f905a96d6570967a77f83b743cd8627d242622145e120f9d98123c3abd275f48b8877164c4747ccb3eeaeaf2c26d3e
6
+ metadata.gz: 87eff25ec01929faa12ac277766518a0a22d7596ab5f34b1a94ace66f30765308ef9fb3865bed937c07abe17305f2cdf2974c883a49534d4bf70dbe8c6029744
7
+ data.tar.gz: d7b743c078fbb4fa6d2306467fa16e7b384b1dc25821e23b89bd19ffa76129c615df863f6b3c7f1ab8aed3b82e87bfcc1aaf3670763695849e41c572f714a28f
data/config/routes.rb CHANGED
@@ -1,4 +1,11 @@
1
1
  Usps::Support::Engine.routes.draw do
2
+ if defined?(Sidekiq::Web)
3
+ unless Sidekiq::Web.middlewares.any? { it.first == Usps::Support::SidekiqAuth }
4
+ Sidekiq::Web.use(Usps::Support::SidekiqAuth)
5
+ end
6
+ mount Sidekiq::Web => '/sidekiq'
7
+ end
8
+
2
9
  resource :admin, only: [], controller: 'usps/support/admins' do
3
10
  collection do
4
11
  post :impersonate
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rails/engine'
4
+ require 'usps/support/sidekiq_auth'
4
5
 
5
6
  module Usps
6
7
  module Support
@@ -71,7 +71,7 @@ module Usps::Support::Models
71
71
  title: 'SERVER WILL RESTART SOON',
72
72
  body: "Please do not attempt to make any changes for a moment.\n" \
73
73
  "We'll let you know when the server's back up.",
74
- timeout: 0,
74
+ timeout: 60_000,
75
75
  id: IDS[:restarting]
76
76
  )
77
77
  end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cgi'
4
+
5
+ module Usps::Support
6
+ # Rack middleware that gates Sidekiq::Web (or any inner Rack app) behind the
7
+ # same JWT/admin authentication used by the host app's controllers.
8
+ #
9
+ # Routes-level constraints can only return true/false, so an expired session
10
+ # at /sidekiq used to silently 404 — the controller refresh flow never ran.
11
+ # Running this as middleware lets us issue a 302 to the login refresh URL the
12
+ # same way `Usps::JwtAuth::Concern#redirect_to_login` does.
13
+ #
14
+ # Usage in an engine route file:
15
+ #
16
+ # if defined?(::Sidekiq::Web)
17
+ # ::Sidekiq::Web.use(Usps::Support::SidekiqAuth)
18
+ # mount ::Sidekiq::Web => '/sidekiq'
19
+ # end
20
+ #
21
+ class SidekiqAuth
22
+ LOGIN_URL = 'https://www.usps.org/jwt/'
23
+
24
+ def initialize(app)
25
+ @app = app
26
+ end
27
+
28
+ def call(env)
29
+ request = ActionDispatch::Request.new(env)
30
+ member = decode_member(request)
31
+ return forbidden unless member && Usps::JwtAuth.config.is_admin.call(member)
32
+
33
+ @app.call(env)
34
+ rescue JWT::DecodeError
35
+ clear_jwt(request)
36
+ redirect_to_login(request)
37
+ end
38
+
39
+ private
40
+
41
+ def decode_member(request)
42
+ data = Usps::JwtAuth.decode(
43
+ fetch_jwt(request),
44
+ audience: [Usps::JwtAuth.config.audience],
45
+ issuer: Regexp.union(Usps::JwtAuth.config.issuers)
46
+ )
47
+ Usps::JwtAuth.config.find_member.call(data['certificate'])
48
+ rescue ActiveRecord::RecordNotFound
49
+ nil
50
+ end
51
+
52
+ def fetch_jwt(request)
53
+ request.session[:jwt] || request.cookie_jar[:jwt]
54
+ end
55
+
56
+ def clear_jwt(request)
57
+ request.session[:jwt] = nil
58
+ request.cookie_jar.delete(:jwt, domain: cookie_domain)
59
+ end
60
+
61
+ def cookie_domain
62
+ Usps::JwtAuth.config.environment.production? ? '.aws.usps.org' : 'localhost'
63
+ end
64
+
65
+ def redirect_to_login(request)
66
+ url = login_url_base
67
+ url = "#{url}&path=#{CGI.escape(request.fullpath)}"
68
+ [302, { 'Location' => url, 'Content-Type' => 'text/html' }, []]
69
+ end
70
+
71
+ def login_url_base
72
+ if Usps::JwtAuth.config.environment.development?
73
+ "#{LOGIN_URL}?local&port=#{ENV.fetch('PORT', '3000')}"
74
+ else
75
+ "#{LOGIN_URL}?application=#{Usps::JwtAuth.config.audience}"
76
+ end
77
+ end
78
+
79
+ def forbidden
80
+ [403, { 'Content-Type' => 'text/plain' }, ['Forbidden']]
81
+ end
82
+ end
83
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Usps
4
4
  module Support
5
- VERSION = '0.2.37'
5
+ VERSION = '0.2.39'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usps-support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.37
4
+ version: 0.2.39
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
@@ -121,6 +121,7 @@ files:
121
121
  - lib/usps/support/models/hq/squadrons/website.rb
122
122
  - lib/usps/support/models/toast.rb
123
123
  - lib/usps/support/railtie.rb
124
+ - lib/usps/support/sidekiq_auth.rb
124
125
  - lib/usps/support/version.rb
125
126
  homepage: https://github.com/unitedstatespowersquadrons/usps-support
126
127
  licenses: []