castle-rb 6.0.1 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -8
  3. data/lib/castle.rb +7 -11
  4. data/lib/castle/api.rb +7 -12
  5. data/lib/castle/api/approve_device.rb +1 -6
  6. data/lib/castle/api/authenticate.rb +10 -7
  7. data/lib/castle/api/end_impersonation.rb +3 -8
  8. data/lib/castle/api/filter.rb +37 -0
  9. data/lib/castle/api/get_device.rb +1 -6
  10. data/lib/castle/api/get_devices_for_user.rb +1 -6
  11. data/lib/castle/api/log.rb +37 -0
  12. data/lib/castle/api/report_device.rb +1 -6
  13. data/lib/castle/api/risk.rb +37 -0
  14. data/lib/castle/api/start_impersonation.rb +3 -8
  15. data/lib/castle/api/track.rb +1 -6
  16. data/lib/castle/client.rb +36 -16
  17. data/lib/castle/commands/approve_device.rb +1 -5
  18. data/lib/castle/commands/end_impersonation.rb +1 -1
  19. data/lib/castle/commands/filter.rb +23 -0
  20. data/lib/castle/commands/get_device.rb +1 -5
  21. data/lib/castle/commands/get_devices_for_user.rb +1 -5
  22. data/lib/castle/commands/{identify.rb → log.rb} +4 -3
  23. data/lib/castle/commands/report_device.rb +1 -5
  24. data/lib/castle/commands/risk.rb +23 -0
  25. data/lib/castle/commands/start_impersonation.rb +1 -1
  26. data/lib/castle/configuration.rb +18 -8
  27. data/lib/castle/core/get_connection.rb +3 -1
  28. data/lib/castle/core/process_response.rb +5 -2
  29. data/lib/castle/core/process_webhook.rb +10 -5
  30. data/lib/castle/core/send_request.rb +8 -16
  31. data/lib/castle/errors.rb +37 -13
  32. data/lib/castle/failover/prepare_response.rb +2 -7
  33. data/lib/castle/failover/strategy.rb +3 -0
  34. data/lib/castle/headers/extract.rb +4 -4
  35. data/lib/castle/headers/filter.rb +9 -6
  36. data/lib/castle/ips/extract.rb +4 -2
  37. data/lib/castle/logger.rb +3 -3
  38. data/lib/castle/payload/prepare.rb +3 -4
  39. data/lib/castle/secure_mode.rb +3 -2
  40. data/lib/castle/support/hanami.rb +2 -6
  41. data/lib/castle/support/rails.rb +1 -3
  42. data/lib/castle/utils/clean_invalid_chars.rb +1 -3
  43. data/lib/castle/verdict.rb +2 -0
  44. data/lib/castle/version.rb +1 -1
  45. data/lib/castle/webhooks/verify.rb +9 -7
  46. data/spec/integration/rails/rails_spec.rb +9 -7
  47. data/spec/integration/rails/support/home_controller.rb +26 -24
  48. data/spec/lib/castle/api/approve_device_spec.rb +3 -3
  49. data/spec/lib/castle/api/authenticate_spec.rb +20 -24
  50. data/spec/lib/castle/api/end_impersonation_spec.rb +11 -5
  51. data/spec/lib/castle/api/filter_spec.rb +5 -0
  52. data/spec/lib/castle/api/get_device_spec.rb +3 -3
  53. data/spec/lib/castle/api/get_devices_for_user_spec.rb +3 -3
  54. data/spec/lib/castle/api/log_spec.rb +5 -0
  55. data/spec/lib/castle/api/report_device_spec.rb +3 -3
  56. data/spec/lib/castle/api/risk_spec.rb +5 -0
  57. data/spec/lib/castle/api/start_impersonation_spec.rb +11 -5
  58. data/spec/lib/castle/api/track_spec.rb +11 -7
  59. data/spec/lib/castle/api_spec.rb +4 -20
  60. data/spec/lib/castle/client_id/extract_spec.rb +4 -13
  61. data/spec/lib/castle/client_spec.rb +81 -84
  62. data/spec/lib/castle/commands/authenticate_spec.rb +8 -15
  63. data/spec/lib/castle/commands/end_impersonation_spec.rb +6 -9
  64. data/spec/lib/castle/commands/{identify_spec.rb → filter_spec.rb} +41 -19
  65. data/spec/lib/castle/commands/log_spec.rb +100 -0
  66. data/spec/lib/castle/commands/risk_spec.rb +100 -0
  67. data/spec/lib/castle/commands/start_impersonation_spec.rb +6 -9
  68. data/spec/lib/castle/commands/track_spec.rb +9 -18
  69. data/spec/lib/castle/configuration_spec.rb +2 -6
  70. data/spec/lib/castle/context/get_default_spec.rb +8 -8
  71. data/spec/lib/castle/context/prepare_spec.rb +6 -7
  72. data/spec/lib/castle/core/get_connection_spec.rb +6 -22
  73. data/spec/lib/castle/core/process_response_spec.rb +1 -8
  74. data/spec/lib/castle/core/send_request_spec.rb +4 -29
  75. data/spec/lib/castle/headers/extract_spec.rb +1 -3
  76. data/spec/lib/castle/headers/filter_spec.rb +12 -11
  77. data/spec/lib/castle/ips/extract_spec.rb +4 -13
  78. data/spec/lib/castle/logger_spec.rb +2 -6
  79. data/spec/lib/castle/payload/prepare_spec.rb +5 -4
  80. data/spec/lib/castle/session_spec.rb +13 -36
  81. data/spec/lib/castle/singleton_configuration_spec.rb +2 -6
  82. data/spec/lib/castle/utils/clean_invalid_chars_spec.rb +2 -2
  83. data/spec/lib/castle/utils/merge_spec.rb +3 -1
  84. data/spec/lib/castle/validators/present_spec.rb +5 -6
  85. data/spec/lib/castle/webhooks/verify_spec.rb +8 -24
  86. data/spec/lib/castle_spec.rb +4 -10
  87. data/spec/spec_helper.rb +1 -3
  88. data/spec/support/shared_examples/action_request.rb +152 -0
  89. data/spec/support/shared_examples/configuration.rb +14 -42
  90. metadata +23 -18
  91. data/lib/castle/api/identify.rb +0 -26
  92. data/lib/castle/api/review.rb +0 -24
  93. data/lib/castle/commands/review.rb +0 -17
  94. data/lib/castle/events.rb +0 -49
  95. data/spec/lib/castle/api/identify_spec.rb +0 -68
  96. data/spec/lib/castle/api/review_spec.rb +0 -19
  97. data/spec/lib/castle/commands/review_spec.rb +0 -24
  98. data/spec/lib/castle/events_spec.rb +0 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 538dcb5ae34bd3c453ccc2627f351bacb167d46bccb6698dbc90a6c71179a824
