minfraud 1.0.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +5 -13
  2. data/.github/workflows/test.yml +46 -0
  3. data/.gitignore +2 -0
  4. data/.rubocop.yml +108 -0
  5. data/.travis.yml +19 -3
  6. data/CHANGELOG.md +65 -1
  7. data/CODE_OF_CONDUCT.md +4 -4
  8. data/Gemfile +11 -2
  9. data/LICENSE.txt +2 -1
  10. data/README.dev.md +4 -0
  11. data/README.md +245 -59
  12. data/Rakefile +18 -3
  13. data/bin/console +4 -3
  14. data/lib/maxmind/geoip2/model/city.rb +99 -0
  15. data/lib/maxmind/geoip2/model/country.rb +94 -0
  16. data/lib/maxmind/geoip2/model/insights.rb +38 -0
  17. data/lib/maxmind/geoip2/record/abstract.rb +46 -0
  18. data/lib/maxmind/geoip2/record/city.rb +62 -0
  19. data/lib/maxmind/geoip2/record/continent.rb +61 -0
  20. data/lib/maxmind/geoip2/record/country.rb +78 -0
  21. data/lib/maxmind/geoip2/record/location.rb +97 -0
  22. data/lib/maxmind/geoip2/record/maxmind.rb +41 -0
  23. data/lib/maxmind/geoip2/record/place.rb +52 -0
  24. data/lib/maxmind/geoip2/record/postal.rb +54 -0
  25. data/lib/maxmind/geoip2/record/represented_country.rb +47 -0
  26. data/lib/maxmind/geoip2/record/subdivision.rb +72 -0
  27. data/lib/maxmind/geoip2/record/traits.rb +233 -0
  28. data/lib/minfraud.rb +48 -8
  29. data/lib/minfraud/assessments.rb +118 -49
  30. data/lib/minfraud/components/account.rb +31 -9
  31. data/lib/minfraud/components/addressable.rb +73 -26
  32. data/lib/minfraud/components/base.rb +35 -11
  33. data/lib/minfraud/components/billing.rb +5 -0
  34. data/lib/minfraud/components/credit_card.rb +67 -18
  35. data/lib/minfraud/components/custom_inputs.rb +25 -0
  36. data/lib/minfraud/components/device.rb +51 -10
  37. data/lib/minfraud/components/email.rb +29 -7
  38. data/lib/minfraud/components/event.rb +60 -13
  39. data/lib/minfraud/components/order.rb +60 -22
  40. data/lib/minfraud/components/payment.rb +165 -21
  41. data/lib/minfraud/components/report/transaction.rb +80 -0
  42. data/lib/minfraud/components/shipping.rb +14 -5
  43. data/lib/minfraud/components/shopping_cart.rb +19 -12
  44. data/lib/minfraud/components/shopping_cart_item.rb +42 -13
  45. data/lib/minfraud/enum.rb +22 -8
  46. data/lib/minfraud/error_handler.rb +45 -12
  47. data/lib/minfraud/errors.rb +22 -2
  48. data/lib/minfraud/http_service.rb +22 -8
  49. data/lib/minfraud/http_service/request.rb +19 -18
  50. data/lib/minfraud/http_service/response.rb +49 -12
  51. data/lib/minfraud/model/abstract.rb +20 -0
  52. data/lib/minfraud/model/address.rb +52 -0
  53. data/lib/minfraud/model/billing_address.rb +11 -0
  54. data/lib/minfraud/model/credit_card.rb +75 -0
  55. data/lib/minfraud/model/device.rb +54 -0
  56. data/lib/minfraud/model/disposition.rb +35 -0
  57. data/lib/minfraud/model/email.rb +54 -0
  58. data/lib/minfraud/model/email_domain.rb +24 -0
  59. data/lib/minfraud/model/error.rb +28 -0
  60. data/lib/minfraud/model/factors.rb +24 -0
  61. data/lib/minfraud/model/geoip2_location.rb +25 -0
  62. data/lib/minfraud/model/insights.rb +68 -0
  63. data/lib/minfraud/model/ip_address.rb +82 -0
  64. data/lib/minfraud/model/issuer.rb +49 -0
  65. data/lib/minfraud/model/score.rb +76 -0
  66. data/lib/minfraud/model/score_ip_address.rb +23 -0
  67. data/lib/minfraud/model/shipping_address.rb +30 -0
  68. data/lib/minfraud/model/subscores.rb +178 -0
  69. data/lib/minfraud/model/warning.rb +63 -0
  70. data/lib/minfraud/report.rb +58 -0
  71. data/lib/minfraud/resolver.rb +25 -16
  72. data/lib/minfraud/validates.rb +187 -0
  73. data/lib/minfraud/version.rb +4 -1
  74. data/minfraud.gemspec +23 -18
  75. metadata +123 -48
