castle-rb 5.0.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +107 -33
  3. data/lib/castle.rb +46 -22
  4. data/lib/castle/api.rb +22 -13
  5. data/lib/castle/api/approve_device.rb +25 -0
  6. data/lib/castle/api/authenticate.rb +34 -0
  7. data/lib/castle/api/end_impersonation.rb +29 -0
  8. data/lib/castle/api/get_device.rb +25 -0
  9. data/lib/castle/api/get_devices_for_user.rb +25 -0
  10. data/lib/castle/api/identify.rb +26 -0
  11. data/lib/castle/api/report_device.rb +25 -0
  12. data/lib/castle/api/review.rb +24 -0
  13. data/lib/castle/api/start_impersonation.rb +29 -0
  14. data/lib/castle/api/track.rb +26 -0
  15. data/lib/castle/client.rb +48 -62
  16. data/lib/castle/{extractors/client_id.rb → client_id/extract.rb} +2 -2
  17. data/lib/castle/commands/approve_device.rb +21 -0
  18. data/lib/castle/commands/authenticate.rb +13 -13
  19. data/lib/castle/commands/end_impersonation.rb +25 -0
  20. data/lib/castle/commands/get_device.rb +21 -0
  21. data/lib/castle/commands/get_devices_for_user.rb +21 -0
  22. data/lib/castle/commands/identify.rb +12 -13
  23. data/lib/castle/commands/report_device.rb +21 -0
  24. data/lib/castle/commands/review.rb +6 -3
  25. data/lib/castle/commands/start_impersonation.rb +25 -0
  26. data/lib/castle/commands/track.rb +12 -13
  27. data/lib/castle/configuration.rb +17 -19
  28. data/lib/castle/context/{default.rb → get_default.rb} +5 -6
  29. data/lib/castle/context/{merger.rb → merge.rb} +3 -3
  30. data/lib/castle/context/prepare.rb +18 -0
  31. data/lib/castle/context/{sanitizer.rb → sanitize.rb} +1 -1
  32. data/lib/castle/core/get_connection.rb +25 -0
  33. data/lib/castle/{api/response.rb → core/process_response.rb} +4 -2
  34. data/lib/castle/core/process_webhook.rb +20 -0
  35. data/lib/castle/core/send_request.rb +50 -0
  36. data/lib/castle/errors.rb +2 -0
  37. data/lib/castle/events.rb +1 -1
  38. data/lib/castle/failover/prepare_response.rb +23 -0
  39. data/lib/castle/failover/strategy.rb +20 -0
  40. data/lib/castle/{extractors/headers.rb → headers/extract.rb} +8 -6
  41. data/lib/castle/headers/filter.rb +37 -0
  42. data/lib/castle/headers/format.rb +24 -0
  43. data/lib/castle/{extractors/ip.rb → ip/extract.rb} +8 -7
  44. data/lib/castle/logger.rb +19 -0
  45. data/lib/castle/payload/prepare.rb +27 -0
  46. data/lib/castle/secure_mode.rb +6 -2
  47. data/lib/castle/session.rb +18 -0
  48. data/lib/castle/singleton_configuration.rb +9 -0
  49. data/lib/castle/utils/clean_invalid_chars.rb +24 -0
  50. data/lib/castle/utils/clone.rb +15 -0
  51. data/lib/castle/utils/deep_symbolize_keys.rb +45 -0
  52. data/lib/castle/utils/get_timestamp.rb +15 -0
  53. data/lib/castle/utils/{merger.rb → merge.rb} +3 -3
  54. data/lib/castle/utils/secure_compare.rb +22 -0
  55. data/lib/castle/validators/not_supported.rb +1 -0
  56. data/lib/castle/validators/present.rb +1 -0
  57. data/lib/castle/verdict.rb +13 -0
  58. data/lib/castle/version.rb +1 -1
  59. data/lib/castle/webhooks/verify.rb +43 -0
  60. data/spec/integration/rails/rails_spec.rb +33 -7
  61. data/spec/integration/rails/support/application.rb +3 -1
  62. data/spec/integration/rails/support/home_controller.rb +47 -5
  63. data/spec/lib/castle/api/approve_device_spec.rb +21 -0
  64. data/spec/lib/castle/api/authenticate_spec.rb +140 -0
  65. data/spec/lib/castle/api/end_impersonation_spec.rb +59 -0
  66. data/spec/lib/castle/api/get_device_spec.rb +19 -0
  67. data/spec/lib/castle/api/get_devices_for_user_spec.rb +19 -0
  68. data/spec/lib/castle/api/identify_spec.rb +68 -0
  69. data/spec/lib/castle/api/report_device_spec.rb +21 -0
  70. data/spec/lib/castle/{review_spec.rb → api/review_spec.rb} +3 -3
  71. data/spec/lib/castle/api/start_impersonation_spec.rb +59 -0
  72. data/spec/lib/castle/api/track_spec.rb +68 -0
  73. data/spec/lib/castle/api_spec.rb +16 -1
  74. data/spec/lib/castle/{extractors/client_id_spec.rb → client_id/extract_spec.rb} +2 -2
  75. data/spec/lib/castle/client_spec.rb +39 -21
  76. data/spec/lib/castle/commands/approve_device_spec.rb +24 -0
  77. data/spec/lib/castle/commands/authenticate_spec.rb +7 -16
  78. data/spec/lib/castle/commands/end_impersonation_spec.rb +82 -0
  79. data/spec/lib/castle/commands/get_device_spec.rb +24 -0
  80. data/spec/lib/castle/commands/get_devices_for_user_spec.rb +24 -0
  81. data/spec/lib/castle/commands/identify_spec.rb +5 -16
  82. data/spec/lib/castle/commands/report_device_spec.rb +24 -0
  83. data/spec/lib/castle/commands/review_spec.rb +1 -1
  84. data/spec/lib/castle/commands/{impersonate_spec.rb → start_impersonation_spec.rb} +7 -32
  85. data/spec/lib/castle/commands/track_spec.rb +5 -16
  86. data/spec/lib/castle/configuration_spec.rb +9 -138
  87. data/spec/lib/castle/context/{default_spec.rb → get_default_spec.rb} +1 -2
  88. data/spec/lib/castle/context/{merger_spec.rb → merge_spec.rb} +1 -1
  89. data/spec/lib/castle/context/prepare_spec.rb +44 -0
  90. data/spec/lib/castle/context/{sanitizer_spec.rb → sanitize_spec.rb} +1 -1
  91. data/spec/lib/castle/{api/connection_spec.rb → core/get_connection_spec.rb} +3 -3
  92. data/spec/lib/castle/{api/response_spec.rb → core/process_response_spec.rb} +56 -1
  93. data/spec/lib/castle/core/process_webhook_spec.rb +46 -0
  94. data/spec/lib/castle/{api/request_spec.rb → core/send_request_spec.rb} +20 -16
  95. data/spec/lib/castle/failover/strategy_spec.rb +12 -0
  96. data/spec/lib/castle/{extractors/headers_spec.rb → headers/extract_spec.rb} +7 -7
  97. data/spec/lib/castle/{headers_filter_spec.rb → headers/filter_spec.rb} +3 -3
  98. data/spec/lib/castle/headers/format_spec.rb +25 -0
  99. data/spec/lib/castle/{extractors/ip_spec.rb → ip/extract_spec.rb} +1 -1
  100. data/spec/lib/castle/logger_spec.rb +42 -0
  101. data/spec/lib/castle/payload/prepare_spec.rb +54 -0
  102. data/spec/lib/castle/{api/session_spec.rb → session_spec.rb} +6 -4
  103. data/spec/lib/castle/singleton_configuration_spec.rb +18 -0
  104. data/spec/lib/castle/utils/clean_invalid_chars_spec.rb +69 -0
  105. data/spec/lib/castle/utils/{cloner_spec.rb → clone_spec.rb} +3 -3
  106. data/spec/lib/castle/utils/deep_symbolize_keys_spec.rb +50 -0
  107. data/spec/lib/castle/utils/{timestamp_spec.rb → get_timestamp_spec.rb} +1 -1
  108. data/spec/lib/castle/utils/{merger_spec.rb → merge_spec.rb} +3 -3
  109. data/spec/lib/castle/verdict_spec.rb +9 -0
  110. data/spec/lib/castle/webhooks/verify_spec.rb +69 -0
  111. data/spec/spec_helper.rb +2 -0
  112. data/spec/support/shared_examples/configuration.rb +129 -0
  113. metadata +129 -57
  114. data/lib/castle/api/connection.rb +0 -24
  115. data/lib/castle/api/request.rb +0 -42
  116. data/lib/castle/api/session.rb +0 -20
  117. data/lib/castle/commands/impersonate.rb +0 -26
  118. data/lib/castle/failover_auth_response.rb +0 -21
  119. data/lib/castle/headers_filter.rb +0 -35
  120. data/lib/castle/headers_formatter.rb +0 -22
  121. data/lib/castle/review.rb +0 -11
  122. data/lib/castle/utils.rb +0 -55
  123. data/lib/castle/utils/cloner.rb +0 -11
  124. data/lib/castle/utils/timestamp.rb +0 -12
  125. data/spec/lib/castle/headers_formatter_spec.rb +0 -25
  126. data/spec/lib/castle/utils_spec.rb +0 -156
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: castle-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 6.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johan Brissmyr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-29 00:00:00.000000000 Z
11
+ date: 2021-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: appraisal
@@ -34,79 +34,127 @@ files:
34
34
  - lib/castle-rb.rb