4
- data.tar.gz: 87e1156703a21a88d9a1bb4990303720ab7577aedb190b580672f6d7456066d9
3
+ metadata.gz: ab35253e5571890b82e8fd555d8342c1c9713d35d3cf83354c19cccc030c6683
4
+ data.tar.gz: 9ead0508cede4ddccaa4e58ad8c60e0a76b2f6496279eaccec99419a172af326
5
5
  SHA512:
6
- metadata.gz: c126df845b56433ad362acea73516b8022757e546a504d2cd5ff79a3c5fea4d20cffe8dd7577cc224edaeca72b8d1f0d1da88c9996fff2cee1f9e9180b26a95d
7
- data.tar.gz: 4c3990829af576a0a9162018007446aeb29d7483526f1fd358ac798e5c8b382ba091dc75983da2e956f829c66e1b0311bfe0cd81db2f0ae6a2e892d641b14194
6
+ metadata.gz: 33f0642e24430268723c3c960b8a214646b76c3693d088b3fc2d37c4fc9ccd41f4a3b4e295ab73b8e7530184d8b35f83b38918d1b588050b33238cdbbd9e64e0
7
+ data.tar.gz: 4518be516263d115b2e55e2ca058aeb5a727dbb1e82343b52bec9a93a3b589457499a51512a914675c80514222ded34f15d66e741327fa6deb1f7be09feac334
data/README.md CHANGED
@@ -159,7 +159,7 @@ The client will automatically configure the context for each request.
159
159
  If you need to modify the event context properties or if you desire to add additional properties such as user traits to the context, you can pass the properties along with the other data. For example:
