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.



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
# File 'lib/sepa/attribute_checks.rb', line 11

def allowed_commands
  case bank
  when :nordea
    STANDARD_COMMANDS +
    i(
      get_certificate
      renew_certificate
    )
  when :danske
    STANDARD_COMMANDS -
    i() +
    i(
      create_certificate
      get_bank_certificate
     )
  when :op
    STANDARD_COMMANDS -
    i() +
    i(
      get_certificate
      get_service_certificates
     )
  else
    []
  end
end

#check_commandObject

Checks that Client#command is included in #allowed_commands



39
40
41
# File 'lib/sepa/attribute_checks.rb', line 39

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.



135
136
137
138
139
140
141
142
143
144
# File 'lib/sepa/attribute_checks.rb', line 135

def check_content
  return unless command == :upload_file

  check = true
  check &&= content
  check &&= content.respond_to? :length
  check &&= content.length > 0

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

#check_customer_idObject

Checks that Client#customer_id is valid



164
165
166
167
168
# File 'lib/sepa/attribute_checks.rb', line 164

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.



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

def check_encryption_cert_request
  return unless command == :create_certificate

  unless cert_request_valid?(encryption_csr)
    errors.add(:encryption_csr, ENCRYPTION_CERT_REQUEST_ERROR_MESSAGE)
  end
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.



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/sepa/attribute_checks.rb', line 172

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.



205
206
207
208
209
210
211
212
213
# File 'lib/sepa/attribute_checks.rb', line 205

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.



155
156
157
158
159
160
161
# File 'lib/sepa/attribute_checks.rb', line 155

def check_environment
  return if command == :get_bank_certificate

  unless Client::ENVIRONMENTS.include? environment
    errors.add(:environment, ENVIRONMENT_ERROR_MESSAGE)
  end
end

#check_file_referenceObject

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



196
197
198
199
200
# File 'lib/sepa/attribute_checks.rb', line 196

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



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

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.



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

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

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

#check_pinObject

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



147
148
149
150
151
# File 'lib/sepa/attribute_checks.rb', line 147

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



123
124
125
126
127
128
129
130
131
# File 'lib/sepa/attribute_checks.rb', line 123

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).size > 0

  errors.add(attribute, error_message) unless check
end

#check_signing_csrObject

Checks that signing certificate signing request can be initialized properly.



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

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

  unless cert_request_valid?(signing_csr)
    errors.add(:signing_csr, SIGNING_CERT_REQUEST_ERROR_MESSAGE)
  end
end

#check_statusObject

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



187
188
189
190
191
192
193
# File 'lib/sepa/attribute_checks.rb', line 187

def check_status
  return unless [:download_file_list, :download_file].include? command

  unless status && Client::STATUSES.include?(status)
    errors.add :status, STATUS_ERROR_MESSAGE
  end
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
# File 'lib/sepa/attribute_checks.rb', line 102

def check_target_id
  return if i(
      create_certificate
      get_bank_certificate
      get_certificate
      renew_certificate
      
    ).include?(command) ||
    i(
      danske
      op
    ).include?(bank)

  check_presence_and_length(:target_id, 80, TARGET_ID_ERROR_MESSAGE)
end