35
35
  - lib/castle.rb
36
36
  - lib/castle/api.rb
37
- - lib/castle/api/connection.rb
38
- - lib/castle/api/request.rb
39
- - lib/castle/api/response.rb
40
- - lib/castle/api/session.rb
37
+ - lib/castle/api/approve_device.rb
38
+ - lib/castle/api/authenticate.rb
39
+ - lib/castle/api/end_impersonation.rb
40
+ - lib/castle/api/get_device.rb
41
+ - lib/castle/api/get_devices_for_user.rb
42
+ - lib/castle/api/identify.rb
43
+ - lib/castle/api/report_device.rb
44
+ - lib/castle/api/review.rb
45
+ - lib/castle/api/start_impersonation.rb
46
+ - lib/castle/api/track.rb
41
47
  - lib/castle/client.rb
48
+ - lib/castle/client_id/extract.rb
42
49
  - lib/castle/command.rb
50
+ - lib/castle/commands/approve_device.rb
43
51
  - lib/castle/commands/authenticate.rb
52
+ - lib/castle/commands/end_impersonation.rb
53
+ - lib/castle/commands/get_device.rb
54
+ - lib/castle/commands/get_devices_for_user.rb
44
55
  - lib/castle/commands/identify.rb
45
- - lib/castle/commands/impersonate.rb
56
+ - lib/castle/commands/report_device.rb
46
57
  - lib/castle/commands/review.rb
