Module: Sepa::AttributeChecks

Includes:
ErrorMessages
Included in:
Client
Defined in:
lib/sepa/attribute_checks.rb

Overview

Contains functionality to check the attributes passed to Client. Uses ActiveModel::Validations for the actual validation.

Constant Summary

Constants included from ErrorMessages

ErrorMessages::CONTENT_ERROR_MESSAGE, ErrorMessages::CUSTOMER_ID_ERROR_MESSAGE, ErrorMessages::DECRYPTION_ERROR_MESSAGE, ErrorMessages::ENCRYPTION_CERT_ERROR_MESSAGE, ErrorMessages::ENCRYPTION_CERT_REQUEST_ERROR_MESSAGE, ErrorMessages::ENCRYPTION_PRIVATE_KEY_ERROR_MESSAGE, ErrorMessages::ENVIRONMENT_ERROR_MESSAGE, ErrorMessages::FILE_REFERENCE_ERROR_MESSAGE, ErrorMessages::FILE_TYPE_ERROR_MESSAGE, ErrorMessages::HASH_ERROR_MESSAGE, ErrorMessages::NOT_OK_RESPONSE_CODE_ERROR_MESSAGE, ErrorMessages::PIN_ERROR_MESSAGE, ErrorMessages::SIGNATURE_ERROR_MESSAGE, ErrorMessages::SIGNING_CERT_REQUEST_ERROR_MESSAGE, ErrorMessages::STATUS_ERROR_MESSAGE, ErrorMessages::TARGET_ID_ERROR_MESSAGE

Instance Method Summary collapse

Instance Method Details

#allowed_commandsArray<Symbol>

Commands which are allowed for a specific bank

Returns:

  • (Array<Symbol>)

    the commands which are allowed for Client#bank.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sepa/attribute_checks.rb', line 10

def allowed_commands
  case bank
  when :nordea
    [
      STANDARD_COMMANDS,
      :get_certificate,
      :renew_certificate,
    ].flatten
  when :danske
    [
      STANDARD_COMMANDS - [:get_user_info],
      :create_certificate,
      :get_bank_certificate,
      :renew_certificate,
    ].flatten
  when :op
    [
      STANDARD_COMMANDS - [:get_user_info],
      :get_certificate,
      :get_service_certificates,
    ].flatten
  when :samlink
    [
      STANDARD_COMMANDS - [:get_user_info],
      :get_certificate,
      :renew_certificate,
    ].flatten
  else
    []
  end
end

#check_commandObject

Checks that Client#command is included in #allowed_commands



43
44
45
# File 'lib/sepa/attribute_checks.rb', line 43

def check_command
  errors.add(:command, "Invalid command") unless allowed_commands.include? command
end

#check_contentObject

Checks that the content (payload) of the request is somewhat correct. This validation is only run when Client#command is :upload_file.



139
140
141
142
143
144
145
146
147
148
# File 'lib/sepa/attribute_checks.rb', line 139

def check_content
  return unless command == :upload_file

  check = true
  check &&= content
  check &&= content.respond_to? :length
  check &&= !content.empty?

  errors.add(:content, CONTENT_ERROR_MESSAGE) unless check
end

#check_customer_idObject

Checks that Client#customer_id is valid



167
168
169
170
171
# File 'lib/sepa/attribute_checks.rb', line 167

def check_customer_id
  unless customer_id && customer_id.respond_to?(:length) && customer_id.length.between?(1, 16)
    errors.add(:customer_id, CUSTOMER_ID_ERROR_MESSAGE)
  end
end

#check_encryption_cert_requestObject

Checks that encryption certificate signing request can be initialized properly.



76
77
78
79
80
81
82
# File 'lib/sepa/attribute_checks.rb', line 76

def check_encryption_cert_request
  return unless bank == :danske
  return unless [:create_certificate, :renew_certificate].include? command
  return if cert_request_valid?(encryption_csr)

  errors.add(:encryption_csr, ENCRYPTION_CERT_REQUEST_ERROR_MESSAGE)
end

#check_encryption_certificateObject

Checks that Client#bank_encryption_certificate can be initialized properly. Only run if Client#bank is :danske and Client#command is not :get_bank_certificate.



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/sepa/attribute_checks.rb', line 175

def check_encryption_certificate
  return unless bank == :danske
  return if command == :get_bank_certificate

  unless bank_encryption_certificate
    return errors.add(:bank_encryption_certificate, ENCRYPTION_CERT_ERROR_MESSAGE)
  end

  x509_certificate bank_encryption_certificate

rescue
  errors.add(:bank_encryption_certificate, ENCRYPTION_CERT_ERROR_MESSAGE)
end