@@ -0,0 +1,233 @@
1
+ # Copyright (c) 2020 by MaxMind, Inc.
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ # frozen_string_literal: true
22
+
23
+ require 'ipaddr'
24
+ require 'maxmind/geoip2/record/abstract'
25
+
26
+ module MaxMind
27
+ module GeoIP2
28
+ module Record
29
+ # Contains data for the traits record associated with an IP address.
30
+ #
31
+ # This record is returned by all location services and databases.
32
+ class Traits < Abstract
33
+ # @!visibility private
34
+ def initialize(record)
35
+ super(record)
36
+ if !record.key?('network') && record.key?('ip_address') &&
37
+ record.key?('prefix_length')
38
+ ip = IPAddr.new(record['ip_address']).mask(record['prefix_length'])
39
+ # We could use ip.prefix instead of record['prefix_length'], but that
40
+ # method only becomes available in Ruby 2.5+.
41
+ record['network'] = format('%s/%d', ip.to_s, record['prefix_length'])
42
+ end
43
+ end
44
+
45
+ # The autonomous system number associated with the IP address. See
46
+ # Wikipedia[https://en.wikipedia.org/wiki/Autonomous_system_(Internet)].
47
+ # This attribute is only available from the City and Insights web service
48
+ # and the GeoIP2 Enterprise database.
49
+ #
50
+ # @return [Integer, nil]
51
+ def autonomous_system_number
52
+ get('autonomous_system_number')
53
+ end
54
+
55
+ # The organization associated with the registered autonomous system number
56
+ # for the IP address. See
57
+ # Wikipedia[https://en.wikipedia.org/wiki/Autonomous_system_(Internet)].
58
+ # This attribute is only available from the City and Insights web service
59
+ # and the GeoIP2 Enterprise database.
60
+ #
61
+ # @return [String, nil]
62
+ def autonomous_system_organization
63
+ get('autonomous_system_organization')
64
+ end
65
+
66
+ # The connection type may take the following values: "Dialup",
67
+ # "Cable/DSL", "Corporate", "Cellular". Additional values may be added in
68
+ # the future. This attribute is only available in the GeoIP2 Enterprise
69
+ # database.
70
+ #
71
+ # @return [String, nil]
72
+ def connection_type
73
+ get('connection_type')
74
+ end
75
+
76
+ # The second level domain associated with the IP address. This will be
77
+ # something like "example.com" or "example.co.uk", not "foo.example.com".
78
+ # This attribute is only available from the City and Insights web service
79
+ # and the GeoIP2 Enterprise database.
80
+ #
81
+ # @return [String, nil]
82
+ def domain
83
+ get('domain')
84
+ end
85
+
86
+ # The IP address that the data in the model is for. If you performed a "me"
87
+ # lookup against the web service, this will be the externally routable IP
88
+ # address for the system the code is running on. If the system is behind a
89
+ # NAT, this may differ from the IP address locally assigned to it. This
90
+ # attribute is returned by all end points.
91
+ #
92
+ # @return [String]
93
+ def ip_address
94
+ get('ip_address')
95
+ end
96
+
97
+ # This is true if the IP address belongs to any sort of anonymous network.
98
+ # This property is only available from GeoIP2 Precision Insights.
99
+ #
100
+ # @return [Boolean]
101
+ def anonymous?
102
+ get('is_anonymous')
103
+ end
104
+
105
+ # This is true if the IP address is registered to an anonymous VPN
106
+ # provider. If a VPN provider does not register subnets under names
107
+ # associated with them, we will likely only flag their IP ranges using the
108
+ # hosting_provider? property. This property is only available from GeoIP2
109
+ # Precision Insights.
110
+ #
111
+ # @return [Boolean]
112
+ def anonymous_vpn?
113
+ get('is_anonymous_vpn')
114
+ end
115
+
116
+ # This is true if the IP address belongs to a hosting or VPN provider (see
117
+ # description of the anonymous_vpn? property). This property is only
118
+ # available from GeoIP2 Precision Insights.
119
+ #
120
+ # @return [Boolean]
121
+ def hosting_provider?
122
+ get('is_hosting_provider')
123
+ end
124
+
125
+ # This attribute is true if MaxMind believes this IP address to be a
126
+ # legitimate proxy, such as an internal VPN used by a corporation. This
127
+ # attribute is only available in the GeoIP2 Enterprise database.
128
+ #
129
+ # @return [Boolean]
130
+ def legitimate_proxy?
131
+ get('is_legitimate_proxy')
132
+ end
133
+
134
+ # This is true if the IP address belongs to a public proxy. This property
135
+ # is only available from GeoIP2 Precision Insights.
136
+ #
137
+ # @return [Boolean]
138
+ def public_proxy?
139
+ get('is_public_proxy')
140
+ end
141
+
142
+ # This is true if the IP address is on a suspected anonymizing network
143
+ # and belongs to a residential ISP. This property is only available
144
+ # from GeoIP2 Precision Insights.
145
+ #
146
+ # @return [Boolean]
147
+ def residential_proxy?
148
+ get('is_residential_proxy')
149
+ end
150
+
151
+ # This is true if the IP address is a Tor exit node. This property is only
152
+ # available from GeoIP2 Precision Insights.
153
+ #
154
+ # @return [Boolean]
155
+ def tor_exit_node?
156
+ get('is_tor_exit_node')
157
+ end
158
+
159
+ # The name of the ISP associated with the IP address. This attribute is
160
+ # only available from the City and Insights web services and the GeoIP2
161
+ # Enterprise database.
162
+ #
163
+ # @return [String, nil]
164
+ def isp
165
+ get('isp')
166
+ end
167
+
168
+ # The network in CIDR notation associated with the record. In particular,
169
+ # this is the largest network where all of the fields besides ip_address
170
+ # have the same value.
171
+ #
172
+ # @return [String]
173
+ def network
174
+ get('network')
175
+ end
176
+
177
+ # The name of the organization associated with the IP address. This
178
+ # attribute is only available from the City and Insights web services and
179
+ # the GeoIP2 Enterprise database.
180
+ #
181
+ # @return [String, nil]
182
+ def organization
183
+ get('organization')
184
+ end
185
+
186
+ # An indicator of how static or dynamic an IP address is. This property is
187
+ # only available from GeoIP2 Precision Insights.
188
+ #
189
+ # @return [Float, nil]
190
+ def static_ip_score
191
+ get('static_ip_score')
192
+ end
193
+
194
+ # The estimated number of users sharing the IP/network during the past 24
195
+ # hours. For IPv4, the count is for the individual IP. For IPv6, the count
196
+ # is for the /64 network. This property is only available from GeoIP2
197
+ # Precision Insights.
198
+ #
199
+ # @return [Integer, nil]
200
+ def user_count
201
+ get('user_count')
202
+ end
203
+
204
+ # The user type associated with the IP address. This can be one of the
205
+ # following values:
206
+ #
207
+ # * business
208
+ # * cafe
209
+ # * cellular
210
+ # * college
211
+ # * content_delivery_network
212
+ # * dialup
213
+ # * government
214
+ # * hosting
215
+ # * library
216
+ # * military
217
+ # * residential
218
+ # * router
219
+ # * school
220
+ # * search_engine_spider
221
+ # * traveler
222
+ #
223
+ # This attribute is only available from the Insights web service and the
224
+ # GeoIP2 Enterprise database.
225
+ #
226
+ # @return [String, nil]
227
+ def user_type
228
+ get('user_type')
229
+ end
230
+ end
231
+ end
232
+ end
233
+ end
@@ -1,19 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
1
4
  require 'minfraud'
