whiplash-app 0.9.1 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/whiplash/app/api_config.rb +6 -4
- data/lib/whiplash/app/canonical_host.rb +25 -0
- data/lib/whiplash/app/connections.rb +1 -1
- data/lib/whiplash/app/controller_helpers.rb +80 -0
- data/lib/whiplash/app/railtie.rb +31 -0
- data/lib/whiplash/app/version.rb +1 -1
- data/lib/whiplash/app.rb +23 -9
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 228da95848c936a8a771e6f67e6d4aa03899ab722274b2d355917593c9b83a89
|
4
|
+
data.tar.gz: 2f1f50222b2b7a578d23a88efdfd5328e2d0b772bdae0696249be13457e57db5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31ba3a5b17e6679551d961ce009efb489e3a4a49bf0cf5cf8e9d4e978626c6ae825a38dad30d62552cf619e08a78a09066cd3ffb67a04599f5dcb6f7d01bc4a4
|
7
|
+
data.tar.gz: e845ad83845b36f932d21a3fd4fa283e8ef0fc45066cc37a82f80c3c178600a3c4870c76633f9ee88aad1967b16db7e7caf280a55e91b2f0d0d7fcf4ce6965e1
|
@@ -10,6 +10,12 @@ module Whiplash
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
def rate_limit
|
14
|
+
(ENV['WHIPLASH_RATE_LIMIT'] || 25).to_i
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
13
19
|
def production_url
|
14
20
|
ENV["WHIPLASH_API_URL"] || "https://www.getwhiplash.com"
|
15
21
|
end
|
@@ -18,10 +24,6 @@ module Whiplash
|
|
18
24
|
ENV["WHIPLASH_API_URL"] || "https://sandbox.getwhiplash.com"
|
19
25
|
end
|
20
26
|
|
21
|
-
def rate_limit
|
22
|
-
(ENV['WHIPLASH_RATE_LIMIT'] || 25).to_i
|
23
|
-
end
|
24
|
-
|
25
27
|
end
|
26
28
|
end
|
27
29
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Whiplash
|
3
|
+
class App
|
4
|
+
module CanonicalHost
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def require_canonical_host!
|
10
|
+
canonical_host = ENV.fetch('CANONICAL_HOST', false).in?([true, 'true', 1, '1'])
|
11
|
+
return unless canonical_host
|
12
|
+
application_host = URI.parse(Rails.configuration.app_url).host
|
13
|
+
return if application_host == request.host
|
14
|
+
return unless request.method_symbol == :get # can't redirect PUT, POST, DELETE
|
15
|
+
|
16
|
+
redirect_to_canonical_host request.query_parameters
|
17
|
+
end
|
18
|
+
|
19
|
+
def redirect_to_canonical_host(query_params, status=301)
|
20
|
+
redirect_to "#{Rails.configuration.app_url}#{request.path}#{'?' if query_params.to_query.present?}#{query_params.to_query}", status: status
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -27,7 +27,7 @@ module Whiplash
|
|
27
27
|
|
28
28
|
def app_request(options={})
|
29
29
|
return base_app_request(options) unless defined?(Sidekiq)
|
30
|
-
limiter = Sidekiq::Limiter.window('whiplash-core', self.rate_limit, :second, wait_timeout: 15)
|
30
|
+
limiter = Sidekiq::Limiter.window('whiplash-core', self.class.rate_limit, :second, wait_timeout: 15)
|
31
31
|
limiter.within_limit do
|
32
32
|
base_app_request(options)
|
33
33
|
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Whiplash
|
3
|
+
class App
|
4
|
+
module ControllerHelpers
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
helper_method :cookie_domain,
|
9
|
+
:core_url,
|
10
|
+
:core_url_for,
|
11
|
+
:current_user
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def cookie_domain
|
17
|
+
'.' + URI.parse(core_url).host
|
18
|
+
end
|
19
|
+
|
20
|
+
def core_url
|
21
|
+
ENV['WHIPLASH_API_URL']
|
22
|
+
end
|
23
|
+
|
24
|
+
def core_url_for(path)
|
25
|
+
[core_url, path].join('/')
|
26
|
+
end
|
27
|
+
|
28
|
+
def current_user
|
29
|
+
return if cookies[:user].blank?
|
30
|
+
begin
|
31
|
+
@current_user ||= JSON.parse(cookies[:user])
|
32
|
+
rescue StandardError => e
|
33
|
+
Rails.logger.warn "User could not be initialized: #{e.message}"
|
34
|
+
@current_user = nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def http_scheme
|
39
|
+
URI(core_url).scheme
|
40
|
+
end
|
41
|
+
|
42
|
+
def init_whiplash_api(options = {})
|
43
|
+
return redirect_to core_url_for('login') if cookies[:oauth_token].blank?
|
44
|
+
token = {access_token: cookies[:oauth_token]}
|
45
|
+
begin
|
46
|
+
@whiplash_api = Whiplash::App.new(token, options)
|
47
|
+
rescue StandardError => e
|
48
|
+
Rails.logger.warn "API failed to initialize: #{e.message}"
|
49
|
+
@whiplash_api = nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def require_user
|
54
|
+
redirect_to core_url_for('login') if current_user.blank?
|
55
|
+
end
|
56
|
+
|
57
|
+
def set_locale!
|
58
|
+
I18n.default_locale = :en
|
59
|
+
I18n.locale = current_user.try('locale') || I18n.default_locale
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
def set_current_user_cookie!(expires_at = nil)
|
64
|
+
user = @whiplash_api.get!("me").body
|
65
|
+
fields_we_care_about = %w(id email role locale first_name last_name partner_id warehouse_id customer_ids)
|
66
|
+
user_hash = user.slice(*fields_we_care_about)
|
67
|
+
expires_at ||= user['current_sign_in_expires_at']
|
68
|
+
|
69
|
+
shared_values = {
|
70
|
+
expires: DateTime.parse(expires_at),
|
71
|
+
secure: http_scheme == 'https',
|
72
|
+
samesite: :strict,
|
73
|
+
domain: cookie_domain
|
74
|
+
}
|
75
|
+
cookies[:user] = shared_values.merge(value: user_hash.to_json)
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Whiplash
|
4
|
+
class App
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
|
7
|
+
config.before_configuration do |app|
|
8
|
+
# App name/etc, mainly for consistency in logging
|
9
|
+
app_name = app.class.module_parent.name.underscore.dasherize
|
10
|
+
app.config.environment_key = ENV.fetch('ENVIRONMENT_KEY', Rails.env.to_s)
|
11
|
+
app.config.application_key = ENV.fetch('APPLICATION_KEY', app_name)
|
12
|
+
app.config.application_name_space = [config.application_key, config.environment_key].join('-')
|
13
|
+
|
14
|
+
# session settings
|
15
|
+
session_days = 30
|
16
|
+
session_seconds = session_days * 24 * 60 * 60
|
17
|
+
session_length = ENV.fetch('SESSION_LENGTH', session_seconds).to_i
|
18
|
+
app.config.session_length = session_length
|
19
|
+
app.config.session_store :cookie_store, :key => '_session', :expire_after => session_length
|
20
|
+
end
|
21
|
+
|
22
|
+
initializer "whiplash_app.action_controller" do
|
23
|
+
ActiveSupport.on_load(:action_controller) do
|
24
|
+
include Whiplash::App::CanonicalHost
|
25
|
+
include Whiplash::App::ControllerHelpers
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/whiplash/app/version.rb
CHANGED
data/lib/whiplash/app.rb
CHANGED
@@ -7,12 +7,19 @@ require "errors/whiplash_api_error"
|
|
7
7
|
require "oauth2"
|
8
8
|
require "faraday"
|
9
9
|
|
10
|
+
# Rails app stuff
|
11
|
+
if defined?(Rails::Railtie)
|
12
|
+
require "whiplash/app/railtie"
|
13
|
+
require "whiplash/app/canonical_host"
|
14
|
+
require "whiplash/app/controller_helpers"
|
15
|
+
end
|
16
|
+
|
10
17
|
module Whiplash
|
11
18
|
class App
|
12
|
-
|
19
|
+
extend Whiplash::App::Signing
|
20
|
+
extend Whiplash::App::ApiConfig
|
13
21
|
include Whiplash::App::Connections
|
14
22
|
include Whiplash::App::FinderMethods
|
15
|
-
extend Whiplash::App::Signing
|
16
23
|
|
17
24
|
attr_accessor :customer_id, :shop_id, :token
|
18
25
|
|
@@ -23,16 +30,16 @@ module Whiplash
|
|
23
30
|
@api_version = options[:api_version] || 2 # can be 2_1
|
24
31
|
end
|
25
32
|
|
26
|
-
def client
|
27
|
-
OAuth2::Client.new(ENV["WHIPLASH_CLIENT_ID"], ENV["WHIPLASH_CLIENT_SECRET"], site: api_url)
|
28
|
-
end
|
29
|
-
|
30
33
|
def versioned_api_url
|
31
34
|
"api/v#{@api_version}"
|
32
35
|
end
|
33
36
|
|
37
|
+
def client
|
38
|
+
OAuth2::Client.new(ENV["WHIPLASH_CLIENT_ID"], ENV["WHIPLASH_CLIENT_SECRET"], site: self.class.api_url)
|
39
|
+
end
|
40
|
+
|
34
41
|
def connection
|
35
|
-
Faraday.new [api_url, versioned_api_url].join("/") do |conn|
|
42
|
+
Faraday.new [self.class.api_url, versioned_api_url].join("/") do |conn|
|
36
43
|
conn.request :authorization, 'Bearer', token.token
|
37
44
|
conn.request :json
|
38
45
|
conn.response :json, :content_type => /\bjson$/
|
@@ -47,9 +54,9 @@ module Whiplash
|
|
47
54
|
case ENV["WHIPLASH_CLIENT_SCOPE"]
|
48
55
|
when /app_(manage|read)/
|
49
56
|
begin
|
50
|
-
access_token =
|
57
|
+
access_token = self.class.client_credentials_token
|
51
58
|
rescue URI::InvalidURIError => e
|
52
|
-
raise StandardError, "The
|
59
|
+
raise StandardError, "The provided URL (#{ENV["WHIPLASH_API_URL"]}) is not valid"
|
53
60
|
end
|
54
61
|
else
|
55
62
|
raise StandardError, "You must request an access token before you can refresh it" if token.nil?
|
@@ -64,6 +71,13 @@ module Whiplash
|
|
64
71
|
false
|
65
72
|
end
|
66
73
|
|
74
|
+
class << self
|
75
|
+
def client_credentials_token
|
76
|
+
client = OAuth2::Client.new(ENV["WHIPLASH_CLIENT_ID"], ENV["WHIPLASH_CLIENT_SECRET"], site: api_url)
|
77
|
+
client.client_credentials.get_token(scope: ENV["WHIPLASH_CLIENT_SCOPE"])
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
67
81
|
private
|
68
82
|
def format_token(oauth_token)
|
69
83
|
return oauth_token if oauth_token.is_a?(OAuth2::AccessToken)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: whiplash-app
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Don Sullivan, Mark Dickson
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oauth2
|
@@ -115,8 +115,11 @@ files:
|
|
115
115
|
- lib/errors/whiplash_api_error.rb
|
116
116
|
- lib/whiplash/app.rb
|
117
117
|
- lib/whiplash/app/api_config.rb
|
118
|
+
- lib/whiplash/app/canonical_host.rb
|
118
119
|
- lib/whiplash/app/connections.rb
|
120
|
+
- lib/whiplash/app/controller_helpers.rb
|
119
121
|
- lib/whiplash/app/finder_methods.rb
|
122
|
+
- lib/whiplash/app/railtie.rb
|
120
123
|
- lib/whiplash/app/signing.rb
|
121
124
|
- lib/whiplash/app/version.rb
|
122
125
|
- whiplash-app.gemspec
|