160
160
  ```ruby
161
161
  {
162
- event: ::Castle::Events::LOGIN_SUCCEEDED,
162
+ event: '$login.succeeded',
163
163
  user_id: user.id,
164
164
  properties: {
165
165
  key: 'value'
@@ -180,7 +180,7 @@ Here is a simple example of a track event.
180
180
  ```ruby
181
181
  begin
182
182
  castle.track(
183
- event: ::Castle::Events::LOGIN_SUCCEEDED,
183
+ event: '$login.succeeded',
184
184
  user_id: user.id
185
185
  )
186
186
  rescue Castle::Error => e
@@ -213,7 +213,7 @@ end
213
213
  ```ruby
214
214
  payload = ::Castle::Payload::Prepare.call(
215
215
  {
216
- event: ::Castle::Events::LOGIN_SUCCEEDED,
216
+ event: '$login.succeeded',
217
217
  user_id: user.id,
218
218
  properties: {
219
219
  key: 'value'
@@ -234,12 +234,12 @@ If you want to reuse the connection to send multiple events:
234
234
  ```ruby
235
235
  Castle::Session.call do |http|
236
236
  castle.track(
237
- event: ::Castle::Events::LOGOUT_SUCCEEDED,
237
+ event: '$logout.succeeded',
238
238
  user_id: user2.id
239
239
  http: http
240
240
  )
241
241
  castle.track(
242
- event: ::Castle::Events::LOGIN_SUCCEEDED,
242
+ event: '$login.succeeded',
243
243
  user_id: user1.id
244
244
  http: http
245
245
  )
@@ -248,11 +248,11 @@ end
248
248
 
249
249
  ## Events
250
250
 