2
5
  require 'minfraud/enum'
6
+ require 'minfraud/validates'
3
7
  require 'minfraud/components/base'
4
8
  require 'minfraud/components/account'
5
9
  require 'minfraud/components/addressable'
6
10
  require 'minfraud/components/billing'
7
11
  require 'minfraud/components/credit_card'
12
+ require 'minfraud/components/custom_inputs'
8
13
  require 'minfraud/components/device'
9
14
  require 'minfraud/components/email'
10
15
  require 'minfraud/components/event'
11
16
  require 'minfraud/components/order'
12
17
  require 'minfraud/components/payment'
13
- require 'minfraud/components/shopping_cart_item'
18
+ require 'minfraud/components/report/transaction'
14
19
  require 'minfraud/components/shipping'
15
20
  require 'minfraud/components/shopping_cart'
16
- require 'minfraud/components/device'
21
+ require 'minfraud/components/shopping_cart_item'
17
22
  require 'minfraud/resolver'
18
23
  require 'minfraud/version'
19
24
  require 'minfraud/errors'
@@ -22,23 +27,58 @@ require 'minfraud/http_service/request'
22
27
  require 'minfraud/http_service/response'
23
28
  require 'minfraud/error_handler'
24
29
  require 'minfraud/assessments'
30
+ require 'minfraud/report'
25
31
 
