fingerly 0.1.1 → 0.3.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.
@@ -5,8 +5,38 @@ require "time"
5
5
  module Fingerly
6
6
  # One signal group that contributed to an event's suspect score.
7
7
  class EventTrigger
8
- # @return [String, nil] the signal group, such as +vpn+ or +bot+
8
+ SIGNAL_ATTESTATION_FAILED = "attestation_failed"
9
+ SIGNAL_ATTESTATION_MISSING = "attestation_missing"
10
+ SIGNAL_ATTESTATION_DOWNGRADE = "attestation_downgrade"
11
+ SIGNAL_DEVICE_INCONSISTENT = "device_inconsistent"
12
+ SIGNAL_REPORT_INCOMPLETE = "report_incomplete"
13
+ SIGNAL_PROBE_ERRORS = "probe_errors"
14
+ SIGNAL_FETCH_METADATA_MISSING = "fetch_metadata_missing"
15
+ SIGNAL_WEB_TICKET_MISSING = "web_ticket_missing"
16
+ SIGNAL_WEB_TICKET_INVALID = "web_ticket_invalid"
17
+ SIGNAL_TAMPERING = "tampering"
18
+ SIGNAL_BROWSER_TAMPERING = "browser_tampering"
19
+ SIGNAL_CUSTOM_ROOT_CA = "custom_root_ca"
20
+ SIGNAL_SECONDARY_USER_PROFILE = "secondary_user_profile"
21
+
22
+ V3_SIGNALS = [
23
+ SIGNAL_ATTESTATION_FAILED,
24
+ SIGNAL_ATTESTATION_DOWNGRADE,
25
+ SIGNAL_DEVICE_INCONSISTENT,
26
+ SIGNAL_REPORT_INCOMPLETE,
27
+ SIGNAL_PROBE_ERRORS,
28
+ SIGNAL_FETCH_METADATA_MISSING,
29
+ SIGNAL_WEB_TICKET_MISSING,
30
+ SIGNAL_WEB_TICKET_INVALID,
31
+ SIGNAL_TAMPERING,
32
+ SIGNAL_CUSTOM_ROOT_CA,
33
+ SIGNAL_SECONDARY_USER_PROFILE
34
+ ].freeze
35
+
36
+ # @return [String, nil] the signal or signal group, such as +vpn+ or +attestation_failed+
9
37
  attr_reader :signal
38
+ # @return [String, nil] optional group identifier when returned alongside a raw signal
39
+ attr_reader :group
10
40
  # @return [Integer] the group's combined weight in this score
11
41
  attr_reader :weight
12
42
  # @return [String, nil] +low+, +medium+ or +high+
@@ -15,6 +45,7 @@ module Fingerly
15
45
  # @api private
16
46
  def initialize(payload)
17
47
  @signal = payload["signal"]
48
+ @group = payload["group"]
18
49
  @weight = payload["weight"] || 0
19
50
  @confidence = payload["confidence"]
20
51
  freeze
@@ -33,12 +64,15 @@ module Fingerly
33
64
  # the distinction at the call site.
34
65
  class Event
35
66
  # Fields copied straight from the response, in the order the API documents
36
- # them. Anything not listed stays reachable through {#raw}.
67
+ # them. Anything not listed stays reachable through {#raw}. +visitor_id+ is
68
+ # +""+ when the submission was unidentifiable, +reason+ explains why
69
+ # enrichment did not happen when unavailable, and +project_id+ is the UUID
70
+ # string of the Fingerly project the event belongs to (nil on older servers).
37
71
  STRING_FIELDS = %w[
38
- request_id visitor_id state environment platform sdk_platform sdk_version
39
- origin url_host url_path tag ip_address country_code country_name asn
40
- asn_name anonymity_network agent agent_version os device_kind visitor_kind
41
- suspect_level
72
+ request_id project_id visitor_id state reason environment platform sdk_platform
73
+ sdk_version origin url_host url_path tag ip_network country_code
74
+ country_name asn asn_name anonymity_network agent agent_version os
75
+ device_kind visitor_kind suspect_level
42
76
  ].freeze
43
77
 
