Module: CommandTower::Messaging::Endpoints::Persistence

Defined in:
app/services/command_tower/messaging/endpoints/persistence.rb

Constant Summary collapse

MAX_RETRIES =
3

Class Method Summary collapse

Class Method Details

.build_encrypted_attrs(normalized_address:, masked_display_value:) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'app/services/command_tower/messaging/endpoints/persistence.rb', line 46

def build_encrypted_attrs(normalized_address:, masked_display_value:)
  encrypted = SecretBox.encrypt(normalized_address)
  {
    address_fingerprint: Fingerprinter.fingerprint(normalized_address),
    masked_display_value:,
    address_ciphertext: encrypted.fetch(:ciphertext),
    encryption_key_version: encrypted.fetch(:key_version),
  }
end

.build_pushover_credential_attrs(validated:) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/services/command_tower/messaging/endpoints/persistence.rb', line 65

def build_pushover_credential_attrs(validated:)
  user_key = SecretBox.encrypt(
    validated.normalized_user_key,
    purpose: SecretBox::PUSHOVER_USER_KEY_PURPOSE,
  )
  application_token = SecretBox.encrypt(
    validated.normalized_application_token,
    purpose: SecretBox::PUSHOVER_APPLICATION_TOKEN_PURPOSE,
  )

  {
    user_key_ciphertext: user_key.fetch(:ciphertext),
    application_token_ciphertext: application_token.fetch(:ciphertext),
    encryption_key_version: user_key.fetch(:key_version),
  }
end

.build_pushover_parent_attrs(validated:) ⇒ Object



56
57
58
59
60
61
62
63
# File 'app/services/command_tower/messaging/endpoints/persistence.rb', line 56

def build_pushover_parent_attrs(validated:)
  {
    address_fingerprint: Fingerprinter.fingerprint(validated.pair_fingerprint_material),
    masked_display_value: validated.masked_display_value,
    address_ciphertext: nil,
    encryption_key_version: SecretBox.key_version,
  }
end

.create_pushover_credential!(endpoint:, validated:) ⇒ Object



82
83
84
85
86
87
# File 'app/services/command_tower/messaging/endpoints/persistence.rb', line 82

def create_pushover_credential!(endpoint:, validated:)
  ::CommandTower::Messaging::EndpointPushoverCredential.create!(
    endpoint:,
    **build_pushover_credential_attrs(validated:),
  )
end

.find_active_by_fingerprint(owner_user_id:, channel_key:, fingerprint:) ⇒ Object



40
41
42
43
44
# File 'app/services/command_tower/messaging/endpoints/persistence.rb', line 40

def find_active_by_fingerprint(owner_user_id:, channel_key:, fingerprint:)
  Endpoint.active.for_owner(owner_user_id).for_channel(channel_key).find_by(
    address_fingerprint: fingerprint,
  )
end

.with_unique_conflict_handlingObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/services/command_tower/messaging/endpoints/persistence.rb', line 11

def with_unique_conflict_handling
  attempts = 0
  begin
    attempts += 1
    yield
  rescue ActiveRecord::RecordNotUnique => e
    translated = translate_unique_conflict(e)
    raise translated if translated

    raise ConflictError, "endpoint uniqueness conflict" if attempts >= MAX_RETRIES

    retry
  end
end