32
+ # This class holds global configuration parameters and provides a namespace
33
+ # for the gem's classes.
26
34
  module Minfraud
27
35
  class << self
28
- # @!attribute user_id
29
- # @return [String] MaxMind username that is used for authorization
36
+ # The MaxMind account ID that is used for authorization.
37
+ #
38
+ # @return [Integer, nil]
39
+ attr_accessor :account_id
40
+
41
+ # Enable client side validation. This is disabled by default.
42
+ #
43
+ # @return [Boolean, nil]
44
+ attr_accessor :enable_validation
45
+
46
+ # The host to use when connecting to the web service.
47
+ #
48
+ # @return [String, nil]
49
+ attr_accessor :host
50
+
51
+ # The MaxMind account ID that is used for authorization.
52
+ #
53
+ # @deprecated Use {::account_id} instead. This will be removed in the next
54
+ # major version.
55
+ #
56
+ # @return [Integer, nil]
30
57
  attr_accessor :user_id
31
58
 
32
- # @!attribute license_key
33
- # @return [String] MaxMind license key that is used for authorization
59
+ # The MaxMind license key that is used for authorization.
60
+ #
61
+ # @return [String, nil]
34
62
  attr_accessor :license_key
35
63
 
36
- # @yield [self] to accept configuration settings
64
+ # @!visibility private
65
+ attr_reader :connection
66
+
67
+ # Yield self to accept configuration settings.
68
+ #
69
+ # @yield [self]
37
70
  def configure
38
71
  yield self
72
+
73
+ config = Minfraud::HTTPService.configuration
74
+ @connection = Faraday.new(config[:server], {}, &config[:middleware])
39
75
  end
40
76
 
41
- # @return [Hash] current Minfraud configuration
77
+ # The current Minfraud configuration.
78
+ #
79
+ # @deprecated This will be removed in the next major version.
80
+ #
81
+ # @return [Hash]
42
82
  def configuration
