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

def allowed_commands
  case bank
  when :nordea
    STANDARD_COMMANDS +
    i(get_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



36
37
38
# File 'lib/sepa/attribute_checks.rb', line 36

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.



131
132
133
134
135
136
137
138
139
140
# File 'lib/sepa/attribute_checks.rb', line 131

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



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

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.



72
73
74
75
76
77
78
# File 'lib/sepa/attribute_checks.rb', line 72

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.



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/sepa/attribute_checks.rb', line 168

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.



201
202
203
204
205
206
207
208
209
# File 'lib/sepa/attribute_checks.rb', line 201

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.



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

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



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

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



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

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.



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

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



143
144
145
146
147
# File 'lib/sepa/attribute_checks.rb', line 143

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



119
120
121
122
123
124
125
126
127
# File 'lib/sepa/attribute_checks.rb', line 119

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.



63
64
65
66
67
68
69
# File 'lib/sepa/attribute_checks.rb', line 63

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.



183
184
185
186
187
188
189
# File 'lib/sepa/attribute_checks.rb', line 183

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.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sepa/attribute_checks.rb', line 99

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

  check_presence_and_length(:target_id, 80, TARGET_ID_ERROR_MESSAGE)
end