44
78
  STRING_FIELDS.each { |name| attr_reader name.to_sym }
@@ -50,25 +84,31 @@ module Fingerly
50
84
  # @return [Array<EventTrigger>] the signal groups behind the score; never
51
85
  # nil, and empty when nothing fired
52
86
  attr_reader :triggers
53
- # @return [Hash, nil] the archived submission, when one was kept. Its
54
- # contents are deliberately opaque, not a public contract.
87
+ # @return [Hash, nil] an allow-listed view of the archived request, when
88
+ # one was kept: schema, request id, time, SDK, network, IP
89
+ # classification and city-level location, risk, attestation and
90
+ # velocity. It holds no raw signals, user agent or address, and its
91
+ # contents are not a public contract.
55
92
  attr_reader :document
56
- # @return [Hash, nil] the deferred report attached to this request, when
57
- # one arrived. Also opaque.
58
- attr_reader :deferred_signals
59
93
  # @return [Hash] the response body exactly as it arrived, so a field the
60
94
  # API adds after this SDK was released needs no upgrade
61
95
  attr_reader :raw
62
96
 
97
+ # @param lenient_time [Boolean] keeps a missing or malformed
98
+ # +occurred_at+ as nil rather than raising, for +events.verify+, which
99
+ # answers +too_old+ for it
63
100
  # @api private
64
- def initialize(payload)
101
+ def initialize(payload, lenient_time: false)
65
102
  STRING_FIELDS.each { |name| instance_variable_set(:"@#{name}", payload[name]) }
66
- @occurred_at = Models.parse_instant(payload["occurred_at"])
103
+ @occurred_at = if lenient_time
104
+ Models.rfc3339(payload["occurred_at"])
105
+ else
106
+ Models.parse_instant(payload["occurred_at"])
107
+ end
67
108
  @suspect_score = payload["suspect_score"]
68
109
  @has_document = payload["has_document"] ? true : false
69
110
  @triggers = Array(payload["triggers"]).map { |item| EventTrigger.new(item) }.freeze
70
111
  @document = payload["document"]
71
- @deferred_signals = payload["deferred_signals"]
72
112
  @raw = payload
73
113
  freeze
74
114
  end
@@ -124,6 +164,129 @@ module Fingerly
124
164
  end
125
165
  end
126
166
 
