refactoring

This commit is contained in:
Oleg Hasjanov 2021-06-15 10:39:46 +03:00
parent b7c0a6c4c9
commit 6a07aeb4c7
2 changed files with 103 additions and 71 deletions

View file

@ -6,13 +6,13 @@ module Depp
attr_accessor :name, :current_user, :epp_xml attr_accessor :name, :current_user, :epp_xml
STATUSES = %w( STATUSES = %w[
clientDeleteProhibited clientDeleteProhibited
clientHold clientHold
clientRenewProhibited clientRenewProhibited
clientTransferProhibited clientTransferProhibited
clientUpdateProhibited clientUpdateProhibited
) ].freeze
PERIODS = [ PERIODS = [
['3 months', '3m'], ['3 months', '3m'],
@ -28,11 +28,15 @@ module Depp
['8 years', '8y'], ['8 years', '8y'],
['9 years', '9y'], ['9 years', '9y'],
['10 years', '10y'], ['10 years', '10y'],
] ].freeze
def initialize(args = {}) def initialize(args = {})
self.current_user = args[:current_user] self.current_user = args[:current_user]
self.epp_xml = EppXml::Domain.new(cl_trid_prefix: current_user.tag, schema_prefix: 'domain-ee', schema_version: '1.1') self.epp_xml = EppXml::Domain.new(
cl_trid_prefix: current_user.tag,
schema_prefix: 'domain-ee',
schema_version: '1.1'
)
end end
def info(domain_name) def info(domain_name)
@ -49,6 +53,11 @@ module Depp
current_user.request(xml) current_user.request(xml)
end end
def hostname_present
domain_params[:nameservers_attributes]
.select { |_key, value| value['hostname'].present? }.any?
end
def create(domain_params) def create(domain_params)
dns_hash = {} dns_hash = {}
keys = Domain.create_dnskeys_hash(domain_params) keys = Domain.create_dnskeys_hash(domain_params)
@ -57,22 +66,22 @@ module Depp
period = domain_params[:period].to_i.to_s period = domain_params[:period].to_i.to_s
period_unit = domain_params[:period][-1].to_s period_unit = domain_params[:period][-1].to_s
if domain_params[:nameservers_attributes].select { |key, value| value['hostname'].present? }.any? xml = if hostname_present
xml = epp_xml.create({ epp_xml.create({
name: { value: domain_params[:name] }, name: { value: domain_params[:name] },
period: { value: period, attrs: { unit: period_unit } }, period: { value: period, attrs: { unit: period_unit } },
ns: Domain.create_nameservers_hash(domain_params), ns: Domain.create_nameservers_hash(domain_params),
registrant: { value: domain_params[:registrant] }, registrant: { value: domain_params[:registrant] },
_anonymus: Domain.create_contacts_hash(domain_params) _anonymus: Domain.create_contacts_hash(domain_params)
}, dns_hash, Domain.construct_custom_params_hash(domain_params)) }, dns_hash, Domain.construct_custom_params_hash(domain_params))
else else
xml = epp_xml.create({ epp_xml.create({
name: { value: domain_params[:name] }, name: { value: domain_params[:name] },
period: { value: period, attrs: { unit: period_unit } }, period: { value: period, attrs: { unit: period_unit } },
registrant: { value: domain_params[:registrant] }, registrant: { value: domain_params[:registrant] },
_anonymus: Domain.create_contacts_hash(domain_params) _anonymus: Domain.create_contacts_hash(domain_params)
}, dns_hash, Domain.construct_custom_params_hash(domain_params)) }, dns_hash, Domain.construct_custom_params_hash(domain_params))
end end
current_user.request(xml) current_user.request(xml)
end end
@ -92,9 +101,10 @@ module Depp
def delete(domain_params) def delete(domain_params)
xml = epp_xml.delete({ xml = epp_xml.delete({
name: { value: domain_params[:name] }}, name: { value: domain_params[:name] },
Depp::Domain.construct_custom_params_hash(domain_params), },
(domain_params[:verified].present? && 'yes')) Depp::Domain.construct_custom_params_hash(domain_params),
(domain_params[:verified].present? && 'yes'))
current_user.request(xml) current_user.request(xml)
end end
@ -104,10 +114,10 @@ module Depp
period_unit = params[:period][-1].to_s period_unit = params[:period][-1].to_s
current_user.request(epp_xml.renew( current_user.request(epp_xml.renew(
name: { value: params[:domain_name] }, name: { value: params[:domain_name] },
curExpDate: { value: params[:cur_exp_date] }, curExpDate: { value: params[:cur_exp_date] },
period: { value: period, attrs: { unit: period_unit } } period: { value: period, attrs: { unit: period_unit } }
)) ))
end end
def transfer(params) def transfer(params)
@ -117,9 +127,9 @@ module Depp
op = params[:reject] ? 'reject' : op op = params[:reject] ? 'reject' : op
current_user.request(epp_xml.transfer({ current_user.request(epp_xml.transfer({
name: { value: params[:domain_name] }, name: { value: params[:domain_name] },
authInfo: { pw: { value: params[:transfer_code] } } authInfo: { pw: { value: params[:transfer_code] } }
}, op, Domain.construct_custom_params_hash(params))) }, op, Domain.construct_custom_params_hash(params)))
end end
def confirm_transfer(domain_params) def confirm_transfer(domain_params)
@ -127,9 +137,9 @@ module Depp
pw = data.css('pw').text pw = data.css('pw').text
xml = epp_xml.transfer({ xml = epp_xml.transfer({
name: { value: domain_params[:name] }, name: { value: domain_params[:name] },
authInfo: { pw: { value: pw } } authInfo: { pw: { value: pw } }
}, 'approve') }, 'approve')
current_user.request(xml) current_user.request(xml)
end end
@ -171,24 +181,25 @@ module Depp
hostname: x.css('hostName').text, hostname: x.css('hostName').text,
ipv4: Array(x.css('hostAddr[ip="v4"]')).map(&:text).join(','), ipv4: Array(x.css('hostAddr[ip="v4"]')).map(&:text).join(','),
ipv6: Array(x.css('hostAddr[ip="v6"]')).map(&:text).join(',') ipv6: Array(x.css('hostAddr[ip="v6"]')).map(&:text).join(',')
} }
end end
data.css('keyData').each_with_index do |x, i| data.css('keyData').each_with_index do |x, i|
ret[:dnskeys_attributes][i] = { ret[:dnskeys_attributes][i] = {
flags: x.css('flags').text, flags: x.css('flags').text,
protocol: x.css('protocol').text, protocol: x.css('protocol').text,
alg: x.css('alg').text, alg: x.css('alg').text,
public_key: x.css('pubKey').text, public_key: x.css('pubKey').text,
ds_key_tag: x.css('keyTag').first.try(:text), ds_key_tag: x.css('keyTag').first.try(:text),
ds_alg: x.css('alg').first.try(:text), ds_alg: x.css('alg').first.try(:text),
ds_digest_type: x.css('digestType').first.try(:text), ds_digest_type: x.css('digestType').first.try(:text),
ds_digest: x.css('digest').first.try(:text) ds_digest: x.css('digest').first.try(:text)
} }
end end
data.css('status').each_with_index do |x, i| data.css('status').each_with_index do |x, i|
next unless STATUSES.include?(x['s']) next unless STATUSES.include?(x['s'])
ret[:statuses_attributes][i] = { ret[:statuses_attributes][i] = {
code: x['s'], code: x['s'],
description: x.text description: x.text
@ -203,7 +214,7 @@ module Depp
if domain_params[:legal_document].present? if domain_params[:legal_document].present?
type = domain_params[:legal_document].original_filename.split('.').last.downcase type = domain_params[:legal_document].original_filename.split('.').last.downcase
custom_params[:_anonymus] << { custom_params[:_anonymus] << {
legalDocument: { value: Base64.encode64(domain_params[:legal_document].read), attrs: { type: type } } legalDocument: { value: Base64.encode64(domain_params[:legal_document].read), attrs: { type: type } }
} }
end end
@ -231,9 +242,12 @@ module Depp
rem_arr << { ns: rem_ns } if rem_ns.any? rem_arr << { ns: rem_ns } if rem_ns.any?
rem_arr << { _anonymus: rem_anon } if rem_anon.any? rem_arr << { _anonymus: rem_anon } if rem_anon.any?
if domain_params[:registrant] != old_domain_params[:registrant] return if domain_params[:registrant] == old_domain_params[:registrant]
chg = [{ registrant: { value: domain_params[:registrant] } }] if !domain_params[:verified].present?
chg = [{ registrant: { value: domain_params[:registrant], attrs: { verified: 'yes' } } }] if domain_params[:verified] chg = [{ registrant: { value: domain_params[:registrant] } }]
if domain_params[:verified].blank? && (domain_params[:verified])
chg = [{ registrant: { value: domain_params[:registrant],
attrs: { verified: 'yes' } } }]
end end
add_arr = nil if add_arr.none? add_arr = nil if add_arr.none?
@ -263,13 +277,17 @@ module Depp
host_attr = [] host_attr = []
host_attr << { hostName: { value: v['hostname'] } } host_attr << { hostName: { value: v['hostname'] } }
v['ipv4'].to_s.split(",").each do |ip| if v['ipv4'].present?
host_attr << { hostAddr: { value: ip, attrs: { ip: 'v4' } } } v['ipv4'].to_s.split(',').each do |ip|
end if v['ipv4'].present? host_attr << { hostAddr: { value: ip, attrs: { ip: 'v4' } } }
end
end
v['ipv6'].to_s.split(",").each do |ip| if v['ipv6'].present?
host_attr << { hostAddr: { value: ip, attrs: { ip: 'v6' } } } v['ipv6'].to_s.split(',').each do |ip|
end if v['ipv6'].present? host_attr << { hostAddr: { value: ip, attrs: { ip: 'v6' } } }
end
end
ret << { hostAttr: host_attr } ret << { hostAttr: host_attr }
end end
@ -281,6 +299,7 @@ module Depp
ret = [] ret = []
domain_params[:contacts_attributes].each do |_k, v| domain_params[:contacts_attributes].each do |_k, v|
next if v['code'].blank? next if v['code'].blank?
ret << { ret << {
contact: { value: v['code'], attrs: { type: v['type'] } } contact: { value: v['code'], attrs: { type: v['type'] } }
} }
@ -294,9 +313,11 @@ module Depp
domain_params[:dnskeys_attributes].each do |_k, v| domain_params[:dnskeys_attributes].each do |_k, v|
if v['ds_key_tag'].blank? if v['ds_key_tag'].blank?
kd = create_key_data_hash(v) kd = create_key_data_hash(v)
ret << { if kd
keyData: kd ret << {
} if kd keyData: kd
}
end
else else
ret << { ret << {
dsData: [ dsData: [
@ -315,6 +336,7 @@ module Depp
def create_key_data_hash(key_data_params) def create_key_data_hash(key_data_params)
return nil if key_data_params['public_key'].blank? return nil if key_data_params['public_key'].blank?
{ {
flags: { value: key_data_params['flags'] }, flags: { value: key_data_params['flags'] },
protocol: { value: key_data_params['protocol'] }, protocol: { value: key_data_params['protocol'] },
@ -331,6 +353,6 @@ module Depp
end end
ret ret
end end
end end
end end
end end

View file

@ -2,9 +2,9 @@ class EppConstraint
OBJECT_TYPES = { OBJECT_TYPES = {
domain: [ domain: [
{ domain: Xsd::Schema.filename(for_prefix: 'domain-ee') }, { domain: Xsd::Schema.filename(for_prefix: 'domain-ee') },
{ domain: Xsd::Schema.filename(for_prefix: 'domain-eis') } { domain: Xsd::Schema.filename(for_prefix: 'domain-eis') },
], ],
contact: { contact: Xsd::Schema.filename(for_prefix: 'contact-ee') } contact: { contact: Xsd::Schema.filename(for_prefix: 'contact-ee') },
}.freeze }.freeze
def initialize(type) def initialize(type)
@ -16,23 +16,14 @@ class EppConstraint
# TODO: Maybe move this to controller to keep params clean # TODO: Maybe move this to controller to keep params clean
return redirect_to_error_controller(request) if request.params[:action] == 'wrong_schema' return redirect_to_error_controller(request) if request.params[:action] == 'wrong_schema'
if request.params[:raw_frame] request = parse_raw_frame(request) if request.params[:raw_frame]
request.params[:raw_frame] = request.params[:raw_frame].gsub!(/(?<=>)(.*?)(?=<)/) do |s|
s.strip request = parse_params(request)
end
end
request.params[:nokogiri_frame] ||= Nokogiri::XML(request.params[:raw_frame] || request.params[:frame])
request.params[:parsed_frame] ||= request.params[:nokogiri_frame].dup.remove_namespaces!
unless %i[poll session].include?(@type) unless %i[poll session].include?(@type)
element = "//#{@type}:#{request.params[:action]}" element = "//#{@type}:#{request.params[:action]}"
if @type == :domain return enumerate_domain_object(request, element) if @type == :domain
OBJECT_TYPES[@type].each do |obj|
return true unless request.params[:nokogiri_frame].xpath(element.to_s, obj).none?
end
return false
end
return false if request.params[:nokogiri_frame].xpath(element.to_s, OBJECT_TYPES[@type]).none? return false if request.params[:nokogiri_frame].xpath(element.to_s, OBJECT_TYPES[@type]).none?
end end
@ -41,6 +32,25 @@ class EppConstraint
true true
end end
def parse_raw_frame(request)
request.params[:raw_frame] = request.params[:raw_frame].gsub!(/(?<=>)(.*?)(?=<)/, &:strip)
request
end
def enumerate_domain_object(request, element)
OBJECT_TYPES[@type].each do |obj|
return true unless request.params[:nokogiri_frame].xpath(element.to_s, obj).none?
end
false
end
def parse_params(request)
request.params[:nokogiri_frame] ||= Nokogiri::XML(request.params[:raw_frame] || request.params[:frame])
request.params[:parsed_frame] ||= request.params[:nokogiri_frame].dup.remove_namespaces!
request
end
def redirect_to_error_controller(request) def redirect_to_error_controller(request)
request.params[:epp_object_type] = @error request.params[:epp_object_type] = @error
true true