#check_encryption_private_keyObject

Checks that Client#encryption_private_key can be initialized properly. Is only run if Client#bank is :danske and Client#command is not :create_certificate or :get_bank_certificate.



208
209
210
211
212
213
214
215
216
# File 'lib/sepa/attribute_checks.rb', line 208

def check_encryption_private_key
  return unless bank == :danske
  return if [:create_certificate, :get_bank_certificate].include? command

  rsa_key encryption_private_key

rescue
  errors.add :encryption_private_key, ENCRYPTION_PRIVATE_KEY_ERROR_MESSAGE
end

#check_environmentObject

Checks that Client#environment is included in Client::ENVIRONMENTS. Not run if Client#command is :get_bank_certificate.



159
160
161
162
163
164
# File 'lib/sepa/attribute_checks.rb', line 159

def check_environment
  return if command == :get_bank_certificate
  return if Client::ENVIRONMENTS.include?(environment)

  errors.add(:environment, ENVIRONMENT_ERROR_MESSAGE)
end

#check_file_referenceObject

Checks presence and length of Client#file_reference if Client#command is :download_file



199
200
201
202
203
# File 'lib/sepa/attribute_checks.rb', line 199

def check_file_reference
  return unless command == :download_file

  check_presence_and_length :file_reference, 33, FILE_REFERENCE_ERROR_MESSAGE
end

#check_file_typeObject

Checks that Client#file_type is proper



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/sepa/attribute_checks.rb', line 85

def check_file_type
  if file_type.present?
    valid = file_type.size < 35
  else
    return if bank == :op && %i(download_file download_file_list).include?(command)

    valid = !(%i(
      download_file
      download_file_list
      upload_file
    ).include? command)
  end

  errors.add(:file_type, FILE_TYPE_ERROR_MESSAGE) unless valid
end

#check_keysObject

Checks that signing keys and certificates can be initialized properly.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sepa/attribute_checks.rb', line 48

def check_keys
  return if %i(
    create_certificate
    get_bank_certificate
    get_certificate
    get_service_certificates
  ).include? command

  begin
    rsa_key signing_private_key
  rescue
    errors.add(:signing_private_key, "Invalid signing private key")
  end

  x509_certificate own_signing_certificate
rescue
  errors.add(:own_signing_certificate, "Invalid signing certificate")
end

#check_pinObject

Checks that the Client#pin used in certificate requests in valid



151
152
153
154
155
# File 'lib/sepa/attribute_checks.rb', line 151

def check_pin
  return unless [:create_certificate, :get_certificate].include? command

  check_presence_and_length(:pin, 20, PIN_ERROR_MESSAGE)
end

#check_presence_and_length(attribute, length, error_message) ⇒ Object

Checks presence and length of an attribute

Parameters:

  • attribute (Symbol)

    the attribute to validate

  • length (Integer)

    the maximum length of the attribute

  • error_message (#to_s)

    the error message to display if the validation fails



127
128
129
130
131
132
133
134
135
# File 'lib/sepa/attribute_checks.rb', line 127

def check_presence_and_length(attribute, length, error_message)
  check = true
  check &&= send(attribute)
  check &&= send(attribute).respond_to? :size
  check &&= send(attribute).size < length
  check &&= !send(attribute).empty?

  errors.add(attribute, error_message) unless check
end

#check_signing_csrObject

Checks that signing certificate signing request can be initialized properly.



68
69
70
71
72
73
# File 'lib/sepa/attribute_checks.rb', line 68

def check_signing_csr
  return unless [:get_certificate, :create_certificate, :renew_certificate].include? command
  return if cert_request_valid?(signing_csr)

  errors.add(:signing_csr, SIGNING_CERT_REQUEST_ERROR_MESSAGE)
end

#check_statusObject

Checks that Client#status is included in Client::STATUSES.



190
191
192
193
194
195
196
# File 'lib/sepa/attribute_checks.rb', line 190

def check_status
  return if bank == :samlink && command != :download_file_list
  return unless [:download_file_list, :download_file].include? command
  return if status && Client::STATUSES.include?(status)

  errors.add :status, STATUS_ERROR_MESSAGE
end

#check_target_idObject

Checks that Client#target_id is valid.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/sepa/attribute_checks.rb', line 102

def check_target_id
  exclude_commands = [
    :create_certificate,
    :get_bank_certificate,
    :get_certificate,
    :get_user_info,
    :renew_certificate,
  ]

  exclude_banks = [
    :danske,
    :op,
    :samlink,
  ]

  return if exclude_commands.include?(command) || exclude_banks.include?(bank)

  check_presence_and_length(:target_id, 80, TARGET_ID_ERROR_MESSAGE)
end