43
83
  {
44
84
  user_id: @user_id,
@@ -1,90 +1,159 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Minfraud
4
+ # Assessments is used to perform minFraud Score, Insights, and Factors
5
+ # requests.
6
+ #
7
+ # @see https://dev.maxmind.com/minfraud/
2
8
  class Assessments
3
9
  include ::Minfraud::HTTPService
4
10
  include ::Minfraud::Resolver
5
11
 
6
- # @attribute account
7
- # @return [Minfraud::Components::Account] Account component
12
+ # The Account component.
13
+ #
14
+ # @return [Minfraud::Components::Account, nil]
8
15
  attr_accessor :account
9
16
 
10
- # @attribute billing
11
- # @return [Minfraud::Components::Billing] Billing component
17
+ # The Billing component.
18
+ #
19
+ # @return [Minfraud::Components::Billing, nil]
12
20
  attr_accessor :billing
13
21
 
14
- # @attribute credit_card
15
- # @return [Minfraud::Components::CreditCard] CreditCard component
22
+ # The CreditCard component.
23
+ #
24
+ # @return [Minfraud::Components::CreditCard, nil]
16
25
  attr_accessor :credit_card
17
26
 
18
- # @attribute device
19
- # @return [Minfraud::Components::Device] Device component
27
+ # The CustomInputs component.
28
+ #
29
+ # @return [Minfraud::Components::CustomInputs, nil]
30
+ attr_accessor :custom_inputs
31
+
32
+ # The Device component.
33
+ #
34
+ # @return [Minfraud::Components::Device, nil]
20
35
  attr_accessor :device
21
36
 
22
- # @attribute email
23
- # @return [Minfraud::Components::Email] Email component
37
+ # The Email component.
38
+ #
39
+ # @return [Minfraud::Components::Email, nil]
24
40
  attr_accessor :email
25
41
 
26
- # @attribute event
27
- # @return [Minfraud::Components::Event] Event component
42
+ # The Event component.
43
+ #
44
+ # @return [Minfraud::Components::Event, nil]
28
45
  attr_accessor :event
29
46
 
30
- # @attribute order
31
- # @return [Minfraud::Components::Order] Order component
47
+ # The Order component.
48
+ #
49
+ # @return [Minfraud::Components::Order, nil]
32
50
  attr_accessor :order
33
51
 
34
- # @attribute payment
35
- # @return [Minfraud::Components::Payment] Payment component
52
+ # The Payment component.
53
+ #
54
+ # @return [Minfraud::Components::Payment, nil]
36
55
  attr_accessor :payment
37
56
 
38
- # @!attribute shipping
39
- # @return [Minfraud::Components::Shipping] Shipping component
57
+ # The Shipping component.
58
+ #
59
+ # @return [Minfraud::Components::Shipping, nil]
40
60
  attr_accessor :shipping
41
61
 
42
- # @!attribute shopping_cart
43
- # @return [Minfraud::Components::ShoppingCarat] ShoppingCart component
62
+ # The ShoppingCart component.
63
+ #
64
+ # @return [Minfraud::Components::ShoppingCart, nil]
44
65
  attr_accessor :shopping_cart
45
66
 
46
- # @param [Hash] params hash of parameters
47
- # @param [Minfraud::Resolver] resolver resolver that maps params to components
48
- # @note In case when params is a Hash of components it just assigns them to the corresponding instance variables
49
- # @return [Minfraud::Assessments] Assessments instance
67
+ # @param params [Hash] Hash of parameters. Each key is a symbol
68
+ # corresponding to one of the available component attributes. Values may
69
+ # be component objects or hashes that will be provided to the component
70
+ # constructors.
71
+ #
72
+ # @param resolver [Minfraud::Resolver] Resolver that maps parameters to
73
+ # components.
50
74
  def initialize(params = {}, resolver = ::Minfraud::Resolver)
75
+ @locales = params.delete('locales')
76
+ @locales = ['en'] if @locales.nil?
77
+
51
78
  resolver.assign(self, params)
52
79
  end
53
80
 
54
- # @!macro [attach] define
55
- # @method $1
56
- # Makes a request to minFraud $1 endpoint.
57
- # Raises an error in case of invalid response
58
- # @return [Minfraud::HTTPService::Response] Wrapped minFraud response
59
- def self.define(endpoint)
60
- define_method(endpoint) do
61
- raw = request.perform(verb: :post, endpoint: endpoint.to_s, body: request_body)
62
- response = ::Minfraud::HTTPService::Response.new(
63
- status: raw.status.to_i,
64
- body: raw.body,
65
- headers: raw.headers
66
- )
67
-
68
- ::Minfraud::ErrorHandler.inspect(response)
69
- end
81
+ # Perform a minFraud Factors request.
82
+ #
83
+ # @return [Minfraud::HTTPService::Response]
84
+ #
85
+ # @raise [Minfraud::AuthorizationError] If there was an authentication
86
+ # problem.
87
+ #
88
+ # @raise [Minfraud::ClientError] If there was a critical problem with one
89
+ # of your inputs.
90
+ #
91
+ # @raise [Minfraud::ServerError] If the server reported an error of some
92
+ # kind.
93
+ def factors
94
+ perform_request(:factors)
70
95
  end
71
96
 
72
- define :score
73
- define :insights
74
- define :factors
97
+ # Perform a minFraud Insights request.
98
+ #
99
+ # @return [Minfraud::HTTPService::Response]
100
+ #
101
+ # @raise [Minfraud::AuthorizationError] If there was an authentication
102
+ # problem.
103
+ #
104
+ # @raise [Minfraud::ClientError] If there was a critical problem with one
105
+ # of your inputs.
106
+ #
107
+ # @raise [Minfraud::ServerError] If the server reported an error of some
108
+ # kind.
109
+ def insights
110
+ perform_request(:insights)
111
+ end
112
+
113
+ # Perform a minFraud Score request.
114
+ #
115
+ # @return [Minfraud::HTTPService::Response]
116
+ #
117
+ # @raise [Minfraud::AuthorizationError] If there was an authentication
118
+ # problem.
119
+ #
120
+ # @raise [Minfraud::ClientError] If there was a critical problem with one
121
+ # of your inputs.
122
+ #
123
+ # @raise [Minfraud::ServerError] If the server reported an error of some
124
+ # kind.
125
+ def score
126
+ perform_request(:score)
127
+ end
75
128
 
76
129
  private
77
- # Creates a unified request body from components converted to JSON
78
- # @return [Hash] Request body
130
+
131
+ def perform_request(endpoint)
132
+ raw = request.perform(
133
+ verb: :post,
134
+ endpoint: endpoint.to_s,
135
+ body: request_body,
136
+ )
137
+
138
+ response = ::Minfraud::HTTPService::Response.new(
139
+ endpoint: endpoint,
140
+ locales: @locales,
141
+ status: raw.status.to_i,
142
+ body: raw.body,
143
+ headers: raw.headers
144
+ )
145
+
146
+ ::Minfraud::ErrorHandler.examine(response)
147
+ end
148
+
79
149
  def request_body
80
- MAPPING.keys.inject({}) do |mem, e|
81
- next mem unless value = send(e)
150
+ MAPPING.keys.reduce({}) do |mem, e|
151
+ next mem unless (value = send(e))
152
+
82
153
  mem.merge!(e.to_s => value.to_json)
83
154
  end
84
155
  end
85
156
 
86
- # Creates memoized Minfraud::HTTPService::Request instance
87
- # @return [Minfraud::HTTPService::Request] Request instance based on configuration params
88
157
  def request
89
158
  @request ||= Request.new(::Minfraud::HTTPService.configuration)
90
159
  end