58
+ - lib/castle/commands/start_impersonation.rb
47
59
  - lib/castle/commands/track.rb
48
60
  - lib/castle/configuration.rb
49
- - lib/castle/context/default.rb
50
- - lib/castle/context/merger.rb
51
- - lib/castle/context/sanitizer.rb
61
+ - lib/castle/context/get_default.rb
62
+ - lib/castle/context/merge.rb
63
+ - lib/castle/context/prepare.rb
64
+ - lib/castle/context/sanitize.rb
65
+ - lib/castle/core/get_connection.rb
66
+ - lib/castle/core/process_response.rb
67
+ - lib/castle/core/process_webhook.rb
68
+ - lib/castle/core/send_request.rb
52
69
  - lib/castle/errors.rb
53
70
  - lib/castle/events.rb
54
- - lib/castle/extractors/client_id.rb
55
- - lib/castle/extractors/headers.rb
56
- - lib/castle/extractors/ip.rb
57
- - lib/castle/failover_auth_response.rb
58
- - lib/castle/headers_filter.rb
59
- - lib/castle/headers_formatter.rb
60
- - lib/castle/review.rb
71
+ - lib/castle/failover/prepare_response.rb
72
+ - lib/castle/failover/strategy.rb
73
+ - lib/castle/headers/extract.rb
74
+ - lib/castle/headers/filter.rb
75
+ - lib/castle/headers/format.rb
76
+ - lib/castle/ip/extract.rb
77
+ - lib/castle/logger.rb
78
+ - lib/castle/payload/prepare.rb
61
79
  - lib/castle/secure_mode.rb
