bible270 1.17.2 → 1.18.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/CHANGELOG.md +15 -1
- data/README.md +41 -0
- data/app/controllers/bible270/admin_progress_syncs_controller.rb +140 -0
- data/app/controllers/bible270/progress_sync_controller.rb +153 -0
- data/app/jobs/bible270/progress_sync_job.rb +31 -0
- data/app/models/bible270/checkoff.rb +5 -0
- data/app/models/bible270/participation.rb +10 -0
- data/app/models/bible270/progress_sync_delivery.rb +51 -0
- data/app/models/bible270/progress_sync_link.rb +36 -0
- data/app/models/bible270/reader.rb +1 -0
- data/app/views/bible270/admin/_progress_sync.html.erb +92 -0
- data/app/views/bible270/admin/show.html.erb +2 -0
- data/app/views/bible270/shared/_styles.html.erb +5 -0
- data/config/routes.rb +9 -1
- data/db/migrate/20260101000028_create_bible270_progress_sync.rb +48 -0
- data/lib/bible270/configuration.rb +9 -0
- data/lib/bible270/migration_reconciler.rb +133 -0
- data/lib/bible270/progress_sync/authentication.rb +61 -0
- data/lib/bible270/progress_sync/client.rb +147 -0
- data/lib/bible270/progress_sync.rb +406 -0
- data/lib/bible270/version.rb +1 -1
- data/lib/bible270.rb +1 -0
- data/lib/generators/bible270/install/templates/bible270.rb.tt +12 -0
- data/lib/tasks/bible270_progress_sync.rake +13 -0
- metadata +12 -1
|
@@ -199,6 +199,8 @@
|
|
|
199
199
|
</div>
|
|
200
200
|
|
|
201
201
|
|
|
202
|
+
<%= render 'progress_sync' %>
|
|
203
|
+
|
|
202
204
|
<div class="b270-panel">
|
|
203
205
|
<%= render "bible270/shared/interaction_status" %>
|
|
204
206
|
<%= render "bible270/shared/reflections", comments: @comments, moderate: true, editable: true, actions: true %>
|
|
@@ -516,6 +516,11 @@
|
|
|
516
516
|
.b270-scripture-panel-header>.b270-subhead,.b270 .b270-scripture-panel-header>.b270-btn{margin:0}
|
|
517
517
|
.b270-scripture-panel-header>.b270-subhead{min-width:0;overflow-wrap:anywhere}
|
|
518
518
|
.b270 .b270-scripture-panel-header>.b270-btn{flex-shrink:0;padding:6px 10px;font-size:12.5px;white-space:nowrap}
|
|
519
|
+
.b270-progress-sync-links{list-style:none;padding:0;margin:12px 0 0}
|
|
520
|
+
.b270-progress-sync-link{padding:12px 0;border-top:1px solid var(--line-soft);overflow-wrap:anywhere}
|
|
521
|
+
.b270-progress-sync-link p,.b270-progress-sync-candidate p{margin:0 0 8px}
|
|
522
|
+
.b270-progress-sync-actions{display:flex;align-items:center;flex-wrap:wrap;gap:8px}
|
|
523
|
+
.b270-progress-sync-actions form,.b270-progress-sync-actions .b270-btn{margin:0}
|
|
519
524
|
.b270-scripture-actions{display:flex;align-items:center;gap:10px;margin:0;white-space:nowrap}
|
|
520
525
|
.b270-scripture-actions>form{margin:0}
|
|
521
526
|
.b270-scripture-actions>a,.b270-scripture-actions .b270-linkbtn{margin:0;font-size:12.5px;color:var(--oxblood)}
|
data/config/routes.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
# Rails routes are intentionally kept in one cohesive DSL block.
|
|
4
|
-
Bible270::Engine.routes.draw do
|
|
4
|
+
Bible270::Engine.routes.draw do # rubocop:disable Metrics/BlockLength
|
|
5
5
|
root to: 'days#index'
|
|
6
6
|
get 'scripture-frame', to: 'scripture_frames#show', as: :scripture_frame, defaults: { format: 'json' }
|
|
7
7
|
|
|
@@ -19,7 +19,15 @@ Bible270::Engine.routes.draw do
|
|
|
19
19
|
patch 'comments/:id', to: 'comments#update'
|
|
20
20
|
delete 'comments/:id', to: 'comments#destroy', as: :comment
|
|
21
21
|
|
|
22
|
+
# Server-to-server requests use signed instance credentials, never browser sessions.
|
|
23
|
+
post 'progress-sync/lookup', to: 'progress_sync#lookup', defaults: { format: :json }
|
|
24
|
+
post 'progress-sync/complete', to: 'progress_sync#complete', defaults: { format: :json }
|
|
25
|
+
|
|
22
26
|
# Admin panel — configured site-admins and readers granted admin access.
|
|
27
|
+
post 'admin/readers/:reader_id/progress-sync/lookup', to: 'admin_progress_syncs#lookup', as: :lookup_admin_reader_progress_sync
|
|
28
|
+
post 'admin/readers/:reader_id/progress-sync', to: 'admin_progress_syncs#create', as: :admin_reader_progress_syncs
|
|
29
|
+
post 'admin/readers/:reader_id/progress-sync/:id/copy', to: 'admin_progress_syncs#copy', as: :copy_admin_reader_progress_sync
|
|
30
|
+
delete 'admin/readers/:reader_id/progress-sync/:id', to: 'admin_progress_syncs#destroy', as: :admin_reader_progress_sync
|
|
23
31
|
patch 'admin/scripture-frame-defaults/:id', to: 'admin_scripture_frames#update_default', as: :admin_default_scripture_frame
|
|
24
32
|
resources :admin_scripture_frames, path: 'admin/scripture-frames', only: %i[index new create edit update destroy]
|
|
25
33
|
get 'admin', to: 'admin#index', as: :admin
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class CreateBible270ProgressSync < ActiveRecord::Migration[7.0]
|
|
4
|
+
def change
|
|
5
|
+
add_column :bible270_participations, :progress_sync_generation, :integer, null: false, default: 0
|
|
6
|
+
|
|
7
|
+
create_table :bible270_progress_sync_links do |t|
|
|
8
|
+
t.references :participation, null: false,
|
|
9
|
+
foreign_key: { to_table: :bible270_participations, on_delete: :cascade },
|
|
10
|
+
index: { name: 'idx_b270_sync_links_participation' }
|
|
11
|
+
t.integer :source_generation, null: false
|
|
12
|
+
t.string :destination, null: false
|
|
13
|
+
t.string :destination_url, null: false
|
|
14
|
+
t.bigint :remote_reader_id, null: false
|
|
15
|
+
t.bigint :remote_participation_id, null: false
|
|
16
|
+
t.integer :remote_generation, null: false
|
|
17
|
+
t.string :remote_name, null: false
|
|
18
|
+
t.string :email, null: false
|
|
19
|
+
t.string :plan_fingerprint, null: false
|
|
20
|
+
t.string :state, null: false, default: 'active'
|
|
21
|
+
t.datetime :last_synced_at
|
|
22
|
+
t.text :last_error
|
|
23
|
+
t.integer :last_added
|
|
24
|
+
t.integer :last_existing
|
|
25
|
+
t.timestamps
|
|
26
|
+
end
|
|
27
|
+
# Paused links retain their immutable target and delivery history. Explicit
|
|
28
|
+
# relinking creates a new row; old outbox entries can never change recipients.
|
|
29
|
+
add_index :bible270_progress_sync_links, %i[participation_id destination], unique: true,
|
|
30
|
+
where: "state = 'active'", name: 'idx_b270_sync_links_active_destination'
|
|
31
|
+
|
|
32
|
+
create_table :bible270_progress_sync_deliveries do |t|
|
|
33
|
+
t.references :progress_sync_link, null: false,
|
|
34
|
+
foreign_key: { to_table: :bible270_progress_sync_links, on_delete: :cascade },
|
|
35
|
+
index: { name: 'idx_b270_sync_deliveries_link' }
|
|
36
|
+
t.json :readings, null: false
|
|
37
|
+
t.integer :attempts, null: false, default: 0
|
|
38
|
+
t.datetime :next_attempt_at, null: false
|
|
39
|
+
t.datetime :locked_until
|
|
40
|
+
t.string :claim_token
|
|
41
|
+
t.datetime :delivered_at
|
|
42
|
+
t.text :last_error
|
|
43
|
+
t.timestamps
|
|
44
|
+
end
|
|
45
|
+
add_index :bible270_progress_sync_deliveries, %i[delivered_at next_attempt_at locked_until],
|
|
46
|
+
name: 'idx_b270_sync_deliveries_due'
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -287,6 +287,11 @@ module Bible270
|
|
|
287
287
|
# are stored separately and supplement either the bundled or host catalog.
|
|
288
288
|
attr_accessor :scripture_frames_path
|
|
289
289
|
|
|
290
|
+
# Explicit trust between installations; credentials stay in host configuration.
|
|
291
|
+
attr_accessor :progress_sync_instance_id, :progress_sync_destinations, :progress_sync_sources
|
|
292
|
+
# Enable only with a persistent Active Job adapter and a running worker.
|
|
293
|
+
attr_accessor :progress_sync_deliver_later
|
|
294
|
+
|
|
290
295
|
# Who to email when someone new joins. Three forms:
|
|
291
296
|
#
|
|
292
297
|
# config.registration_notice_emails = %w[andrew@example.org] # an explicit list
|
|
@@ -373,6 +378,10 @@ module Bible270
|
|
|
373
378
|
@mention_notifications = true
|
|
374
379
|
@header_mark = nil
|
|
375
380
|
@scripture_frames_path = nil
|
|
381
|
+
@progress_sync_instance_id = nil
|
|
382
|
+
@progress_sync_destinations = {}
|
|
383
|
+
@progress_sync_sources = {}
|
|
384
|
+
@progress_sync_deliver_later = false
|
|
376
385
|
@remember_for = 365 * 24 * 60 * 60
|
|
377
386
|
@require_sign_in_to_participate = true
|
|
378
387
|
self.mount_at = '/daily-bread'
|
|
@@ -37,6 +37,7 @@ module Bible270
|
|
|
37
37
|
'AddAdminToBible270Readers' => :check_admin,
|
|
38
38
|
'AddRevertToBible270PlanRuns' => :check_run_revert,
|
|
39
39
|
'CreateBible270ScriptureFrames' => :check_scripture_frames,
|
|
40
|
+
'CreateBible270ProgressSync' => :check_progress_sync,
|
|
40
41
|
|
|
41
42
|
'CreateActiveStorageTables' => :check_active_storage
|
|
42
43
|
}.freeze
|
|
@@ -297,6 +298,15 @@ module Bible270
|
|
|
297
298
|
enabled: { type: :boolean, null: false, default: true })
|
|
298
299
|
end
|
|
299
300
|
|
|
301
|
+
def check_progress_sync
|
|
302
|
+
missing_progress_sync_columns(
|
|
303
|
+
:bible270_participations,
|
|
304
|
+
progress_sync_generation: { type: :integer, default: 0 }
|
|
305
|
+
) + missing_progress_sync_columns(:bible270_progress_sync_links, progress_sync_link_columns) +
|
|
306
|
+
missing_progress_sync_columns(:bible270_progress_sync_deliveries, progress_sync_delivery_columns) +
|
|
307
|
+
progress_sync_relationships_missing + progress_sync_link_indexes_missing
|
|
308
|
+
end
|
|
309
|
+
|
|
300
310
|
def check_admin
|
|
301
311
|
table_missing(:bible270_readers, columns: %i[admin]) +
|
|
302
312
|
missing_column_definitions(:bible270_readers, admin: { type: :boolean, null: false, default: false })
|
|
@@ -339,6 +349,125 @@ module Bible270
|
|
|
339
349
|
|
|
340
350
|
attr_reader :connection, :migration_context
|
|
341
351
|
|
|
352
|
+
def progress_sync_link_columns
|
|
353
|
+
{
|
|
354
|
+
id: progress_sync_id_definition,
|
|
355
|
+
participation_id: progress_sync_id_definition,
|
|
356
|
+
source_generation: { type: :integer },
|
|
357
|
+
destination: { type: :string },
|
|
358
|
+
destination_url: { type: :string },
|
|
359
|
+
remote_reader_id: progress_sync_id_definition,
|
|
360
|
+
remote_participation_id: progress_sync_id_definition,
|
|
361
|
+
remote_generation: { type: :integer },
|
|
362
|
+
remote_name: { type: :string },
|
|
363
|
+
email: { type: :string },
|
|
364
|
+
plan_fingerprint: { type: :string },
|
|
365
|
+
state: { type: :string, default: 'active' },
|
|
366
|
+
last_synced_at: { type: :datetime, null: true },
|
|
367
|
+
last_error: { type: :text, null: true },
|
|
368
|
+
last_added: { type: :integer, null: true },
|
|
369
|
+
last_existing: { type: :integer, null: true },
|
|
370
|
+
created_at: { type: :datetime },
|
|
371
|
+
updated_at: { type: :datetime }
|
|
372
|
+
}
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
def progress_sync_delivery_columns
|
|
376
|
+
{
|
|
377
|
+
id: progress_sync_id_definition,
|
|
378
|
+
progress_sync_link_id: progress_sync_id_definition,
|
|
379
|
+
readings: { type: :json },
|
|
380
|
+
attempts: { type: :integer, default: 0 },
|
|
381
|
+
next_attempt_at: { type: :datetime },
|
|
382
|
+
locked_until: { type: :datetime, null: true },
|
|
383
|
+
claim_token: { type: :string, null: true },
|
|
384
|
+
delivered_at: { type: :datetime, null: true },
|
|
385
|
+
last_error: { type: :text, null: true },
|
|
386
|
+
created_at: { type: :datetime },
|
|
387
|
+
updated_at: { type: :datetime }
|
|
388
|
+
}
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
def progress_sync_id_definition
|
|
392
|
+
# SQLite uses 64-bit integers for both INTEGER references and bigint IDs.
|
|
393
|
+
{ type: :integer, limit: connection.adapter_name == 'SQLite' ? nil : 8 }
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
def missing_progress_sync_columns(table, definitions)
|
|
397
|
+
definitions = definitions.to_h do |name, attributes|
|
|
398
|
+
definition = { null: false, default: nil, default_function: nil }.merge(attributes)
|
|
399
|
+
# PostgreSQL's generated primary key uses a sequence default.
|
|
400
|
+
definition.delete(:default_function) if name == :id
|
|
401
|
+
[name, definition]
|
|
402
|
+
end
|
|
403
|
+
table_missing(table, columns: definitions.keys) + missing_column_definitions(table, definitions)
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
def progress_sync_relationships_missing
|
|
407
|
+
table_missing(
|
|
408
|
+
:bible270_progress_sync_links,
|
|
409
|
+
columns: [],
|
|
410
|
+
indexes: [{ columns: %i[participation_id] }],
|
|
411
|
+
foreign_keys: [{ column: :participation_id, to_table: :bible270_participations, primary_key: :id, on_delete: :cascade }]
|
|
412
|
+
) + table_missing(
|
|
413
|
+
:bible270_progress_sync_deliveries,
|
|
414
|
+
columns: [],
|
|
415
|
+
indexes: [{ columns: %i[progress_sync_link_id] }, { columns: %i[delivered_at next_attempt_at locked_until] }],
|
|
416
|
+
foreign_keys: [{ column: :progress_sync_link_id, to_table: :bible270_progress_sync_links, primary_key: :id, on_delete: :cascade }]
|
|
417
|
+
)
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
def progress_sync_link_indexes_missing
|
|
421
|
+
table = :bible270_progress_sync_links
|
|
422
|
+
return [] unless connection.data_source_exists?(table)
|
|
423
|
+
|
|
424
|
+
columns = %w[participation_id destination]
|
|
425
|
+
indexes = connection.indexes(table)
|
|
426
|
+
missing = []
|
|
427
|
+
unless indexes.any? { |index| index.columns == columns && index.unique && active_destination_predicate?(index.where) }
|
|
428
|
+
missing << "unique index #{table}(participation_id, destination) where state = 'active'"
|
|
429
|
+
end
|
|
430
|
+
if indexes.any? { |index| index.unique && index.where.nil? && index.columns.is_a?(Array) && index.columns.sort == columns.sort }
|
|
431
|
+
missing << "conflicting unconditional unique index #{table}(participation_id, destination)"
|
|
432
|
+
end
|
|
433
|
+
missing
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
def active_destination_predicate?(predicate)
|
|
437
|
+
operands = unwrap_index_expression(predicate.to_s.strip).split('=', -1)
|
|
438
|
+
return false unless operands.length == 2
|
|
439
|
+
|
|
440
|
+
['state', '"state"'].include?(unwrap_index_text_operand(operands[0])) &&
|
|
441
|
+
unwrap_index_text_operand(operands[1]) == "'active'"
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
def unwrap_index_text_operand(expression)
|
|
445
|
+
loop do
|
|
446
|
+
expression = unwrap_index_expression(expression.strip)
|
|
447
|
+
uncast = expression.sub(%r{\s*::\s*(?:text|varchar|character\s+varying)\z}, '')
|
|
448
|
+
return expression if expression == uncast
|
|
449
|
+
|
|
450
|
+
expression = uncast
|
|
451
|
+
end
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
# Only peel enclosing pairs, never parentheses inside an identifier/literal
|
|
455
|
+
# or a broader expression. Operand matching then allows just state = 'active'.
|
|
456
|
+
def unwrap_index_expression(expression)
|
|
457
|
+
while expression.start_with?('(') && expression.end_with?(')')
|
|
458
|
+
depth = 0
|
|
459
|
+
closing = expression.each_char.with_index.find do |character, _index|
|
|
460
|
+
depth += 1 if character == '('
|
|
461
|
+
depth -= 1 if character == ')'
|
|
462
|
+
depth.zero?
|
|
463
|
+
end
|
|
464
|
+
break unless closing && closing.last == expression.length - 1
|
|
465
|
+
|
|
466
|
+
expression = expression[1...-1].strip
|
|
467
|
+
end
|
|
468
|
+
expression
|
|
469
|
+
end
|
|
470
|
+
|
|
342
471
|
def table_missing(table, columns:, indexes: [], foreign_keys: [])
|
|
343
472
|
return ["table #{table}"] unless connection.data_source_exists?(table)
|
|
344
473
|
|
|
@@ -361,6 +490,9 @@ module Bible270
|
|
|
361
490
|
definitions.fetch(column.name.to_sym, {}).filter_map do |attribute, expected|
|
|
362
491
|
actual = column.public_send(attribute)
|
|
363
492
|
actual = ActiveModel::Type::Boolean.new.cast(actual) if attribute == :default && column.type == :boolean
|
|
493
|
+
if attribute == :default && column.type == :integer && actual.is_a?(String) && actual.match?(%r{\A[+-]?\d+\z})
|
|
494
|
+
actual = Integer(actual, 10)
|
|
495
|
+
end
|
|
364
496
|
"column #{table}.#{column.name} #{attribute} #{expected}" unless actual == expected
|
|
365
497
|
end
|
|
366
498
|
end
|
|
@@ -394,6 +526,7 @@ module Bible270
|
|
|
394
526
|
def matching_foreign_key?(foreign_key, expected)
|
|
395
527
|
foreign_key.column.to_s == expected.fetch(:column).to_s &&
|
|
396
528
|
foreign_key.to_table.to_s == expected.fetch(:to_table).to_s &&
|
|
529
|
+
(!expected.key?(:primary_key) || foreign_key.primary_key.to_s == expected[:primary_key].to_s) &&
|
|
397
530
|
(!expected.key?(:on_delete) || foreign_key.on_delete == expected[:on_delete])
|
|
398
531
|
end
|
|
399
532
|
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'openssl'
|
|
4
|
+
require 'digest'
|
|
5
|
+
require 'active_support/security_utils'
|
|
6
|
+
|
|
7
|
+
module Bible270
|
|
8
|
+
module ProgressSync
|
|
9
|
+
module Authentication
|
|
10
|
+
VERSION = '1'
|
|
11
|
+
MAX_BYTES = 256 * 1024
|
|
12
|
+
TTL = 300
|
|
13
|
+
SOURCE_HEADER = 'X-Bible270-Sync-Source'
|
|
14
|
+
TIMESTAMP_HEADER = 'X-Bible270-Sync-Timestamp'
|
|
15
|
+
SIGNATURE_HEADER = 'X-Bible270-Sync-Signature'
|
|
16
|
+
VERSION_HEADER = 'X-Bible270-Sync-Version'
|
|
17
|
+
|
|
18
|
+
module_function
|
|
19
|
+
|
|
20
|
+
def valid_source?(source)
|
|
21
|
+
source.is_a?(String) && source.match?(%r{\A[A-Za-z0-9._:-]{1,128}\z})
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def signature(secret:, source:, timestamp:, method:, path:, body:) # rubocop:disable Metrics/ParameterLists
|
|
25
|
+
message = [VERSION, source, timestamp, method.upcase, path, Digest::SHA256.hexdigest(body)].join("\n")
|
|
26
|
+
OpenSSL::HMAC.hexdigest('SHA256', secret, message)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def headers(secret:, source:, method:, path:, body:, timestamp: Time.now.to_i.to_s) # rubocop:disable Metrics/ParameterLists
|
|
30
|
+
{
|
|
31
|
+
SOURCE_HEADER => source, TIMESTAMP_HEADER => timestamp, VERSION_HEADER => VERSION,
|
|
32
|
+
SIGNATURE_HEADER => signature(secret: secret, source: source, timestamp: timestamp, method: method, path: path, body: body)
|
|
33
|
+
}
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def authenticate!(headers:, sources:, method:, path:, body:, now: Time.now.to_i) # rubocop:disable Metrics/ParameterLists
|
|
37
|
+
source = headers[SOURCE_HEADER]
|
|
38
|
+
timestamp = headers[TIMESTAMP_HEADER]
|
|
39
|
+
supplied = headers[SIGNATURE_HEADER]
|
|
40
|
+
unless headers[VERSION_HEADER] == VERSION && valid_source?(source) &&
|
|
41
|
+
timestamp.is_a?(String) && timestamp.match?(%r{\A\d{1,12}\z}) &&
|
|
42
|
+
(now - timestamp.to_i).abs <= TTL && supplied.is_a?(String) && supplied.match?(%r{\A[0-9a-f]{64}\z})
|
|
43
|
+
raise Unauthorized, 'Progress sync authentication failed'
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
entry = sources.is_a?(Hash) ? sources[source] : nil
|
|
47
|
+
secret = entry.is_a?(Hash) ? (entry[:secret] || entry['secret']) : nil
|
|
48
|
+
unless secret.is_a?(String) && secret.length >= 32
|
|
49
|
+
raise Unauthorized, 'Progress sync authentication failed'
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
expected = signature(secret: secret, source: source, timestamp: timestamp, method: method, path: path, body: body)
|
|
53
|
+
unless ActiveSupport::SecurityUtils.secure_compare(expected, supplied)
|
|
54
|
+
raise Unauthorized, 'Progress sync authentication failed'
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
source
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'uri'
|
|
6
|
+
require 'openssl'
|
|
7
|
+
require 'timeout'
|
|
8
|
+
require 'bible270/progress_sync'
|
|
9
|
+
require 'bible270/progress_sync/authentication'
|
|
10
|
+
|
|
11
|
+
module Bible270
|
|
12
|
+
module ProgressSync
|
|
13
|
+
class Client
|
|
14
|
+
REQUEST_TIMEOUT = 30
|
|
15
|
+
|
|
16
|
+
def initialize(destination_key)
|
|
17
|
+
config = Bible270.config
|
|
18
|
+
@source = config.progress_sync_instance_id if config.respond_to?(:progress_sync_instance_id)
|
|
19
|
+
entries = config.progress_sync_destinations if config.respond_to?(:progress_sync_destinations)
|
|
20
|
+
entry = entries[destination_key.to_s] if entries.is_a?(Hash)
|
|
21
|
+
unless Authentication.valid_source?(@source) && entry.is_a?(Hash)
|
|
22
|
+
raise ConfigurationError, 'Progress sync requires a source instance ID and an allowlisted destination'
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
@url = (entry[:url] || entry['url']).to_s.chomp('/')
|
|
26
|
+
@secret = entry[:secret] || entry['secret']
|
|
27
|
+
@uri = URI.parse(@url)
|
|
28
|
+
unless valid_destination?
|
|
29
|
+
raise ConfigurationError, 'Destination requires an HTTPS mount URL and a secret of at least 32 characters'
|
|
30
|
+
end
|
|
31
|
+
rescue URI::InvalidURIError
|
|
32
|
+
raise ConfigurationError, 'Destination URL is invalid'
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def lookup(email)
|
|
36
|
+
email = email.to_s.strip.downcase
|
|
37
|
+
unless valid_email?(email)
|
|
38
|
+
raise Conflict, 'A valid destination reader email is required'
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
result = post('lookup', email: email, plan_fingerprint: ProgressSync.fingerprint)
|
|
42
|
+
unless positive_integer?(result['reader_id']) && positive_integer?(result['participation_id']) &&
|
|
43
|
+
nonnegative_integer?(result['generation']) && result['name'].is_a?(String) && !result['name'].strip.empty? &&
|
|
44
|
+
valid_email?(result['email']) && result['email'].strip.downcase == email &&
|
|
45
|
+
result['plan_fingerprint'] == ProgressSync.fingerprint
|
|
46
|
+
raise Conflict, 'Destination returned an incompatible reader or reading plan'
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
result
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def complete(link, readings)
|
|
53
|
+
unless link.destination_url == @url
|
|
54
|
+
raise ConfigurationError, 'Destination URL changed; explicitly relink'
|
|
55
|
+
end
|
|
56
|
+
unless readings.is_a?(Array) && readings.length <= 5000
|
|
57
|
+
raise Conflict, 'A completion batch must contain at most 5000 readings'
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
result = post('complete', reader_id: link.remote_reader_id, participation_id: link.remote_participation_id,
|
|
61
|
+
generation: link.remote_generation, plan_fingerprint: link.plan_fingerprint, readings: readings)
|
|
62
|
+
unless nonnegative_integer?(result['added']) && nonnegative_integer?(result['existing']) &&
|
|
63
|
+
result['added'] + result['existing'] == readings.uniq.length
|
|
64
|
+
raise Unavailable, 'Destination returned invalid completion counts'
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
result
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def valid_destination?
|
|
73
|
+
@uri.is_a?(URI::HTTPS) && @uri.host && !@uri.userinfo && !@uri.query && !@uri.fragment &&
|
|
74
|
+
@secret.is_a?(String) && @secret.length >= 32
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def positive_integer?(value) = value.is_a?(Integer) && value.positive?
|
|
78
|
+
def nonnegative_integer?(value) = value.is_a?(Integer) && value >= 0
|
|
79
|
+
def valid_email?(value) = value.is_a?(String) && value.bytesize <= 320 && value.strip.match?(%r{\A[^@\s]+@[^@\s]+\z})
|
|
80
|
+
|
|
81
|
+
def post(action, payload)
|
|
82
|
+
body = JSON.generate(payload)
|
|
83
|
+
raise Conflict, 'Progress sync request is too large' if body.bytesize > Authentication::MAX_BYTES
|
|
84
|
+
|
|
85
|
+
path = "#{@uri.path.chomp('/')}/progress-sync/#{action}"
|
|
86
|
+
request = Net::HTTP::Post.new(path)
|
|
87
|
+
request['Content-Type'] = 'application/json'
|
|
88
|
+
request['Accept'] = 'application/json'
|
|
89
|
+
# Disable Net::HTTP's automatic decompression so the byte limit applies to
|
|
90
|
+
# the actual response we parse, not an unbounded inflated buffer.
|
|
91
|
+
request['Accept-Encoding'] = 'identity'
|
|
92
|
+
Authentication.headers(secret: @secret, source: @source, method: 'POST', path: path, body: body).each do |key, value|
|
|
93
|
+
request[key] = value
|
|
94
|
+
end
|
|
95
|
+
request.body = body
|
|
96
|
+
http = Net::HTTP.new(@uri.host, @uri.port, nil)
|
|
97
|
+
http.use_ssl = true
|
|
98
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
|
99
|
+
http.open_timeout = 5
|
|
100
|
+
http.read_timeout = 10
|
|
101
|
+
http.write_timeout = 10
|
|
102
|
+
http.max_retries = 0
|
|
103
|
+
response_body = +''
|
|
104
|
+
status = nil
|
|
105
|
+
# Per-operation timeouts do not bound a peer that keeps trickling bytes.
|
|
106
|
+
# Finish well before the outbox lease expires, including TLS and cleanup.
|
|
107
|
+
Timeout.timeout(REQUEST_TIMEOUT) do
|
|
108
|
+
http.start do |connection|
|
|
109
|
+
connection.request(request) do |response|
|
|
110
|
+
status = response.code.to_i
|
|
111
|
+
# Never follow redirects, and never expose response error pages.
|
|
112
|
+
check_status!(status)
|
|
113
|
+
if response['Content-Length'].to_i > Authentication::MAX_BYTES
|
|
114
|
+
raise Unavailable, 'Destination response is too large'
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
response.read_body do |chunk|
|
|
118
|
+
raise Unavailable, 'Destination response is too large' if response_body.bytesize + chunk.bytesize > Authentication::MAX_BYTES
|
|
119
|
+
|
|
120
|
+
response_body << chunk
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
result = JSON.parse(response_body)
|
|
126
|
+
raise Unavailable, 'Destination returned invalid JSON' unless result.is_a?(Hash)
|
|
127
|
+
|
|
128
|
+
result
|
|
129
|
+
rescue JSON::ParserError, EncodingError
|
|
130
|
+
raise Unavailable, 'Destination returned invalid JSON', cause: nil
|
|
131
|
+
rescue Timeout::Error, SocketError, IOError, SystemCallError, OpenSSL::SSL::SSLError, Net::HTTPBadResponse, Net::ProtocolError
|
|
132
|
+
raise Unavailable, 'Progress sync destination could not be reached securely', cause: nil
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def check_status!(status)
|
|
136
|
+
return if status == 200
|
|
137
|
+
|
|
138
|
+
case status
|
|
139
|
+
when 401, 403 then raise Unauthorized, 'Destination rejected progress sync authentication'
|
|
140
|
+
when 400, 404, 409, 410, 413, 422 then raise Conflict, 'Destination rejected the linked reader, reading plan, or completion batch'
|
|
141
|
+
when 300..399 then raise ConfigurationError, 'Progress sync destination redirects are not allowed'
|
|
142
|
+
else raise Unavailable, 'Progress sync destination is temporarily unavailable'
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|