| 1 | ## Domain Registry Interface, Login Security Extension Mapping for EPP | ||
| 2 | ## | ||
| 3 | ## Copyright (c) 2019 Patrick Mevzek <netdri@dotandco.com>. All rights reserved. | ||
| 4 | ## | ||
| 5 | ## This file is part of Net::DRI | ||
| 6 | ## | ||
| 7 | ## Net::DRI is free software; you can redistribute it and/or modify | ||
| 8 | ## it under the terms of the GNU General Public License as published by | ||
| 9 | ## the Free Software Foundation; either version 2 of the License, or | ||
| 10 | ## (at your option) any later version. | ||
| 11 | ## | ||
| 12 | ## See the LICENSE file that comes with this distribution for more details. | ||
| 13 | ######################################################################################### | ||
| 14 | |||
| 15 | package Net::DRI::Protocol::EPP::Extensions::LoginSecurity; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use feature 'state'; | ||
| 20 | |||
| 21 | use Net::DRI::Exception; | ||
| 22 | use Net::DRI::Util; | ||
| 23 | |||
| 24 | #################################################################################################### | ||
| 25 | |||
| 26 | sub register_commands | ||
| 27 | { | ||
| 28 | my ($class, $version)=@_; | ||
| 29 | state $rsession = { 'session' => { 'login' => [ \&login, \&parse ]} }; | ||
| 30 | |||
| 31 | return $rsession; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub setup | ||
| 35 | { | ||
| 36 | my ($class, $po, $version)=@_; | ||
| 37 | state $ns = { 'loginSec' => 'urn:ietf:params:xml:ns:epp:loginSec-0.2' }; | ||
| 38 | $po->ns($ns); | ||
| 39 | |||
| 40 | # Patch core login | ||
| 41 | # (this is bad and should not be needed if we would be able to register_commands at specific time of run) | ||
| 42 | my $core_login = $po->{commands_by_class}{'Net::DRI::Protocol::EPP::Core::Session'}{session}{login}[0]; | ||
| 43 | |||
| 44 | my $new_login = sub { | ||
| 45 | my ($po, $login, $password, $rdata) = @_; | ||
| 46 | |||
| 47 | my $ns = $po->ns(); | ||
| 48 | if (defined $ns && exists $ns->{loginSec} && grep { $_ eq $ns->{loginSec} } @{$po->default_parameters()->{server}->{extensions_selected}}) | ||
| 49 | { | ||
| 50 | if (length($password) > 16) | ||
| 51 | { | ||
| 52 | $rdata->{login_security} = { password => $password }; | ||
| 53 | $password = '[LOGIN-SECURITY]'; # §3.2 | ||
| 54 | } | ||
| 55 | if (Net::DRI::Util::has_key($rdata, 'client_newpassword') and length($rdata->{client_newpassword}) > 16) | ||
| 56 | { | ||
| 57 | $rdata->{login_security}->{client_newpassword} = $rdata->{client_newpassword}; | ||
| 58 | $rdata->{client_newpassword} = '[LOGIN-SECURITY]'; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | return $core_login->($po, $login, $password, $rdata); | ||
| 62 | }; | ||
| 63 | |||
| 64 | $po->{commands_by_class}{'Net::DRI::Protocol::EPP::Core::Session'}{session}{login} = [ $new_login, undef ]; | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub implements { return 'https://tools.ietf.org/html/draft-gould-regext-login-security-02'; } | ||
| 69 | |||
| 70 | |||
| 71 | sub login | ||
| 72 | { | ||
| 73 | my ($po, $login, $password, $rdata)=@_; | ||
| 74 | |||
| 75 | return unless Net::DRI::Util::has_key($rdata, 'login_security'); | ||
| 76 | |||
| 77 | my ($long_password, $long_new_password)=@{$rdata->{login_security}}{qw/password client_newpassword/}; | ||
| 78 | delete $rdata->{login_security}; | ||
| 79 | |||
| 80 | my $agent=sprintf('Net::DRI/%s (%s XML::LibXML/%s perl/%vd %s)', | ||
| 81 | $Net::DRI::VERSION, | ||
| 82 | $po->nameversion(), | ||
| 83 | $XML::LibXML::VERSION, | ||
| 84 | $^V, | ||
| 85 | $^O | ||
| 86 | ); | ||
| 87 | $agent=$rdata->{client_agent} if Net::DRI::Util::has_key($rdata, 'client_agent'); | ||
| 88 | |||
| 89 | my $mes=$po->message(); | ||
| 90 | my $eid=$mes->command_extension_register('loginSec', 'loginSec'); | ||
| 91 | |||
| 92 | my @nodes; | ||
| 93 | push @nodes, ['loginSec:userAgent', $agent]; | ||
| 94 | if (defined $long_password) | ||
| 95 | { | ||
| 96 | Net::DRI::Exception::usererr_invalid_parameters('password') unless Net::DRI::Util::xml_is_token($long_password, 6); | ||
| 97 | push @nodes, ['loginSec:pw', $long_password]; | ||
| 98 | } | ||
| 99 | if (defined $long_new_password) | ||
| 100 | { | ||
| 101 | Net::DRI::Exception::usererr_invalid_parameters('client_newpassword') unless Net::DRI::Util::xml_is_token($long_new_password, 6); | ||
| 102 | push @nodes, ['loginSec:newPW', $long_new_password]; | ||
| 103 | } | ||
| 104 | $mes->command_extension($eid, \@nodes); | ||
| 105 | |||
| 106 | return; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub parse | ||
| 110 | { | ||
| 111 | my ($po, $otype, $oaction, $oname, $rinfo)=@_; | ||
| 112 | my $mes=$po->message(); | ||
| 113 | |||
| 114 | # This needs to be parsed for both success and error cases | ||
| 115 | my $data = $mes->get_extension('loginSec','loginSecData'); | ||
| 116 | return unless defined $data; | ||
| 117 | |||
| 118 | my %events = (warning => [], error => []); | ||
| 119 | foreach my $el (Net::DRI::Util::xml_list_children($data)) | ||
| 120 | { | ||
| 121 | my ($name, $node)=@$el; | ||
| 122 | |||
| 123 | next unless $name eq 'event'; | ||
| 124 | |||
| 125 | my %event = (type => $node->getAttribute('type'), # mandatory | ||
| 126 | lang => 'en'); # optional, default value | ||
| 127 | foreach my $attr (qw/name exDate value duration lang/) | ||
| 128 | { | ||
| 129 | next unless $node->hasAttribute($attr); | ||
| 130 | $event{$attr eq 'name' ? 'subtype' : $attr} = $node->getAttribute($attr); | ||
| 131 | } | ||
| 132 | |||
| 133 | my $content = $node->textContent(); | ||
| 134 | $content =~ s/^\s+//; | ||
| 135 | $content =~ s/\s+$//; | ||
| 136 | $event{description} = $content if $content ne ''; | ||
| 137 | |||
| 138 | push @{$events{$node->getAttribute('level')}}, \%event; | ||
| 139 | } | ||
| 140 | |||
| 141 | $rinfo->{session}->{login_security} = \%events; | ||
| 142 | |||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | #################################################################################################### | ||
| 147 | 1; | ||
| 148 | |||
| 149 | |||
| 150 | __END__ | ||
| 151 | |||
| 152 | =pod | ||
| 153 | |||
| 154 | =head1 NAME | ||
| 155 | |||
| 156 | Net::DRI::Protocol::EPP::Extensions::LoginSecurity - EPP Login Security Extension mapping (draft-gould-regext-login-security-02) for Net::DRI | ||
| 157 | |||
| 158 | =head1 DESCRIPTION | ||
| 159 | |||
| 160 | Please see the README file for details. | ||
| 161 | |||
| 162 | =head1 SUPPORT | ||
| 163 | |||
| 164 | For now, support questions should be sent to: | ||
| 165 | |||
| 166 | E<lt>netdri@dotandco.comE<gt> | ||
| 167 | |||
| 168 | Please also see the SUPPORT file in the distribution. | ||
| 169 | |||
| 170 | =head1 SEE ALSO | ||
| 171 | |||
| 172 | E<lt>http://www.dotandco.com/services/software/Net-DRI/E<gt> | ||
| 173 | |||
| 174 | =head1 AUTHOR | ||
| 175 | |||
| 176 | Patrick Mevzek, E<lt>netdri@dotandco.comE<gt> | ||
| 177 | |||
| 178 | =head1 COPYRIGHT | ||
| 179 | |||
| 180 | Copyright (c) 2019 Patrick Mevzek <netdri@dotandco.com>. | ||
| 181 | All rights reserved. | ||
| 182 | |||
| 183 | This program is free software; you can redistribute it and/or modify | ||
| 184 | it under the terms of the GNU General Public License as published by | ||
| 185 | the Free Software Foundation; either version 2 of the License, or | ||
| 186 | (at your option) any later version. | ||
| 187 | |||
| 188 | See the LICENSE file that comes with this distribution for more details. | ||
| 189 | |||
| 190 | =cut | ||
| 191 |