80
+ - lib/castle/session.rb
81
+ - lib/castle/singleton_configuration.rb
62
82
  - lib/castle/support/hanami.rb
63
83
  - lib/castle/support/padrino.rb
64
84
  - lib/castle/support/rails.rb
65
85
  - lib/castle/support/sinatra.rb
66
- - lib/castle/utils.rb
67
- - lib/castle/utils/cloner.rb
68
- - lib/castle/utils/merger.rb
69
- - lib/castle/utils/timestamp.rb
86
+ - lib/castle/utils/clean_invalid_chars.rb
87
+ - lib/castle/utils/clone.rb
88
+ - lib/castle/utils/deep_symbolize_keys.rb
89
+ - lib/castle/utils/get_timestamp.rb
90
+ - lib/castle/utils/merge.rb
91
+ - lib/castle/utils/secure_compare.rb
70
92
  - lib/castle/validators/not_supported.rb
71
93
  - lib/castle/validators/present.rb
94
+ - lib/castle/verdict.rb
72
95
  - lib/castle/version.rb
96
+ - lib/castle/webhooks/verify.rb
73
97
  - spec/integration/rails/rails_spec.rb
74
98
  - spec/integration/rails/support/all.rb
75
99
  - spec/integration/rails/support/application.rb
76
100
  - spec/integration/rails/support/home_controller.rb
77
- - spec/lib/castle/api/connection_spec.rb
78
- - spec/lib/castle/api/request_spec.rb
79
- - spec/lib/castle/api/response_spec.rb
80
- - spec/lib/castle/api/session_spec.rb
101
+ - spec/lib/castle/api/approve_device_spec.rb
102
+ - spec/lib/castle/api/authenticate_spec.rb
103
+ - spec/lib/castle/api/end_impersonation_spec.rb
104
+ - spec/lib/castle/api/get_device_spec.rb
105
+ - spec/lib/castle/api/get_devices_for_user_spec.rb
106
+ - spec/lib/castle/api/identify_spec.rb
107
+ - spec/lib/castle/api/report_device_spec.rb
108
+ - spec/lib/castle/api/review_spec.rb
109
+ - spec/lib/castle/api/start_impersonation_spec.rb
110
+ - spec/lib/castle/api/track_spec.rb
81
111
  - spec/lib/castle/api_spec.rb
112
+ - spec/lib/castle/client_id/extract_spec.rb
82
113
  - spec/lib/castle/client_spec.rb
83
114
  - spec/lib/castle/command_spec.rb
115
+ - spec/lib/castle/commands/approve_device_spec.rb
84
116
  - spec/lib/castle/commands/authenticate_spec.rb
117
+ - spec/lib/castle/commands/end_impersonation_spec.rb
118
+ - spec/lib/castle/commands/get_device_spec.rb
119
+ - spec/lib/castle/commands/get_devices_for_user_spec.rb
85
120
  - spec/lib/castle/commands/identify_spec.rb
86
- - spec/lib/castle/commands/impersonate_spec.rb
121
+ - spec/lib/castle/commands/report_device_spec.rb
87
122
  - spec/lib/castle/commands/review_spec.rb
123
+ - spec/lib/castle/commands/start_impersonation_spec.rb
88
124
  - spec/lib/castle/commands/track_spec.rb
89
125
  - spec/lib/castle/configuration_spec.rb
90
- - spec/lib/castle/context/default_spec.rb
91
- - spec/lib/castle/context/merger_spec.rb
92
- - spec/lib/castle/context/sanitizer_spec.rb
126
+ - spec/lib/castle/context/get_default_spec.rb
127
+ - spec/lib/castle/context/merge_spec.rb
128
+ - spec/lib/castle/context/prepare_spec.rb
129
+ - spec/lib/castle/context/sanitize_spec.rb
130
+ - spec/lib/castle/core/get_connection_spec.rb
131
+ - spec/lib/castle/core/process_response_spec.rb
132
+ - spec/lib/castle/core/process_webhook_spec.rb
133
+ - spec/lib/castle/core/send_request_spec.rb
93
134
  - spec/lib/castle/events_spec.rb
