lib/Net/RDAP/Server/EPPBackend.pm untested

filter: Statementstmt 0.0% 0 / 166 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 264 CC 16 SCAR 20.1
CC
16
Cov
0%
get_domain
20.0
head_domain
12.0
generate_dnssec
12.0
CRAP 7.5
1 package Net::RDAP::Server::EPPBackend;
2 # ABSTRACT: an RDAP server that retrieves registration data from an EPP server.
3 use base qw(Net::RDAP::Server);
4 use List::Util qw(any);
5 use JSON::PP;
6 use Net::EPP::Simple;
7 use Net::RDAP::EPPStatusMap;
8 use Net::EPP::ResponseCodes;
9 use vars qw($EVENTS $CONTACTS);
10 use common::sense;
11  
12  
13 #
14 # this maps EPP event element names to the corresponding RDAP event type, and if
15 # applicable, the entity that performed the event action.
16 #
17 $EVENTS = {
18 crDate => [q{registration}, q{crID}],
19 exDate => [q{expiration} ],
20 upDate => [q{last changed}, q{upID}],
21 trDate => [q{transfer}, q{trID}],
22 };
23  
24 #
25 # this maps EPP contact types to the equivalent RDAP role.
26 #
27 $CONTACTS = {
28 admin => q{administrative},
29 tech => q{technical},
30 billing => q{billing},
31 };
32  
33 sub set_backend {
34 my ($self, %args) = @_;
35  
36 $self->{_epp} = Net::EPP::Simple->new(%args);
37  
38 $self->set_handlers;
39  
40 return $self;
41 }
42  
43 sub epp { shift->{_epp} }
44  
45 sub set_handlers {
46 my $self = shift;
47  
48 $self->set_handler(HEAD => 'help', sub { shift->ok });
49 $self->set_handler(GET => 'help', sub { $self->get_help(@_) });
50 $self->set_handler(HEAD => q{domain}, sub { $self->head_domain(@_) });
51 $self->set_handler(GET => q{domain}, sub { $self->get_domain(@_) });
52 }
53  
54 sub get_help {
55 my ($self, $response) = @_;
56  
57 $response->ok;
58  
59 $response->content({
60 rdapConformance => [q{rdap_level_0}],
61 notices => [ {
62 title => 'About this server',
63 description => [ 'This server runs on '.__PACKAGE__.'.' ],
64 } ]
65 });
66 }
67  
68 #
69 # Perform a <check> command and set the HTTP status depending on the object
70 # availability
71 #
72 sub head_domain {
73 my ($self, $response) = @_;
74  
75 my $avail = $self->epp->check_domain($response->request->object);
76  
77 if (!defined($avail)) {
78 $response->error(504, q{Unable to query EPP server.});
79  
80 } elsif (0 == $avail) {
81 $response->ok;
82  
83 }
84 }
85  
86 #
87 # Perform an <info> command, and if successful, construct an RDAP response from
88 # the information return.
89 #
90 sub get_domain {
91 my ($self, $response) = @_;
92  
93 my $info = $self->epp->domain_info($response->request->object);
94  
95 if ($info) {
96 $response->ok;
97 $response->content($self->generate_domain_record($info));
98  
99 } elsif (OBJECT_DOES_NOT_EXIST == $Net::EPP::Simple::Code) {
100 $response->error(404, $Net::EPP::Simple::Error);
101  
102 } elsif (AUTHORIZATION_ERROR == $Net::EPP::Simple::Code) {
103 $response->error(403, $Net::EPP::Simple::Error);
104  
105 } else {
106 $response->error(504, $Net::EPP::Simple::Error);
107  
108 }
109 }
110  
111 #
112 # Convert an EPP <info> response to an RDAP response.
113 #
114 sub generate_domain_record {
115 my ($self, $info) = @_;
116  
117 my $domain = {
118 rdapConformance => [q{rdap_level_0}],
119 objectClassName => q{domain},
120 ldhName => $info->{name},
121 handle => $info->{roid},
122 status => $self->generate_status($info),
123 nameservers => $self->generate_nameservers($info),
124 secureDNS => $self->generate_dnssec($info),
125 events => $self->generate_events($info),
126 entities => $self->generate_entities($info),
127 };
128  
129 return $domain;
130 }
131  
132 #
133 # Convert DNSSEC info from an <info> response.
134 #
135 sub generate_dnssec {
136 my ($self, $info) = @_;
137  
138 if ($info->{DS}) {
139 return {
140 delegationSigned => $JSON::PP::true,
141 dsData => $self->generate_dnssec_dsdata($info->{DS}),
142 };
143  
144 } elsif ($info->{DNSKEY}) {
145 return {
146 delegationSigned => $JSON::PP::true,
147 keyData => $self->generate_dnssec_keydata($info->{DNSKEY}),
148 };
149  
150 } else {
151 return {
152 delegationSigned => $JSON::PP::false,
153 };
154  
155 }
156 }
157  
158 #
159 # Convert dsData info from an <info> response.
160 #
161 sub generate_dnssec_dsdata {
162 my ($self, $ds) = @_;
163  
164 my @dsData;
165  
166 foreach my $ds (map { [ split(/ /, $_, 4)] } @{$ds}) {
167 push(@dsData, {
168 keyTag => int($ds->[0]),
169 algorithm => int($ds->[1]),
170 digestType => int($ds->[2]),
171 digest => $ds->[3],
172 });
173 }
174  
175 return \@dsData;
176 }
177  
178 #
179 # Convert keyData info from an <info> response.
180 #
181 sub generate_dnssec_keydata {
182 my ($self, $keys) = @_;
183  
184 my @keyData;
185  
186 foreach my $key (map { [ split(/ /, $_, 4)] } @{$keys}) {
187 push(@keyData, {
188 flags => int($key->[0]),
189 protocol => int($key->[1]),
190 algorithm => int($key->[2]),
191 publicKey => $key->[3],
192 });
193 }
194  
195 return \@keyData;
196 }
197  
198 #
199 # Convert the EPP status codes in an EPP <info> response to an arrayref of RDAP
200 # status codes.
201 #
202 sub generate_status {
203 my ($self, $info) = @_;
204 return [ map { epp2rdap($_) } @{$info->{status}} ];
205 }
206  
207 #
208 # Convert the nameservers in an EPP <info> response to an arrayref of RDAP
209 # nameserver objects.
210 #
211 sub generate_nameservers {
212 my ($self, $info) = @_;
213 return [ map { { objectClassName => q{nameserver}, ldhName => $_ } } @{$info->{ns}} ],
214 }
215  
216 #
217 # Convert the events in an EPP <info> response to an arrayref of RDAP
218 # event objects.
219 #
220 sub generate_events {
221 my ($self, $info) = @_;
222  
223 my @events;
224  
225 foreach my $key (grep { exists($info->{$_}) } keys(%{$EVENTS})) {
226 my $event = {
227 eventAction => $EVENTS->{$key}->[0],
228 eventDate => $info->{$key},
229 };
230  
231 if (exists($EVENTS->{$key}->[1]) && exists($info->{$EVENTS->{$key}->[1]})) {
232 $event->{eventActor} = $info->{$EVENTS->{$key}->[1]};
233 }
234  
235 push(@events, $event);
236 }
237  
238 return \@events;
239 }
240  
241 #
242 # Convert the contact/client IDs in an EPP <info> response to an arrayref of
243 # RDAP entity objects.
244 #
245 sub generate_entities {
246 my ($self, $info) = @_;
247  
248 #
249 # this maps contact IDs to the appropriate RDAP role
250 #
251 my $roles => {};
252  
253 push(@{$roles->{$info->{registrant}}}, q{registrant}) if (exists($info->{registrant}));
254 push(@{$roles->{$info->{clID}}}, q{registrar}) if (exists($info->{clID}));
255  
256 foreach my $role (keys(%{$info->{contacts}})) {
257 push(@{$roles->{$info->{contacts}->{$role}}}, $CONTACTS->{$role});
258 }
259  
260 return [ map { $self->generate_entity($_, $roles->{$_}) } sort(keys(%{$roles})) ];
261 }
262  
263 #
264 # Given a contact/client ID, construct an RDAP entity.
265 #
266 sub generate_entity {
267 my ($self, $handle, $roles) = @_;
268  
269 my $entity = {
270 objectClassName => q{entity},
271 handle => $handle,
272 roles => $roles,
273 };
274  
275 if (1 < scalar(@{$roles}) || q{registrar} ne $roles->[0]) {
276 if (my $cinfo = $self->epp->contact_info($handle)) {
277 $entity->{vcardArray} = $self->generate_vcardArray($cinfo);
278 }
279 }
280  
281 return $entity;
282 }
283  
284 #
285 # Given a contact <info> respose, construct a JCard object.
286 #
287 sub generate_vcardArray {
288 my ($self, $info) = @_;
289  
290 my @props;
291  
292 push(@props, [q{version}, {}, q{text}, q{4.0}]);
293  
294 foreach my $type (qw(int loc)) {
295 if (exists($info->{postalInfo}->{$type})) {
296 push(@props, $self->generate_postal_info($info->{postalInfo}->{$type}));
297 last;
298 }
299 }
300  
301 push(@props, $self->generate_telephones($info));
302  
303 push(@props, [
304 q{email},
305 {},
306 q{text},
307 $info->{email},
308 ]);
309  
310 return [q{vcard}, \@props];
311 }
312  
313 #
314 # Given a contact <info> respose, generate a set of JCard properties for the
315 # postal address information.
316 #
317 sub generate_postal_info {
318 my ($self, $info) = @_;
319  
320 my @props;
321  
322 #
323 # the FN property is mandatory
324 #
325 push(@props, [
326 q{fn},
327 {},
328 q{text},
329 $info->{name} || "",
330 ]);
331  
332 if (exists($info->{org}) && length($info->{org}) > 0) {
333 push(@props, [
334 q{kind},
335 {},
336 q{text},
337 q{org},
338 ]);
339 push(@props, [
340 q{org},
341 {},
342 q{text},
343 $info->{org},
344 ]);
345 }
346  
347 push(@props, [
348 q{adr},
349 {
350 cc => $info->{addr}->{cc},
351 },
352 q{text},
353 [ map { $_ || "" } (
354 undef,
355 undef,
356 $info->{addr}->{street},
357 $info->{addr}->{city},
358 $info->{addr}->{sp},
359 $info->{addr}->{pc},
360 undef,
361 ) ]
362 ]);
363  
364 return @props;
365 }
366  
367 #
368 # Given a contact <info> respose, generate a set of JCard properties for the
369 # voice and fax numbers.
370 #
371 sub generate_telephones {
372 my ($self, $info) = @_;
373  
374 my @props;
375  
376 foreach my $type (qw(voice fax)) {
377 if (exists($info->{$type}) && length($info->{$type}) > 0) {
378 push(@props, [
379 q{tel},
380 {type => $type},
381 q{text},
382 $info->{$type},
383 ]);
384 }
385 }
386  
387 return @props;
388 }
389  
390 1;
391  
392 __END__