251
- List of Recognized Events can be found [here](https://github.com/castle/castle-ruby/tree/master/lib/castle/events.rb) or in the [docs](https://docs.castle.io/api_reference/#list-of-recognized-events)
251
+ List of Recognized Events can be found in the [docs](https://docs.castle.io/v1/reference/events/)
252
252
 
253
253
  ## Device management
254
254
 
255
- This SDK allows issuing requests to [Castle's Device Management Endpoints](https://docs.castle.io/device_management_tool/). Use these endpoints for admin-level management of end-user devices (i.e., for an internal dashboard).
255
+ This SDK allows issuing requests to [Castle's Device Management Endpoints](https://docs.castle.io/v1/reference/api-reference/#devices). Use these endpoints for admin-level management of end-user devices (i.e., for an internal dashboard).
256
256
 
257
257
  Fetching device data, approving a device, reporting a device requires a valid `device_token`.
258
258
 
@@ -305,4 +305,4 @@ Castle uses webhooks to notify about `$incident.confirmed` or `$review.opened` e
305
305
 
306
306
  ## Documentation
307
307
 
308
- [Official Castle docs](https://castle.io/docs)
308
+ [Official Castle docs](https://docs.castle.io/)
data/lib/castle.rb CHANGED
@@ -1,16 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- %w[
4
- openssl
5
- net/http
6
- json
7
- time
8
- ].each(&method(:require))
3
+ %w[openssl net/http json time].each(&method(:require))
9
4
 
10
5
  %w[
11
6
  castle/version
12
7
  castle/verdict
13
- castle/events
14
8
  castle/errors
15
9
  castle/command
16
10
  castle/utils/deep_symbolize_keys
@@ -29,21 +23,23 @@
29
23
  castle/commands/approve_device
30
24
  castle/commands/authenticate
31
25
  castle/commands/end_impersonation
26
+ castle/commands/filter
32
27
  castle/commands/get_device
33
28
  castle/commands/get_devices_for_user
34
- castle/commands/identify
29
+ castle/commands/log
35
30
  castle/commands/report_device
36
- castle/commands/review
31
+ castle/commands/risk
37
32
  castle/commands/start_impersonation
38
33
  castle/commands/track
39
34
  castle/api/approve_device
40
35
  castle/api/authenticate
41
36
  castle/api/end_impersonation
37
+ castle/api/filter
42
38
  castle/api/get_device
43
39
  castle/api/get_devices_for_user
44
- castle/api/identify
40
+ castle/api/log
45
41
  castle/api/report_device
46
- castle/api/review
42
+ castle/api/risk
47
43
  castle/api/start_impersonation
48
44
  castle/api/track
49
45
  castle/payload/prepare
data/lib/castle/api.rb CHANGED
@@ -20,12 +20,10 @@ module Castle
20
20
  # @param command [String]
21
21
  # @param headers [Hash]
22
22
  # @param http [Net::HTTP]
23
- # @param config [Castle::Configuration, Castle::SingletonConfiguration]
23
+ # @param config [Castle::Configuration, Castle::SingletonConfiguration, nil]
24
24
  # @return [Hash]
25
- def call(command, headers = {}, http = nil, config = Castle.config)
26
- Castle::Core::ProcessResponse.call(
27
- send_request(command, headers, http, config)
28
- )
25
+ def call(command, headers = {}, http = nil, config = nil)
26
+ Castle::Core::ProcessResponse.call(send_request(command, headers, http, config), config)
29
27
  end
30
28
 
31
29
  private
@@ -34,16 +32,13 @@ module Castle
34
32
  # @param headers [Hash]
35
33
  # @param http [Net::HTTP]
36
34
  # @param config [Castle::Configuration, Castle::SingletonConfiguration]
37
- def send_request(command, headers = {}, http = nil, config = Castle.config)
35
+ def send_request(command, headers = {}, http = nil, config = nil)
36
+ config ||= Castle.config
37
+
38
38
  raise Castle::ConfigurationError, 'configuration is not valid' unless config.valid?
39
39
 
40
40
  begin
41
- Castle::Core::SendRequest.call(
42
- command,
43
- headers,
44
- http,
45
- config
46
- )
41
+ Castle::Core::SendRequest.call(command, headers, http, config)
47
42
  rescue *HANDLED_ERRORS => e
48
43
  # @note We need to initialize the error, as the original error is a cause for this
49
44
  # custom exception. If we would do it the default Ruby way, the original error
@@ -12,12 +12,7 @@ module Castle
12
12
  http = options.delete(:http)
13
13
  config = options.delete(:config) || Castle.config
14
14
 
15
- Castle::API.call(
16
- Castle::Commands::ApproveDevice.build(options),
17
- {},
18
- http,
19
- config
20
- )
15
+ Castle::API.call(Castle::Commands::ApproveDevice.build(options), {}, http, config)
21
16
  end
22
17
  end
23
18
  end
@@ -14,16 +14,19 @@ module Castle
14
14
  http = options.delete(:http)
15
15
  config = options.delete(:config) || Castle.config
16
16
 
17
- response = Castle::API.call(
18
- Castle::Commands::Authenticate.build(options),
19
- {},
20
- http,
21
- config
22
- )
17
+ response =
18
+ Castle::API.call(Castle::Commands::Authenticate.build(options), {}, http, config)
23
19
  response.merge(failover: false, failover_reason: nil)
24
20
  rescue Castle::RequestError, Castle::InternalServerError => e
25
21
  unless config.failover_strategy == :throw
26
- return Castle::Failover::PrepareResponse.new(options[:user_id], reason: e.to_s).call
22
+ strategy = (config || Castle.config).failover_strategy
23
+ return(
24
+ Castle::Failover::PrepareResponse.new(
25
+ options[:user_id],
26
+ reason: e.to_s,
27
+ strategy: strategy
28
+ ).call
29
+ )
27
30
  end
28
31
 
29
32
  raise e
@@ -14,14 +14,9 @@ module Castle
14
14
  http = options.delete(:http)
15
15
  config = options.delete(:config) || Castle.config
16
16
 
17
- Castle::API.call(
18
- Castle::Commands::EndImpersonation.build(options),
19
- {},
20
- http,
21
- config
22
- ).tap do |response|
23
- raise Castle::ImpersonationFailed unless response[:success]
24
- end
17
+ Castle::API
18
+ .call(Castle::Commands::EndImpersonation.build(options), {}, http, config)
19
+ .tap { |response| raise Castle::ImpersonationFailed unless response[:success] }
25
20
  end
26
21
  end
27
22
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Castle
4
+ module API
5
+ # Module for filter endpoint
6
+ module Filter
7
+ class << self
8
+ # @param options [Hash]
9
+ # return [Hash]
10
+ def call(options = {})
11
+ unless options[:no_symbolize]
12
+ options = Castle::Utils::DeepSymbolizeKeys.call(options || {})
13
+ end
14
+ options.delete(:no_symbolize)
15
+ http = options.delete(:http)
16
+ config = options.delete(:config) || Castle.config
17
+
18
+ response = Castle::API.call(Castle::Commands::Filter.build(options), {}, http, config)
19
+ response.merge(failover: false, failover_reason: nil)
20
+ rescue Castle::RequestError, Castle::InternalServerError => e
21
+ unless config.failover_strategy == :throw
22
+ strategy = (config || Castle.config).failover_strategy
23
+ return(
24
+ Castle::Failover::PrepareResponse.new(
25
+ options[:user][:id],
26
+ reason: e.to_s,
27
+ strategy: strategy
28
+ ).call
29
+ )
30
+ end
31
+
32
+ raise e
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -12,12 +12,7 @@ module Castle
12
12
  http = options.delete(:http)
13
13
  config = options.delete(:config) || Castle.config
14
14
 
15
- Castle::API.call(
16
- Castle::Commands::GetDevice.build(options),
17
- {},
18
- http,
19
- config
20
- )
15
+ Castle::API.call(Castle::Commands::GetDevice.build(options), {}, http, config)
21
16
  end
22
17
  end
23
18
  end
@@ -12,12 +12,7 @@ module Castle
12
12
  http = options.delete(:http)
13
13
  config = options.delete(:config) || Castle.config
14
14
 
15
- Castle::API.call(
16
- Castle::Commands::GetDevicesForUser.build(options),
17
- {},
18
- http,
19
- config
20
- )
15
+ Castle::API.call(Castle::Commands::GetDevicesForUser.build(options), {}, http, config)
21
16
  end
22
17
  end
23
18
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Castle
4
+ module API
5
+ # Module for log endpoint
6
+ module Log
7
+ class << self
8
+ # @param options [Hash]
9
+ # return [Hash]
10
+ def call(options = {})
11
+ unless options[:no_symbolize]
12
+ options = Castle::Utils::DeepSymbolizeKeys.call(options || {})
13
+ end
14
+ options.delete(:no_symbolize)
15
+ http = options.delete(:http)
16
+ config = options.delete(:config) || Castle.config
17
+
18
+ response = Castle::API.call(Castle::Commands::Log.build(options), {}, http, config)
19
+ response.merge(failover: false, failover_reason: nil)
20
+ rescue Castle::RequestError, Castle::InternalServerError => e
21
+ unless config.failover_strategy == :throw
22
+ strategy = (config || Castle.config).failover_strategy
23
+ return(
24
+ Castle::Failover::PrepareResponse.new(
25
+ options[:user][:id],
26
+ reason: e.to_s,
27
+ strategy: strategy
28
+ ).call
29
+ )
30
+ end
31
+
32
+ raise e
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -12,12 +12,7 @@ module Castle
12
12
  http = options.delete(:http)
13
13
  config = options.delete(:config) || Castle.config
14
14
 
15
- Castle::API.call(
16
- Castle::Commands::ReportDevice.build(options),
17
- {},
18
- http,
19
- config
20
- )
15
+ Castle::API.call(Castle::Commands::ReportDevice.build(options), {}, http, config)
21
16
  end
22
17
  end
23
18
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Castle
4
+ module API
5
+ # Module for risk endpoint
6
+ module Risk
7
+ class << self
8
+ # @param options [Hash]
9
+ # return [Hash]
10
+ def call(options = {})
11
+ unless options[:no_symbolize]
12
+ options = Castle::Utils::DeepSymbolizeKeys.call(options || {})
13
+ end
14
+ options.delete(:no_symbolize)
15
+ http = options.delete(:http)
16
+ config = options.delete(:config) || Castle.config
17
+
18
+ response = Castle::API.call(Castle::Commands::Risk.build(options), {}, http, config)
19
+ response.merge(failover: false, failover_reason: nil)
20
+ rescue Castle::RequestError, Castle::InternalServerError => e
21
+ unless config.failover_strategy == :throw
22
+ strategy = (config || Castle.config).failover_strategy
23
+ return(
24
+ Castle::Failover::PrepareResponse.new(
25
+ options[:user][:id],
26
+ reason: e.to_s,
27
+ strategy: strategy
28
+ ).call
29
+ )
30
+ end
31
+
32
+ raise e
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -14,14 +14,9 @@ module Castle
14
14
  http = options.delete(:http)
15
15
  config = options.delete(:config) || Castle.config
16
16
 
17
- Castle::API.call(
18
- Castle::Commands::StartImpersonation.build(options),
19
- {},
20
- http,
21
- config
22
- ).tap do |response|
23
- raise Castle::ImpersonationFailed unless response[:success]
24
- end
17
+ Castle::API
18
+ .call(Castle::Commands::StartImpersonation.build(options), {}, http, config)
19
+ .tap { |response| raise Castle::ImpersonationFailed unless response[:success] }
25
20
  end
26
21
  end
27
22
  end
@@ -13,12 +13,7 @@ module Castle
13
13
  http = options.delete(:http)
14
14
  config = options.delete(:config) || Castle.config
15
15
 
16
- Castle::API.call(
17
- Castle::Commands::Track.build(options),
18
- {},
19
- http,
20
- config
21
- )
16
+ Castle::API.call(Castle::Commands::Track.build(options), {}, http, config)
22
17
  end
23
18
  end
24
19
  end
data/lib/castle/client.rb CHANGED
@@ -1,12 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Castle
4
+ # Castle's client.
4
5
  class Client
5
6
  class << self
6
7
  def from_request(request, options = {})
7
- new(
8
- options.merge(context: Castle::Context::Prepare.call(request, options))
9
- )
8
+ new(options.merge(context: Castle::Context::Prepare.call(request, options)))
10
9
  end
11
10
  end
12
11
 
@@ -34,7 +33,7 @@ module Castle
34
33
  end
35
34
 
36
35
  # @param options [Hash]
37
- def identify(options = {})
36
+ def track(options = {})
38
37
  options = Castle::Utils::DeepSymbolizeKeys.call(options || {})
39
38
 
40
39
  return unless tracked?
@@ -43,49 +42,68 @@ module Castle
43
42
 
44
43
  new_context = Castle::Context::Merge.call(@context, options[:context])
45
44
 
46
- Castle::API::Identify.call(options.merge(context: new_context, no_symbolize: true))
45
+ Castle::API::Track.call(options.merge(context: new_context, no_symbolize: true))
47
46
  end
48
47
 
49
48
  # @param options [Hash]
50
- def track(options = {})
49
+ def filter(options = {})
51
50
  options = Castle::Utils::DeepSymbolizeKeys.call(options || {})
52
51
 
53
- return unless tracked?
52
+ return generate_do_not_track_response(options[:user][:id]) unless tracked?
54
53
 
55
54
  add_timestamp_if_necessary(options)
56
55
 
57
56
  new_context = Castle::Context::Merge.call(@context, options[:context])
58
57
 
59
- Castle::API::Track.call(options.merge(context: new_context, no_symbolize: true))
58
+ Castle::API::Filter.call(options.merge(context: new_context, no_symbolize: true))
60
59
  end
61
60
 
62
61
  # @param options [Hash]
63
- def start_impersonation(options = {})
62
+ def risk(options = {})
64
63
  options = Castle::Utils::DeepSymbolizeKeys.call(options || {})
65
64
 
65
+ return generate_do_not_track_response(options[:user][:id]) unless tracked?
66
+
66
67
  add_timestamp_if_necessary(options)
67
68
 
68
69
  new_context = Castle::Context::Merge.call(@context, options[:context])
69
70
 
70
- Castle::API::StartImpersonation.call(options.merge(context: new_context, no_symbolize: true))
71
+ Castle::API::Risk.call(options.merge(context: new_context, no_symbolize: true))
71
72
  end
72
73
 
73
74
  # @param options [Hash]
74
- def end_impersonation(options = {})
75
+ def log(options = {})
75
76
  options = Castle::Utils::DeepSymbolizeKeys.call(options || {})
76
77
 
78
+ return generate_do_not_track_response(options[:user][:id]) unless tracked?
79
+
77
80
  add_timestamp_if_necessary(options)
78
81
 
79
82
  new_context = Castle::Context::Merge.call(@context, options[:context])
80
83
 
81
- Castle::API::EndImpersonation.call(options.merge(context: new_context, no_symbolize: true))
84
+ Castle::API::Log.call(options.merge(context: new_context, no_symbolize: true))
85
+ end
86
+
87
+ # @param options [Hash]
88
+ def start_impersonation(options = {})
89
+ options = Castle::Utils::DeepSymbolizeKeys.call(options || {})
90
+
91
+ add_timestamp_if_necessary(options)
92
+
93
+ new_context = Castle::Context::Merge.call(@context, options[:context])
94
+
95
+ Castle::API::StartImpersonation.call(options.merge(context: new_context, no_symbolize: true))
82
96
  end
83
97
 
84
98
  # @param options [Hash]
85
- def review(options = {})
99
+ def end_impersonation(options = {})
86
100
  options = Castle::Utils::DeepSymbolizeKeys.call(options || {})
87
101
 
88
- Castle::API::Review.call(options.merge(no_symbolize: true))
102
+ add_timestamp_if_necessary(options)
103
+
104
+ new_context = Castle::Context::Merge.call(@context, options[:context])
105
+
106
+ Castle::API::EndImpersonation.call(options.merge(context: new_context, no_symbolize: true))
89
107
  end
90
108
 
91
109
  def disable_tracking
@@ -96,17 +114,19 @@ module Castle
96
114
  @do_not_track = false
97
115
  end
98
116
 
117
+ # @return [Boolean]
99
118
  def tracked?
100
119
  !@do_not_track
101
120
  end
102
121
 
103
122
  private
104
123
 
105
- # @param user_id [String|Boolean]
124
+ # @param user_id [String, Boolean]
106
125
  def generate_do_not_track_response(user_id)
107
126
  Castle::Failover::PrepareResponse.new(
108
127
  user_id,
109
- strategy: :allow, reason: 'Castle is set to do not track.'
128
+ strategy: :allow,
129
+ reason: 'Castle is set to do not track.'
110
130
  ).call
111
131
  end
112
132