94
- - spec/lib/castle/extractors/client_id_spec.rb
95
- - spec/lib/castle/extractors/headers_spec.rb
96
- - spec/lib/castle/extractors/ip_spec.rb
97
- - spec/lib/castle/headers_filter_spec.rb
98
- - spec/lib/castle/headers_formatter_spec.rb
99
- - spec/lib/castle/review_spec.rb
135
+ - spec/lib/castle/failover/strategy_spec.rb
136
+ - spec/lib/castle/headers/extract_spec.rb
137
+ - spec/lib/castle/headers/filter_spec.rb
138
+ - spec/lib/castle/headers/format_spec.rb
139
+ - spec/lib/castle/ip/extract_spec.rb
140
+ - spec/lib/castle/logger_spec.rb
141
+ - spec/lib/castle/payload/prepare_spec.rb
100
142
  - spec/lib/castle/secure_mode_spec.rb
101
- - spec/lib/castle/utils/cloner_spec.rb
102
- - spec/lib/castle/utils/merger_spec.rb
103
- - spec/lib/castle/utils/timestamp_spec.rb
104
- - spec/lib/castle/utils_spec.rb
143
+ - spec/lib/castle/session_spec.rb
144
+ - spec/lib/castle/singleton_configuration_spec.rb
145
+ - spec/lib/castle/utils/clean_invalid_chars_spec.rb
146
+ - spec/lib/castle/utils/clone_spec.rb
147
+ - spec/lib/castle/utils/deep_symbolize_keys_spec.rb
148
+ - spec/lib/castle/utils/get_timestamp_spec.rb
149
+ - spec/lib/castle/utils/merge_spec.rb
105
150
  - spec/lib/castle/validators/not_supported_spec.rb
106
151
  - spec/lib/castle/validators/present_spec.rb
152
+ - spec/lib/castle/verdict_spec.rb
107
153
  - spec/lib/castle/version_spec.rb
154
+ - spec/lib/castle/webhooks/verify_spec.rb
108
155
  - spec/lib/castle_spec.rb
109
156
  - spec/spec_helper.rb
157
+ - spec/support/shared_examples/configuration.rb
110
158
  homepage: https://castle.io
111
159
  licenses:
112
160
  - MIT
@@ -136,35 +184,59 @@ test_files:
136
184
  - spec/integration/rails/support/all.rb
137
185
  - spec/integration/rails/support/home_controller.rb
138
186
  - spec/integration/rails/rails_spec.rb
187
+ - spec/support/shared_examples/configuration.rb
139
188
  - spec/lib/castle_spec.rb
140
- - spec/lib/castle/review_spec.rb
141
- - spec/lib/castle/headers_filter_spec.rb
189
+ - spec/lib/castle/session_spec.rb
190
+ - spec/lib/castle/verdict_spec.rb
142
191
  - spec/lib/castle/client_spec.rb
143
- - spec/lib/castle/context/default_spec.rb
144
- - spec/lib/castle/context/merger_spec.rb
145
- - spec/lib/castle/context/sanitizer_spec.rb
192
+ - spec/lib/castle/context/get_default_spec.rb
193
+ - spec/lib/castle/context/prepare_spec.rb
194
+ - spec/lib/castle/context/sanitize_spec.rb
195
+ - spec/lib/castle/context/merge_spec.rb
196
+ - spec/lib/castle/core/get_connection_spec.rb
197
+ - spec/lib/castle/core/process_webhook_spec.rb
198
+ - spec/lib/castle/core/send_request_spec.rb
199
+ - spec/lib/castle/core/process_response_spec.rb
146
200
  - spec/lib/castle/api_spec.rb
201
+ - spec/lib/castle/logger_spec.rb
202
+ - spec/lib/castle/client_id/extract_spec.rb
147
203
  - spec/lib/castle/configuration_spec.rb
148
204
  - spec/lib/castle/version_spec.rb
