Class: Otto::Privacy::RedactedFingerprint

Inherits:
Object
  • Object
show all
Defined in:
lib/otto/privacy/redacted_fingerprint.rb

Overview

Immutable privacy-safe request fingerprint (aka CrappyFingerprint)

Contains anonymized information about a request that can be used for logging, analytics, and session tracking without storing personally identifiable information.

Examples:

Create from Rack environment

config = Otto::Privacy::Config.new
fingerprint = RedactedFingerprint.new(env, config)
fingerprint.masked_ip   # => '192.168.1.0'
fingerprint.country     # => 'US'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, config) ⇒ RedactedFingerprint

Create a new RedactedFingerprint from a Rack environment

Parameters:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 32

def initialize(env, config)
  remote_ip = env['REMOTE_ADDR']

  @session_id = SecureRandom.uuid
  @timestamp = Time.now.utc
  @masked_ip = IPPrivacy.mask_ip(remote_ip, config.octet_precision)
  @hashed_ip = IPPrivacy.hash_ip(remote_ip, config.rotation_key)
  @country = config.geo_enabled ? GeoResolver.resolve(remote_ip, env) : nil
  @anonymized_ua = anonymize_user_agent(env['HTTP_USER_AGENT'])
  @request_path = env['PATH_INFO']
  @request_method = env['REQUEST_METHOD']
  @referer = anonymize_referer(env['HTTP_REFERER'])

  freeze
end

Instance Attribute Details

#anonymized_uaObject (readonly)

Returns the value of attribute anonymized_ua.



24
25
26
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 24

def anonymized_ua
  @anonymized_ua
end

#countryObject (readonly)

Returns the value of attribute country.



24
25
26
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 24

def country
  @country
end

#hashed_ipObject (readonly)

Returns the value of attribute hashed_ip.



24
25
26
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 24

def hashed_ip
  @hashed_ip
end

#masked_ipObject (readonly)

Returns the value of attribute masked_ip.



24
25
26
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 24

def masked_ip
  @masked_ip
end

#refererObject (readonly)

Returns the value of attribute referer.



24
25
26
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 24

def referer
  @referer
end

#request_methodObject (readonly)

Returns the value of attribute request_method.



24
25
26
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 24

def request_method
  @request_method
end

#request_pathObject (readonly)

Returns the value of attribute request_path.



24
25
26
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 24

def request_path
  @request_path
end

#session_idObject (readonly)

Returns the value of attribute session_id.



24
25
26
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 24

def session_id
  @session_id
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



24
25
26
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 24

def timestamp
  @timestamp
end

Instance Method Details

#inspectString

Inspect representation

Returns:

  • (String)

    Detailed representation for debugging



83
84
85
86
87
88
89
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 83

def inspect
  '#<Otto::Privacy::RedactedFingerprint ' \
    "masked_ip=#{@masked_ip.inspect} " \
    "hashed_ip=#{@hashed_ip[0..15]}... " \
    "country=#{@country.inspect} " \
    "timestamp=#{@timestamp.inspect}>"
end

#to_hHash

Convert to hash for logging or serialization

Returns:

  • (Hash)

    Hash representation of fingerprint



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 51

def to_h
  {
        session_id: @session_id,
         timestamp: @timestamp.iso8601,
         masked_ip: @masked_ip,
         hashed_ip: @hashed_ip,
           country: @country,
     anonymized_ua: @anonymized_ua,
    request_method: @request_method,
      request_path: @request_path,
           referer: @referer,
  }
end

#to_json(*_args) ⇒ String

Convert to JSON string

Returns:

  • (String)

    JSON representation



68
69
70
71
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 68

def to_json(*_args)
  require 'json'
  to_h.to_json
end

#to_sString

String representation

Returns:

  • (String)

    Human-readable representation



76
77
78
# File 'lib/otto/privacy/redacted_fingerprint.rb', line 76

def to_s
  "#<RedactedFingerprint #{@hashed_ip[0..15]}... #{@country} #{@timestamp}>"
end