castle-rb 4.2.1 → 7.0.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.
- checksums.yaml +4 -4
 - data/README.md +160 -45
 - data/lib/castle.rb +49 -28
 - data/lib/castle/api.rb +21 -14
 - data/lib/castle/api/approve_device.rb +20 -0
 - data/lib/castle/api/authenticate.rb +37 -0
 - data/lib/castle/api/end_impersonation.rb +24 -0
 - data/lib/castle/api/filter.rb +37 -0
 - data/lib/castle/api/get_device.rb +20 -0
 - data/lib/castle/api/get_devices_for_user.rb +20 -0
 - data/lib/castle/api/log.rb +37 -0
 - data/lib/castle/api/report_device.rb +20 -0
 - data/lib/castle/api/risk.rb +37 -0
 - data/lib/castle/api/start_impersonation.rb +24 -0
 - data/lib/castle/api/track.rb +21 -0
 - data/lib/castle/client.rb +78 -51
 - data/lib/castle/{extractors/client_id.rb → client_id/extract.rb} +2 -2
 - data/lib/castle/commands/approve_device.rb +17 -0
 - data/lib/castle/commands/authenticate.rb +13 -13
 - data/lib/castle/commands/end_impersonation.rb +25 -0
 - data/lib/castle/commands/filter.rb +23 -0
 - data/lib/castle/commands/get_device.rb +17 -0
 - data/lib/castle/commands/get_devices_for_user.rb +17 -0
 - data/lib/castle/commands/log.rb +23 -0
 - data/lib/castle/commands/report_device.rb +17 -0
 - data/lib/castle/commands/risk.rb +23 -0
 - data/lib/castle/commands/start_impersonation.rb +25 -0
 - data/lib/castle/commands/track.rb +12 -13
 - data/lib/castle/configuration.rb +57 -32
 - data/lib/castle/context/{default.rb → get_default.rb} +5 -6
 - data/lib/castle/context/{merger.rb → merge.rb} +3 -3
 - data/lib/castle/context/prepare.rb +18 -0
 - data/lib/castle/context/{sanitizer.rb → sanitize.rb} +1 -1
 - data/lib/castle/core/get_connection.rb +27 -0
 - data/lib/castle/{api/response.rb → core/process_response.rb} +8 -3
 - data/lib/castle/core/process_webhook.rb +25 -0
 - data/lib/castle/core/send_request.rb +42 -0
 - data/lib/castle/errors.rb +38 -12
 - data/lib/castle/failover/prepare_response.rb +18 -0
 - data/lib/castle/failover/strategy.rb +23 -0
 - data/lib/castle/headers/extract.rb +47 -0
 - data/lib/castle/headers/filter.rb +40 -0
 - data/lib/castle/headers/format.rb +24 -0
 - data/lib/castle/{extractors/ip.rb → ips/extract.rb} +31 -9
 - data/lib/castle/logger.rb +19 -0
 - data/lib/castle/payload/prepare.rb +26 -0
 - data/lib/castle/secure_mode.rb +7 -2
 - data/lib/castle/session.rb +18 -0
 - data/lib/castle/singleton_configuration.rb +9 -0
 - data/lib/castle/support/hanami.rb +2 -6
 - data/lib/castle/support/rails.rb +1 -3
 - data/lib/castle/utils/clean_invalid_chars.rb +22 -0
 - data/lib/castle/utils/clone.rb +15 -0
 - data/lib/castle/utils/deep_symbolize_keys.rb +45 -0
 - data/lib/castle/utils/get_timestamp.rb +15 -0
 - data/lib/castle/utils/{merger.rb → merge.rb} +3 -3
 - data/lib/castle/utils/secure_compare.rb +22 -0
 - data/lib/castle/validators/not_supported.rb +1 -0
 - data/lib/castle/validators/present.rb +1 -0
 - data/lib/castle/verdict.rb +15 -0
 - data/lib/castle/version.rb +1 -1
 - data/lib/castle/webhooks/verify.rb +45 -0
 - data/spec/integration/rails/rails_spec.rb +42 -14
 - data/spec/integration/rails/support/application.rb +3 -1
 - data/spec/integration/rails/support/home_controller.rb +50 -6
 - data/spec/lib/castle/api/approve_device_spec.rb +21 -0
 - data/spec/lib/castle/api/authenticate_spec.rb +136 -0
 - data/spec/lib/castle/api/end_impersonation_spec.rb +65 -0
 - data/spec/lib/castle/api/filter_spec.rb +5 -0
 - data/spec/lib/castle/api/get_device_spec.rb +19 -0
 - data/spec/lib/castle/api/get_devices_for_user_spec.rb +19 -0
 - data/spec/lib/castle/api/log_spec.rb +5 -0
 - data/spec/lib/castle/api/report_device_spec.rb +21 -0
 - data/spec/lib/castle/api/risk_spec.rb +5 -0
 - data/spec/lib/castle/api/start_impersonation_spec.rb +65 -0
 - data/spec/lib/castle/api/track_spec.rb +72 -0
 - data/spec/lib/castle/api_spec.rb +14 -15
 - data/spec/lib/castle/{extractors/client_id_spec.rb → client_id/extract_spec.rb} +6 -15
 - data/spec/lib/castle/client_spec.rb +108 -93
 - data/spec/lib/castle/commands/approve_device_spec.rb +24 -0
 - data/spec/lib/castle/commands/authenticate_spec.rb +15 -31
 - data/spec/lib/castle/commands/end_impersonation_spec.rb +79 -0
 - data/spec/lib/castle/commands/filter_spec.rb +99 -0
 - data/spec/lib/castle/commands/get_device_spec.rb +24 -0
 - data/spec/lib/castle/commands/{review_spec.rb → get_devices_for_user_spec.rb} +7 -7
 - data/spec/lib/castle/commands/log_spec.rb +100 -0
 - data/spec/lib/castle/commands/report_device_spec.rb +24 -0
 - data/spec/lib/castle/commands/risk_spec.rb +100 -0
 - data/spec/lib/castle/commands/start_impersonation_spec.rb +79 -0
 - data/spec/lib/castle/commands/track_spec.rb +14 -34
 - data/spec/lib/castle/configuration_spec.rb +8 -141
 - data/spec/lib/castle/context/{default_spec.rb → get_default_spec.rb} +9 -10
 - data/spec/lib/castle/context/{merger_spec.rb → merge_spec.rb} +1 -1
 - data/spec/lib/castle/context/prepare_spec.rb +43 -0
 - data/spec/lib/castle/context/{sanitizer_spec.rb → sanitize_spec.rb} +1 -1
 - data/spec/lib/castle/core/get_connection_spec.rb +43 -0
 - data/spec/lib/castle/{api/response_spec.rb → core/process_response_spec.rb} +49 -1
 - data/spec/lib/castle/core/process_webhook_spec.rb +46 -0
 - data/spec/lib/castle/core/send_request_spec.rb +77 -0
 - data/spec/lib/castle/failover/strategy_spec.rb +12 -0
 - data/spec/lib/castle/{extractors/headers_spec.rb → headers/extract_spec.rb} +18 -20
 - data/spec/lib/castle/headers/filter_spec.rb +39 -0
 - data/spec/lib/castle/headers/format_spec.rb +25 -0
 - data/spec/lib/castle/{extractors/ip_spec.rb → ips/extract_spec.rb} +27 -8
 - data/spec/lib/castle/logger_spec.rb +38 -0
 - data/spec/lib/castle/payload/prepare_spec.rb +55 -0
 - data/spec/lib/castle/session_spec.rb +65 -0
 - data/spec/lib/castle/singleton_configuration_spec.rb +14 -0
 - data/spec/lib/castle/utils/clean_invalid_chars_spec.rb +69 -0
 - data/spec/lib/castle/utils/{cloner_spec.rb → clone_spec.rb} +3 -3
 - data/spec/lib/castle/utils/deep_symbolize_keys_spec.rb +50 -0
 - data/spec/lib/castle/utils/{timestamp_spec.rb → get_timestamp_spec.rb} +1 -1
 - data/spec/lib/castle/utils/merge_spec.rb +15 -0
 - data/spec/lib/castle/validators/present_spec.rb +5 -6
 - data/spec/lib/castle/verdict_spec.rb +9 -0
 - data/spec/lib/castle/webhooks/verify_spec.rb +53 -0
 - data/spec/lib/castle_spec.rb +4 -10
 - data/spec/spec_helper.rb +3 -3
 - data/spec/support/shared_examples/action_request.rb +152 -0
 - data/spec/support/shared_examples/configuration.rb +101 -0
 - metadata +146 -64
 - data/lib/castle/api/request.rb +0 -42
 - data/lib/castle/api/session.rb +0 -39
 - data/lib/castle/commands/identify.rb +0 -23
 - data/lib/castle/commands/impersonate.rb +0 -26
 - data/lib/castle/commands/review.rb +0 -14
 - data/lib/castle/events.rb +0 -49
 - data/lib/castle/extractors/headers.rb +0 -45
 - data/lib/castle/failover_auth_response.rb +0 -21
 - data/lib/castle/headers_filter.rb +0 -35
 - data/lib/castle/headers_formatter.rb +0 -22
 - data/lib/castle/review.rb +0 -11
 - data/lib/castle/utils.rb +0 -55
 - data/lib/castle/utils/cloner.rb +0 -11
 - data/lib/castle/utils/timestamp.rb +0 -12
 - data/spec/lib/castle/api/request_spec.rb +0 -72
 - data/spec/lib/castle/commands/identify_spec.rb +0 -88
 - data/spec/lib/castle/commands/impersonate_spec.rb +0 -107
 - data/spec/lib/castle/events_spec.rb +0 -5
 - data/spec/lib/castle/headers_filter_spec.rb +0 -37
 - data/spec/lib/castle/headers_formatter_spec.rb +0 -25
 - data/spec/lib/castle/review_spec.rb +0 -19
 - data/spec/lib/castle/utils/merger_spec.rb +0 -13
 - data/spec/lib/castle/utils_spec.rb +0 -156
 