149
- - spec/lib/castle/utils/cloner_spec.rb
150
- - spec/lib/castle/utils/timestamp_spec.rb
151
- - spec/lib/castle/utils/merger_spec.rb
205
+ - spec/lib/castle/payload/prepare_spec.rb
206
+ - spec/lib/castle/utils/clean_invalid_chars_spec.rb
207
+ - spec/lib/castle/utils/deep_symbolize_keys_spec.rb
208
+ - spec/lib/castle/utils/get_timestamp_spec.rb
209
+ - spec/lib/castle/utils/clone_spec.rb
210
+ - spec/lib/castle/utils/merge_spec.rb
152
211
  - spec/lib/castle/command_spec.rb
153
- - spec/lib/castle/headers_formatter_spec.rb
154
- - spec/lib/castle/api/session_spec.rb
155
- - spec/lib/castle/api/request_spec.rb
156
- - spec/lib/castle/api/connection_spec.rb
157
- - spec/lib/castle/api/response_spec.rb
212
+ - spec/lib/castle/headers/format_spec.rb
213
+ - spec/lib/castle/headers/extract_spec.rb
214
+ - spec/lib/castle/headers/filter_spec.rb
215
+ - spec/lib/castle/api/review_spec.rb
216
+ - spec/lib/castle/api/report_device_spec.rb
217
+ - spec/lib/castle/api/authenticate_spec.rb
218
+ - spec/lib/castle/api/track_spec.rb
219
+ - spec/lib/castle/api/get_device_spec.rb
220
+ - spec/lib/castle/api/start_impersonation_spec.rb
221
+ - spec/lib/castle/api/approve_device_spec.rb
222
+ - spec/lib/castle/api/get_devices_for_user_spec.rb
223
+ - spec/lib/castle/api/end_impersonation_spec.rb
224
+ - spec/lib/castle/api/identify_spec.rb
158
225
  - spec/lib/castle/commands/review_spec.rb
226
+ - spec/lib/castle/commands/report_device_spec.rb
159
227
  - spec/lib/castle/commands/authenticate_spec.rb
160
228
  - spec/lib/castle/commands/track_spec.rb
161
- - spec/lib/castle/commands/impersonate_spec.rb
229
+ - spec/lib/castle/commands/get_device_spec.rb
230
+ - spec/lib/castle/commands/start_impersonation_spec.rb
231
+ - spec/lib/castle/commands/approve_device_spec.rb
232
+ - spec/lib/castle/commands/get_devices_for_user_spec.rb
233
+ - spec/lib/castle/commands/end_impersonation_spec.rb
162
234
  - spec/lib/castle/commands/identify_spec.rb
163
235
  - spec/lib/castle/validators/not_supported_spec.rb
164
236
  - spec/lib/castle/validators/present_spec.rb
165
- - spec/lib/castle/extractors/ip_spec.rb
166
- - spec/lib/castle/extractors/headers_spec.rb
167
- - spec/lib/castle/extractors/client_id_spec.rb
168
- - spec/lib/castle/utils_spec.rb
237
+ - spec/lib/castle/webhooks/verify_spec.rb
238
+ - spec/lib/castle/ip/extract_spec.rb
239
+ - spec/lib/castle/failover/strategy_spec.rb
169
240
  - spec/lib/castle/secure_mode_spec.rb