167
+ # The first consumption of an event, from +fingerly.events.consume+.
168
+ class EventConsumption
169
+ # @return [String, nil] the consumed event's request id
170
+ attr_reader :request_id
171
+ # @return [String, nil] when it was consumed, as the API sent it (RFC 3339)
172
+ attr_reader :consumed_at
173
+ # @return [Boolean] true for the call that consumed the event
174
+ attr_reader :first
175
+ # @return [Hash] the response body exactly as it arrived
176
+ attr_reader :raw
177
+
178
+ # @api private
179
+ def initialize(payload)
180
+ @request_id = payload["request_id"].is_a?(String) ? payload["request_id"] : nil
181
+ @consumed_at = payload["consumed_at"].is_a?(String) ? payload["consumed_at"] : nil
182
+ @first = payload["first"] == true
183
+ @raw = payload
184
+ freeze
185
+ end
186
+
187
+ def first? = first
188
+
189
+ def inspect
190
+ "#<Fingerly::EventConsumption request_id=#{request_id.inspect} first=#{first}>"
191
+ end
192
+ end
193
+
194
+ # The answer of +fingerly.events.verify+.
195
+ #
196
+ # {#reason} is +""+ when {#ok?}, and otherwise the first check that failed,
197
+ # as one of the wire strings below. The score is not checked: apply your own
198
+ # policy to {#event} after {#ok?}.
199
+ class EventVerification
200
+ NOT_FOUND = "not_found"
201
+ TOO_OLD = "too_old"
202
+ TAG_MISMATCH = "tag_mismatch"
203
+ UNIDENTIFIABLE = "unidentifiable"
204
+ UNSCORED = "unscored"
205
+ ALREADY_CONSUMED = "already_consumed"
206
+
207
+ # Every reason, in the order the checks run.
208
+ REASONS = [NOT_FOUND, TOO_OLD, TAG_MISMATCH, UNIDENTIFIABLE, UNSCORED, ALREADY_CONSUMED].freeze
209
+
210
+ # @return [Boolean] whether every check passed
211
+ attr_reader :ok
212
+ # @return [String] the failed check, or +""+ when {#ok}
213
+ attr_reader :reason
214
+ # @return [Event, nil] the event read; nil only for +not_found+
215
+ attr_reader :event
216
+
217
+ # @api private
218
+ def initialize(reason:, event:)
219
+ @ok = reason.empty?
220
+ @reason = reason
221
+ @event = event
222
+ freeze
223
+ end
224
+
225
+ def ok? = ok
226
+
227
+ def inspect
228
+ "#<Fingerly::EventVerification ok=#{ok} reason=#{reason.inspect}>"
229
+ end
230
+ end
231
+
232
+ # What Apple holds for the device behind an event, from
233
+ # +fingerly.device_check.query+. The bits mean what you decide; Fingerly
234
+ # never reads them.
235
+ class DeviceCheckBits
236
+ # @return [Boolean] false when Apple holds no bits for the device yet;
237
+ # both bits are then false
238
+ attr_reader :found
239
+ # @return [Boolean] the first bit
240
+ attr_reader :bit0
241
+ # @return [Boolean] the second bit
242
+ attr_reader :bit1
243
+ # @return [String, nil] when the bits last changed, as +YYYY-MM+; nil when
244
+ # Apple holds no bits
245
+ attr_reader :last_update
246
+ # @return [Hash] the response body exactly as it arrived
247
+ attr_reader :raw
248
+
249
+ # @api private
250
+ def initialize(payload)
251
+ @found = payload["found"] == true
252
+ @bit0 = payload["bit0"] == true
253
+ @bit1 = payload["bit1"] == true
254
+ @last_update = payload["last_update"].is_a?(String) ? payload["last_update"] : nil
255
+ @raw = payload
256
+ freeze
257
+ end
258
+
259
+ def found? = found
260
+
261
+ def inspect
262
+ "#<Fingerly::DeviceCheckBits found=#{found} bit0=#{bit0} bit1=#{bit1}>"
263
+ end
264
+ end
265
+
266
+ # The bits Apple accepted, from +fingerly.device_check.write+.
267
+ class DeviceCheckWrite
268
+ # @return [Boolean, nil] the value set for the first bit; nil when the
269
+ # write left it out
270
+ attr_reader :bit0
271
+ # @return [Boolean, nil] the value set for the second bit; nil when the
272
+ # write left it out
273
+ attr_reader :bit1
274
+ # @return [Hash] the response body exactly as it arrived
275
+ attr_reader :raw
276
+
277
+ # @api private
278
+ def initialize(payload)
279
+ @bit0 = [true, false].include?(payload["bit0"]) ? payload["bit0"] : nil
280
+ @bit1 = [true, false].include?(payload["bit1"]) ? payload["bit1"] : nil
281
+ @raw = payload
282
+ freeze
283
+ end
284
+
285
+ def inspect
286
+ "#<Fingerly::DeviceCheckWrite bit0=#{bit0.inspect} bit1=#{bit1.inspect}>"
287
+ end
288
+ end
289
+
127
290
  # The envelope every webhook delivery shares.
128
291
  #
129
292
  # {#data} is left as a plain Hash, so a new event type needs no SDK upgrade.
@@ -178,5 +341,17 @@ module Fingerly
178
341
  rescue ArgumentError, TypeError
179
342
  raise Error, "Fingerly API returned a timestamp that is not RFC 3339"
180
343
  end
344
+
345
+ RFC3339 = /\A\d{4}-\d{2}-\d{2}[Tt]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:[Zz]|[+-]\d{2}:\d{2})\z/
346
+
347
+ # A strict RFC 3339 date-time, or nil for anything else, a date alone
348
+ # included.
349
+ def rfc3339(value)
350
+ return nil unless value.is_a?(String) && value.match?(RFC3339)
351
+
352
+ Time.iso8601(value.upcase)
353
+ rescue ArgumentError
354
+ nil
355
+ end
181
356
  end
182
357
  end