| 
         @@ -1,9 +1,10 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # frozen_string_literal: true
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            class HomeController < ActionController::Base
         
     | 
| 
       4 
     | 
    
         
            -
               
     | 
| 
       5 
     | 
    
         
            -
             
     | 
| 
       6 
     | 
    
         
            -
                 
     | 
| 
      
 4 
     | 
    
         
            +
              # prepare context and calling track with client example
         
     | 
| 
      
 5 
     | 
    
         
            +
              def index1
         
     | 
| 
      
 6 
     | 
    
         
            +
                request_context = ::Castle::Context::Prepare.call(request)
         
     | 
| 
      
 7 
     | 
    
         
            +
                payload = {
         
     | 
| 
       7 
8 
     | 
    
         
             
                  event: '$login.succeeded',
         
     | 
| 
       8 
9 
     | 
    
         
             
                  user_id: '123',
         
     | 
| 
       9 
10 
     | 
    
         
             
                  properties: {
         
     | 
| 
         @@ -12,9 +13,52 @@ class HomeController < ActionController::Base 
     | 
|
| 
       12 
13 
     | 
    
         
             
                  user_traits: {
         
     | 
| 
       13 
14 
     | 
    
         
             
                    key: 'value'
         
     | 
| 
       14 
15 
     | 
    
         
             
                  }
         
     | 
| 
       15 
     | 
    
         
            -
                 
     | 
| 
       16 
     | 
    
         
            -
                client = ::Castle::Client.new(request_context)
         
     | 
| 
       17 
     | 
    
         
            -
                client.track( 
     | 
| 
      
 16 
     | 
    
         
            +
                }
         
     | 
| 
      
 17 
     | 
    
         
            +
                client = ::Castle::Client.new(context: request_context)
         
     | 
| 
      
 18 
     | 
    
         
            +
                client.track(payload)
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                render inline: 'hello'
         
     | 
| 
      
 21 
     | 
    
         
            +
              end
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
              # prepare payload and calling track with client example
         
     | 
| 
      
 24 
     | 
    
         
            +
              def index2
         
     | 
| 
      
 25 
     | 
    
         
            +
                payload =
         
     | 
| 
      
 26 
     | 
    
         
            +
                  ::Castle::Payload::Prepare.call(
         
     | 
| 
      
 27 
     | 
    
         
            +
                    {
         
     | 
| 
      
 28 
     | 
    
         
            +
                      event: '$login.succeeded',
         
     | 
| 
      
 29 
     | 
    
         
            +
                      user_id: '123',
         
     | 
| 
      
 30 
     | 
    
         
            +
                      properties: {
         
     | 
| 
      
 31 
     | 
    
         
            +
                        key: 'value'
         
     | 
| 
      
 32 
     | 
    
         
            +
                      },
         
     | 
| 
      
 33 
     | 
    
         
            +
                      user_traits: {
         
     | 
| 
      
 34 
     | 
    
         
            +
                        key: 'value'
         
     | 
| 
      
 35 
     | 
    
         
            +
                      }
         
     | 
| 
      
 36 
     | 
    
         
            +
                    },
         
     | 
| 
      
 37 
     | 
    
         
            +
                    request
         
     | 
| 
      
 38 
     | 
    
         
            +
                  )
         
     | 
| 
      
 39 
     | 
    
         
            +
                client = ::Castle::Client.new
         
     | 
| 
      
 40 
     | 
    
         
            +
                client.track(payload)
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                render inline: 'hello'
         
     | 
| 
      
 43 
     | 
    
         
            +
              end
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
              # prepare payload and calling track with direct API::Track service
         
     | 
| 
      
 46 
     | 
    
         
            +
              def index3
         
     | 
| 
      
 47 
     | 
    
         
            +
                payload =
         
     | 
| 
      
 48 
     | 
    
         
            +
                  ::Castle::Payload::Prepare.call(
         
     | 
| 
      
 49 
     | 
    
         
            +
                    {
         
     | 
| 
      
 50 
     | 
    
         
            +
                      event: '$login.succeeded',
         
     | 
| 
      
 51 
     | 
    
         
            +
                      user_id: '123',
         
     | 
| 
      
 52 
     | 
    
         
            +
                      properties: {
         
     | 
| 
      
 53 
     | 
    
         
            +
                        key: 'value'
         
     | 
| 
      
 54 
     | 
    
         
            +
                      },
         
     | 
| 
      
 55 
     | 
    
         
            +
                      user_traits: {
         
     | 
| 
      
 56 
     | 
    
         
            +
                        key: 'value'
         
     | 
| 
      
 57 
     | 
    
         
            +
                      }
         
     | 
| 
      
 58 
     | 
    
         
            +
                    },
         
     | 
| 
      
 59 
     | 
    
         
            +
                    request
         
     | 
| 
      
 60 
     | 
    
         
            +
                  )
         
     | 
| 
      
 61 
     | 
    
         
            +
                Castle::API::Track.call(payload)
         
     | 
| 
       18 
62 
     | 
    
         | 
| 
       19 
63 
     | 
    
         
             
                render inline: 'hello'
         
     | 
| 
       20 
64 
     | 
    
         
             
              end
         
     | 
| 
         @@ -0,0 +1,21 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe Castle::API::ApproveDevice do
         
     | 
| 
      
 4 
     | 
    
         
            +
              before do
         
     | 
| 
      
 5 
     | 
    
         
            +
                stub_request(:any, /api.castle.io/)
         
     | 
| 
      
 6 
     | 
    
         
            +
                  .with(basic_auth: ['', 'secret'])
         
     | 
| 
      
 7 
     | 
    
         
            +
                  .to_return(status: 200, body: '{}', headers: {})
         
     | 
| 
      
 8 
     | 
    
         
            +
              end
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
              describe '.call' do
         
     | 
| 
      
 11 
     | 
    
         
            +
                subject(:retrieve) { described_class.call(device_token: device_token) }
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                let(:device_token) { '1234' }
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                before { retrieve }
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                it do
         
     | 
| 
      
 18 
     | 
    
         
            +
                  assert_requested :put, "https://api.castle.io/v1/devices/#{device_token}/approve", times: 1
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
              end
         
     | 
| 
      
 21 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,136 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe Castle::API::Authenticate do
         
     | 
| 
      
 4 
     | 
    
         
            +
              subject(:call_subject) { described_class.call(options) }
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
              let(:ip) { '1.2.3.4' }
         
     | 
| 
      
 7 
     | 
    
         
            +
              let(:cookie_id) { 'abcd' }
         
     | 
| 
      
 8 
     | 
    
         
            +
              let(:ua) { 'Chrome' }
         
     | 
| 
      
 9 
     | 
    
         
            +
              let(:env) do
         
     | 
| 
      
 10 
     | 
    
         
            +
                Rack::MockRequest.env_for(
         
     | 
| 
      
 11 
     | 
    
         
            +
                  '/',
         
     | 
| 
      
 12 
     | 
    
         
            +
                  'HTTP_USER_AGENT' => ua,
         
     | 
| 
      
 13 
     | 
    
         
            +
                  'HTTP_X_FORWARDED_FOR' => ip,
         
     | 
| 
      
 14 
     | 
    
         
            +
                  'HTTP_COOKIE' => "__cid=#{cookie_id};other=efgh"
         
     | 
| 
      
 15 
     | 
    
         
            +
                )
         
     | 
| 
      
 16 
     | 
    
         
            +
              end
         
     | 
| 
      
 17 
     | 
    
         
            +
              let(:request) { Rack::Request.new(env) }
         
     | 
| 
      
 18 
     | 
    
         
            +
              let(:context) { Castle::Context::Prepare.call(request) }
         
     | 
| 
      
 19 
     | 
    
         
            +
              let(:time_now) { Time.now }
         
     | 
| 
      
 20 
     | 
    
         
            +
              let(:time_auto) { time_now.utc.iso8601(3) }
         
     | 
| 
      
 21 
     | 
    
         
            +
              let(:time_user) { (Time.now - 10_000).utc.iso8601(3) }
         
     | 
| 
      
 22 
     | 
    
         
            +
              let(:response_body) { {}.to_json }
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
              before do
         
     | 
| 
      
 25 
     | 
    
         
            +
                Timecop.freeze(time_now)
         
     | 
| 
      
 26 
     | 
    
         
            +
                stub_const('Castle::VERSION', '2.2.0')
         
     | 
| 
      
 27 
     | 
    
         
            +
              end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
              after { Timecop.return }
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
              describe '.call' do
         
     | 
| 
      
 32 
     | 
    
         
            +
                let(:request_body) do
         
     | 
| 
      
 33 
     | 
    
         
            +
                  { event: '$login.succeeded', context: context, user_id: '1234', sent_at: time_auto }
         
     | 
| 
      
 34 
     | 
    
         
            +
                end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
                context 'when used with symbol keys' do
         
     | 
| 
      
 37 
     | 
    
         
            +
                  before do
         
     | 
| 
      
 38 
     | 
    
         
            +
                    stub_request(:any, /api.castle.io/)
         
     | 
| 
      
 39 
     | 
    
         
            +
                      .with(basic_auth: ['', 'secret'])
         
     | 
| 
      
 40 
     | 
    
         
            +
                      .to_return(status: 200, body: response_body, headers: {})
         
     | 
| 
      
 41 
     | 
    
         
            +
                    call_subject
         
     | 
| 
      
 42 
     | 
    
         
            +
                  end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
                  let(:options) { { event: '$login.succeeded', user_id: '1234', context: context } }
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                  it do
         
     | 
| 
      
 47 
     | 
    
         
            +
                    assert_requested :post, 'https://api.castle.io/v1/authenticate', times: 1 do |req|
         
     | 
| 
      
 48 
     | 
    
         
            +
                      JSON.parse(req.body) == JSON.parse(request_body.to_json)
         
     | 
| 
      
 49 
     | 
    
         
            +
                    end
         
     | 
| 
      
 50 
     | 
    
         
            +
                  end
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                  context 'when passed timestamp in options and no defined timestamp' do
         
     | 
| 
      
 53 
     | 
    
         
            +
                    let(:options) do
         
     | 
| 
      
 54 
     | 
    
         
            +
                      { event: '$login.succeeded', user_id: '1234', timestamp: time_user, context: context }
         
     | 
| 
      
 55 
     | 
    
         
            +
                    end
         
     | 
| 
      
 56 
     | 
    
         
            +
                    let(:request_body) do
         
     | 
| 
      
 57 
     | 
    
         
            +
                      {
         
     | 
| 
      
 58 
     | 
    
         
            +
                        event: '$login.succeeded',
         
     | 
| 
      
 59 
     | 
    
         
            +
                        user_id: '1234',
         
     | 
| 
      
 60 
     | 
    
         
            +
                        context: context,
         
     | 
| 
      
 61 
     | 
    
         
            +
                        timestamp: time_user,
         
     | 
| 
      
 62 
     | 
    
         
            +
                        sent_at: time_auto
         
     | 
| 
      
 63 
     | 
    
         
            +
                      }
         
     | 
| 
      
 64 
     | 
    
         
            +
                    end
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
                    it do
         
     | 
| 
      
 67 
     | 
    
         
            +
                      assert_requested :post, 'https://api.castle.io/v1/authenticate', times: 1 do |req|
         
     | 
| 
      
 68 
     | 
    
         
            +
                        JSON.parse(req.body) == JSON.parse(request_body.to_json)
         
     | 
| 
      
 69 
     | 
    
         
            +
                      end
         
     | 
| 
      
 70 
     | 
    
         
            +
                    end
         
     | 
| 
      
 71 
     | 
    
         
            +
                  end
         
     | 
| 
      
 72 
     | 
    
         
            +
                end
         
     | 
| 
      
 73 
     | 
    
         
            +
             
     | 
| 
      
 74 
     | 
    
         
            +
                context 'when denied' do
         
     | 
| 
      
 75 
     | 
    
         
            +
                  let(:failover_appendix) { { failover: false, failover_reason: nil } }
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
                  let(:options) { { event: '$login.succeeded', user_id: '1234', context: context } }
         
     | 
| 
      
 78 
     | 
    
         
            +
             
     | 
| 
      
 79 
     | 
    
         
            +
                  context 'when denied without any risk policy' do
         
     | 
| 
      
 80 
     | 
    
         
            +
                    let(:response_body) { deny_response_without_rp.to_json }
         
     | 
| 
      
 81 
     | 
    
         
            +
                    let(:deny_response_without_rp) do
         
     | 
| 
      
 82 
     | 
    
         
            +
                      { action: 'deny', user_id: '12345', device_token: 'abcdefg1234' }
         
     | 
| 
      
 83 
     | 
    
         
            +
                    end
         
     | 
| 
      
 84 
     | 
    
         
            +
                    let(:deny_without_rp_failover_result) { deny_response_without_rp.merge(failover_appendix) }
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
                    before do
         
     | 
| 
      
 87 
     | 
    
         
            +
                      stub_request(:any, /api.castle.io/)
         
     | 
| 
      
 88 
     | 
    
         
            +
                        .with(basic_auth: ['', 'secret'])
         
     | 
| 
      
 89 
     | 
    
         
            +
                        .to_return(status: 200, body: deny_response_without_rp.to_json, headers: {})
         
     | 
| 
      
 90 
     | 
    
         
            +
                      call_subject
         
     | 
| 
      
 91 
     | 
    
         
            +
                    end
         
     | 
| 
      
 92 
     | 
    
         
            +
             
     | 
| 
      
 93 
     | 
    
         
            +
                    it do
         
     | 
| 
      
 94 
     | 
    
         
            +
                      assert_requested :post, 'https://api.castle.io/v1/authenticate', times: 1 do |req|
         
     | 
| 
      
 95 
     | 
    
         
            +
                        JSON.parse(req.body) == JSON.parse(request_body.to_json)
         
     | 
| 
      
 96 
     | 
    
         
            +
                      end
         
     | 
| 
      
 97 
     | 
    
         
            +
                    end
         
     | 
| 
      
 98 
     | 
    
         
            +
             
     | 
| 
      
 99 
     | 
    
         
            +
                    it { expect(call_subject).to eql(deny_without_rp_failover_result) }
         
     | 
| 
      
 100 
     | 
    
         
            +
                  end
         
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
      
 102 
     | 
    
         
            +
                  context 'when denied with risk policy' do
         
     | 
| 
      
 103 
     | 
    
         
            +
                    let(:deny_response_with_rp) do
         
     | 
| 
      
 104 
     | 
    
         
            +
                      {
         
     | 
| 
      
 105 
     | 
    
         
            +
                        action: 'deny',
         
     | 
| 
      
 106 
     | 
    
         
            +
                        user_id: '12345',
         
     | 
| 
      
 107 
     | 
    
         
            +
                        device_token: 'abcdefg1234',
         
     | 
| 
      
 108 
     | 
    
         
            +
                        risk_policy: {
         
     | 
| 
      
 109 
     | 
    
         
            +
                          id: 'q-rbeMzBTdW2Fd09sbz55A',
         
     | 
| 
      
 110 
     | 
    
         
            +
                          revision_id: 'pke4zqO2TnqVr-NHJOAHEg',
         
     | 
| 
      
 111 
     | 
    
         
            +
                          name: 'Block Users from X',
         
     | 
| 
      
 112 
     | 
    
         
            +
                          type: 'bot'
         
     | 
| 
      
 113 
     | 
    
         
            +
                        }
         
     | 
| 
      
 114 
     | 
    
         
            +
                      }
         
     | 
| 
      
 115 
     | 
    
         
            +
                    end
         
     | 
| 
      
 116 
     | 
    
         
            +
                    let(:response_body) { deny_response_with_rp.to_json }
         
     | 
| 
      
 117 
     | 
    
         
            +
                    let(:deny_with_rp_failover_result) { deny_response_with_rp.merge(failover_appendix) }
         
     | 
| 
      
 118 
     | 
    
         
            +
             
     | 
| 
      
 119 
     | 
    
         
            +
                    before do
         
     | 
| 
      
 120 
     | 
    
         
            +
                      stub_request(:any, /api.castle.io/)
         
     | 
| 
      
 121 
     | 
    
         
            +
                        .with(basic_auth: ['', 'secret'])
         
     | 
| 
      
 122 
     | 
    
         
            +
                        .to_return(status: 200, body: deny_response_with_rp.to_json, headers: {})
         
     | 
| 
      
 123 
     | 
    
         
            +
                      call_subject
         
     | 
| 
      
 124 
     | 
    
         
            +
                    end
         
     | 
| 
      
 125 
     | 
    
         
            +
             
     | 
| 
      
 126 
     | 
    
         
            +
                    it do
         
     | 
| 
      
 127 
     | 
    
         
            +
                      assert_requested :post, 'https://api.castle.io/v1/authenticate', times: 1 do |req|
         
     | 
| 
      
 128 
     | 
    
         
            +
                        JSON.parse(req.body) == JSON.parse(request_body.to_json)
         
     | 
| 
      
 129 
     | 
    
         
            +
                      end
         
     | 
| 
      
 130 
     | 
    
         
            +
                    end
         
     | 
| 
      
 131 
     | 
    
         
            +
             
     | 
| 
      
 132 
     | 
    
         
            +
                    it { expect(call_subject).to eql(deny_with_rp_failover_result) }
         
     | 
| 
      
 133 
     | 
    
         
            +
                  end
         
     | 
| 
      
 134 
     | 
    
         
            +
                end
         
     | 
| 
      
 135 
     | 
    
         
            +
              end
         
     | 
| 
      
 136 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,65 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe Castle::API::EndImpersonation do
         
     | 
| 
      
 4 
     | 
    
         
            +
              subject(:call) { described_class.call(options) }
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
              let(:ip) { '1.2.3.4' }
         
     | 
| 
      
 7 
     | 
    
         
            +
              let(:cookie_id) { 'abcd' }
         
     | 
| 
      
 8 
     | 
    
         
            +
              let(:ua) { 'Chrome' }
         
     | 
| 
      
 9 
     | 
    
         
            +
              let(:env) do
         
     | 
| 
      
 10 
     | 
    
         
            +
                Rack::MockRequest.env_for(
         
     | 
| 
      
 11 
     | 
    
         
            +
                  '/',
         
     | 
| 
      
 12 
     | 
    
         
            +
                  'HTTP_USER_AGENT' => ua,
         
     | 
| 
      
 13 
     | 
    
         
            +
                  'HTTP_X_FORWARDED_FOR' => ip,
         
     | 
| 
      
 14 
     | 
    
         
            +
                  'HTTP_COOKIE' => "__cid=#{cookie_id};other=efgh"
         
     | 
| 
      
 15 
     | 
    
         
            +
                )
         
     | 
| 
      
 16 
     | 
    
         
            +
              end
         
     | 
| 
      
 17 
     | 
    
         
            +
              let(:request) { Rack::Request.new(env) }
         
     | 
| 
      
 18 
     | 
    
         
            +
              let(:context) { Castle::Context::Prepare.call(request) }
         
     | 
| 
      
 19 
     | 
    
         
            +
              let(:time_now) { Time.now }
         
     | 
| 
      
 20 
     | 
    
         
            +
              let(:time_auto) { time_now.utc.iso8601(3) }
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
              before do
         
     | 
| 
      
 23 
     | 
    
         
            +
                Timecop.freeze(time_now)
         
     | 
| 
      
 24 
     | 
    
         
            +
                stub_const('Castle::VERSION', '2.2.0')
         
     | 
| 
      
 25 
     | 
    
         
            +
                stub_request(:any, /api.castle.io/)
         
     | 
| 
      
 26 
     | 
    
         
            +
                  .with(basic_auth: ['', 'secret'])
         
     | 
| 
      
 27 
     | 
    
         
            +
                  .to_return(status: 200, body: response_body, headers: {})
         
     | 
| 
      
 28 
     | 
    
         
            +
              end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
              after { Timecop.return }
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
              describe 'call' do
         
     | 
| 
      
 33 
     | 
    
         
            +
                let(:impersonator) { 'test@castle.io' }
         
     | 
| 
      
 34 
     | 
    
         
            +
                let(:request_body) do
         
     | 
| 
      
 35 
     | 
    
         
            +
                  {
         
     | 
| 
      
 36 
     | 
    
         
            +
                    user_id: '1234',
         
     | 
| 
      
 37 
     | 
    
         
            +
                    sent_at: time_auto,
         
     | 
| 
      
 38 
     | 
    
         
            +
                    properties: {
         
     | 
| 
      
 39 
     | 
    
         
            +
                      impersonator: impersonator
         
     | 
| 
      
 40 
     | 
    
         
            +
                    },
         
     | 
| 
      
 41 
     | 
    
         
            +
                    context: context
         
     | 
| 
      
 42 
     | 
    
         
            +
                  }
         
     | 
| 
      
 43 
     | 
    
         
            +
                end
         
     | 
| 
      
 44 
     | 
    
         
            +
                let(:response_body) { { success: true }.to_json }
         
     | 
| 
      
 45 
     | 
    
         
            +
                let(:options) do
         
     | 
| 
      
 46 
     | 
    
         
            +
                  { user_id: '1234', properties: { impersonator: impersonator }, context: context }
         
     | 
| 
      
 47 
     | 
    
         
            +
                end
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
                context 'when used with symbol keys' do
         
     | 
| 
      
 50 
     | 
    
         
            +
                  before { call }
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                  it do
         
     | 
| 
      
 53 
     | 
    
         
            +
                    assert_requested :delete, 'https://api.castle.io/v1/impersonate', times: 1 do |req|
         
     | 
| 
      
 54 
     | 
    
         
            +
                      JSON.parse(req.body) == JSON.parse(request_body.to_json)
         
     | 
| 
      
 55 
     | 
    
         
            +
                    end
         
     | 
| 
      
 56 
     | 
    
         
            +
                  end
         
     | 
| 
      
 57 
     | 
    
         
            +
                end
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
                context 'when request is not successful' do
         
     | 
| 
      
 60 
     | 
    
         
            +
                  let(:response_body) { {}.to_json }
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
                  it { expect { call }.to raise_error(Castle::ImpersonationFailed) }
         
     | 
| 
      
 63 
     | 
    
         
            +
                end
         
     | 
| 
      
 64 
     | 
    
         
            +
              end
         
     | 
| 
      
 65 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,19 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe Castle::API::GetDevice do
         
     | 
| 
      
 4 
     | 
    
         
            +
              before do
         
     | 
| 
      
 5 
     | 
    
         
            +
                stub_request(:any, /api.castle.io/)
         
     | 
| 
      
 6 
     | 
    
         
            +
                  .with(basic_auth: ['', 'secret'])
         
     | 
| 
      
 7 
     | 
    
         
            +
                  .to_return(status: 200, body: '{}', headers: {})
         
     | 
| 
      
 8 
     | 
    
         
            +
              end
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
              describe '.call' do
         
     | 
| 
      
 11 
     | 
    
         
            +
                subject(:retrieve) { described_class.call(device_token: device_token) }
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                let(:device_token) { '1234' }
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                before { retrieve }
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                it { assert_requested :get, "https://api.castle.io/v1/devices/#{device_token}", times: 1 }
         
     | 
| 
      
 18 
     | 
    
         
            +
              end
         
     | 
| 
      
 19 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,19 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe Castle::API::GetDevicesForUser do
         
     | 
| 
      
 4 
     | 
    
         
            +
              before do
         
     | 
| 
      
 5 
     | 
    
         
            +
                stub_request(:any, /api.castle.io/)
         
     | 
| 
      
 6 
     | 
    
         
            +
                  .with(basic_auth: ['', 'secret'])
         
     | 
| 
      
 7 
     | 
    
         
            +
                  .to_return(status: 200, body: '{}', headers: {})
         
     | 
| 
      
 8 
     | 
    
         
            +
              end
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
              describe '.call' do
         
     | 
| 
      
 11 
     | 
    
         
            +
                subject(:retrieve) { described_class.call(user_id: user_id) }
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                let(:user_id) { '1234' }
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                before { retrieve }
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                it { assert_requested :get, "https://api.castle.io/v1/users/#{user_id}/devices", times: 1 }
         
     | 
| 
      
 18 
     | 
    
         
            +
              end
         
     | 
| 
      
 19 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,21 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe Castle::API::ReportDevice do
         
     | 
| 
      
 4 
     | 
    
         
            +
              before do
         
     | 
| 
      
 5 
     | 
    
         
            +
                stub_request(:any, /api.castle.io/)
         
     | 
| 
      
 6 
     | 
    
         
            +
                  .with(basic_auth: ['', 'secret'])
         
     | 
| 
      
 7 
     | 
    
         
            +
                  .to_return(status: 200, body: '{}', headers: {})
         
     | 
| 
      
 8 
     | 
    
         
            +
              end
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
              describe '.call' do
         
     | 
| 
      
 11 
     | 
    
         
            +
                subject(:retrieve) { described_class.call(device_token: device_token) }
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                let(:device_token) { '1234' }
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                before { retrieve }
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                it do
         
     | 
| 
      
 18 
     | 
    
         
            +
                  assert_requested :put, "https://api.castle.io/v1/devices/#{device_token}/report", times: 1
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
              end
         
     | 
| 
      
 21 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,65 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe Castle::API::StartImpersonation do
         
     | 
| 
      
 4 
     | 
    
         
            +
              subject(:call) { described_class.call(options) }
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
              let(:ip) { '1.2.3.4' }
         
     | 
| 
      
 7 
     | 
    
         
            +
              let(:cookie_id) { 'abcd' }
         
     | 
| 
      
 8 
     | 
    
         
            +
              let(:ua) { 'Chrome' }
         
     | 
| 
      
 9 
     | 
    
         
            +
              let(:env) do
         
     | 
| 
      
 10 
     | 
    
         
            +
                Rack::MockRequest.env_for(
         
     | 
| 
      
 11 
     | 
    
         
            +
                  '/',
         
     | 
| 
      
 12 
     | 
    
         
            +
                  'HTTP_USER_AGENT' => ua,
         
     | 
| 
      
 13 
     | 
    
         
            +
                  'HTTP_X_FORWARDED_FOR' => ip,
         
     | 
| 
      
 14 
     | 
    
         
            +
                  'HTTP_COOKIE' => "__cid=#{cookie_id};other=efgh"
         
     | 
| 
      
 15 
     | 
    
         
            +
                )
         
     | 
| 
      
 16 
     | 
    
         
            +
              end
         
     | 
| 
      
 17 
     | 
    
         
            +
              let(:request) { Rack::Request.new(env) }
         
     | 
| 
      
 18 
     | 
    
         
            +
              let(:context) { Castle::Context::Prepare.call(request) }
         
     | 
| 
      
 19 
     | 
    
         
            +
              let(:time_now) { Time.now }
         
     | 
| 
      
 20 
     | 
    
         
            +
              let(:time_auto) { time_now.utc.iso8601(3) }
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
              before do
         
     | 
| 
      
 23 
     | 
    
         
            +
                Timecop.freeze(time_now)
         
     | 
| 
      
 24 
     | 
    
         
            +
                stub_const('Castle::VERSION', '2.2.0')
         
     | 
| 
      
 25 
     | 
    
         
            +
                stub_request(:any, /api.castle.io/)
         
     | 
| 
      
 26 
     | 
    
         
            +
                  .with(basic_auth: ['', 'secret'])
         
     | 
| 
      
 27 
     | 
    
         
            +
                  .to_return(status: 200, body: response_body, headers: {})
         
     | 
| 
      
 28 
     | 
    
         
            +
              end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
              after { Timecop.return }
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
              describe 'call' do
         
     | 
| 
      
 33 
     | 
    
         
            +
                let(:impersonator) { 'test@castle.io' }
         
     | 
| 
      
 34 
     | 
    
         
            +
                let(:request_body) do
         
     | 
| 
      
 35 
     | 
    
         
            +
                  {
         
     | 
| 
      
 36 
     | 
    
         
            +
                    user_id: '1234',
         
     | 
| 
      
 37 
     | 
    
         
            +
                    sent_at: time_auto,
         
     | 
| 
      
 38 
     | 
    
         
            +
                    properties: {
         
     | 
| 
      
 39 
     | 
    
         
            +
                      impersonator: impersonator
         
     | 
| 
      
 40 
     | 
    
         
            +
                    },
         
     | 
| 
      
 41 
     | 
    
         
            +
                    context: context
         
     | 
| 
      
 42 
     | 
    
         
            +
                  }
         
     | 
| 
      
 43 
     | 
    
         
            +
                end
         
     | 
| 
      
 44 
     | 
    
         
            +
                let(:response_body) { { success: true }.to_json }
         
     | 
| 
      
 45 
     | 
    
         
            +
                let(:options) do
         
     | 
| 
      
 46 
     | 
    
         
            +
                  { user_id: '1234', properties: { impersonator: impersonator }, context: context }
         
     | 
| 
      
 47 
     | 
    
         
            +
                end
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
                context 'when used with symbol keys' do
         
     | 
| 
      
 50 
     | 
    
         
            +
                  before { call }
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                  it do
         
     | 
| 
      
 53 
     | 
    
         
            +
                    assert_requested :post, 'https://api.castle.io/v1/impersonate', times: 1 do |req|
         
     | 
| 
      
 54 
     | 
    
         
            +
                      JSON.parse(req.body) == JSON.parse(request_body.to_json)
         
     | 
| 
      
 55 
     | 
    
         
            +
                    end
         
     | 
| 
      
 56 
     | 
    
         
            +
                  end
         
     | 
| 
      
 57 
     | 
    
         
            +
                end
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
                context 'when request is not successful' do
         
     | 
| 
      
 60 
     | 
    
         
            +
                  let(:response_body) { {}.to_json }
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
                  it { expect { call }.to raise_error(Castle::ImpersonationFailed) }
         
     | 
| 
      
 63 
     | 
    
         
            +
                end
         
     | 
| 
      
 64 
     | 
    
         
            +
              end
         
     | 
| 
      
 65 
     | 
    
         
            +
            end
         
     |