241
+ - spec/lib/castle/singleton_configuration_spec.rb
170
242
  - spec/lib/castle/events_spec.rb
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Castle
4
- module API
5
- # this module returns a new configured Net::HTTP object
6
- module Connection
7
- HTTPS_SCHEME = 'https'
8
-
9
- class << self
10
- def call
11
- http = Net::HTTP.new(Castle.config.url.host, Castle.config.url.port)
12
- http.read_timeout = Castle.config.request_timeout / 1000.0
13
-
14
- if Castle.config.url.scheme == HTTPS_SCHEME
15
- http.use_ssl = true
16
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
17
- end
18
-
19
- http
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Castle
4
- module API
5
- # this class is responsible for making requests to api
6
- module Request
7
- # Default headers that we add to passed ones
8
- DEFAULT_HEADERS = {
9
- 'Content-Type' => 'application/json'
10
- }.freeze
11
-
12
- private_constant :DEFAULT_HEADERS
13
-
14
- class << self
15
- def call(command, api_secret, headers, http = nil)
16
- (http || Castle::API::Connection.call).request(
17
- build(
18
- command,
19
- headers.merge(DEFAULT_HEADERS),
20
- api_secret
21
- )
22
- )
23
- end
24
-
25
- def build(command, headers, api_secret)
26
- request_obj = Net::HTTP.const_get(
27
- command.method.to_s.capitalize
28
- ).new("#{Castle.config.url.path}/#{command.path}", headers)
29
-
30
- unless command.method == :get
31
- request_obj.body = ::Castle::Utils.replace_invalid_characters(
32
- command.data
33
- ).to_json
34
- end
35
-
36
- request_obj.basic_auth('', api_secret)
37
- request_obj
38
- end
39
- end
40
- end
41
- end
42
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Castle
4
- module API
5
- # this module uses the Connection object
6
- # and provides start method for persistent connection usage
7
- # when there is a need of sending multiple requests at once
8
- module Session
9
- HTTPS_SCHEME = 'https'
10
-
11
- class << self
12
- def call(&block)
13
- return unless block_given?
14
-
15
- Connection.call.start(&block)
16
- end
17
- end
18
- end
19
- end
20
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Castle
4
- module Commands
5
- # builder for impersonate command
6
- class Impersonate
7
- def initialize(context)
8
- @context = context
9
- end
10
-
11
- def build(options = {})
12
- Castle::Validators::Present.call(options, %i[user_id])
13
- context = Castle::Context::Merger.call(@context, options[:context])
14
- context = Castle::Context::Sanitizer.call(context)
15
-
16
- Castle::Validators::Present.call(context, %i[user_agent ip])
17
-
18
- Castle::Command.new(
19
- 'impersonate',
20
- options.merge(context: context, sent_at: Castle::Utils::Timestamp.call),
21
- options[:reset] ? :delete : :post
22
- )
23
- end
24
- end
25
- end
26
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Castle
4
- # generate failover authentication response
5
- class FailoverAuthResponse
6
- def initialize(user_id, strategy: Castle.config.failover_strategy, reason:)
7
- @strategy = strategy
8
- @reason = reason
9
- @user_id = user_id
10
- end
11
-
12
- def generate
13
- {
14
- action: @strategy.to_s,
15
- user_id: @user_id,
16
- failover: true,
17
- failover_reason: @reason
18
- }
19
- end
20
- end
21
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Castle
4
- # used for preparing valuable headers list
5
- class HeadersFilter
6
- # headers filter
7
- # HTTP_ - this is how Rack prefixes incoming HTTP headers
8
- # CONTENT_LENGTH - for responses without Content-Length or Transfer-Encoding header
9
- # REMOTE_ADDR - ip address header returned by web server
10
- VALUABLE_HEADERS = /^
11
- HTTP(?:_|-).*|
12
- CONTENT(?:_|-)LENGTH|
13
- REMOTE(?:_|-)ADDR
14
- $/xi.freeze
15
-
16
- private_constant :VALUABLE_HEADERS
17
-
18
- # @param request [Rack::Request]
19
- def initialize(request)
20
- @request_env = request.env
21
- @formatter = HeadersFormatter
22
- end
23
-
24
- # Serialize HTTP headers
25
- # @return [Hash]
26
- def call
27
- @request_env.keys.each_with_object({}) do |header_name, acc|
28
- next unless header_name.match(VALUABLE_HEADERS)
29
-
30
- formatted_name = @formatter.call(header_name)
31
- acc[formatted_name] = @request_env[header_name]
32
- end
33
- end
34
- end
35
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Castle
4
- # formats header name
5
- class HeadersFormatter
6
- class << self
7
- # @param header [String]
8
- # @return [String]
9
- def call(header)
10
- format(header.to_s.gsub(/^HTTP(?:_|-)/i, ''))
11
- end
12
-
13
- private
14
-
15
- # @param header [String]
16
- # @return [String]
17
- def format(header)
18
- header.split(/_|-/).map(&:capitalize).join('-')
19
- end
20
- end
21
- end
22
- end