From bb5cd0b0940da1e9c4be67d93c8f4b5b3a0dae8f Mon Sep 17 00:00:00 2001 From: Irka Date: Wed, 3 Sep 2014 12:25:21 +0300 Subject: [PATCH] MT#8437 Changes into config, validation method and example of the call --- etc/csc.conf | 8 ++++ lib/csc/Controller/account.pm | 72 +++++++++++++++------------------- lib/csc/Utils.pm | 74 +++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 41 deletions(-) diff --git a/etc/csc.conf b/etc/csc.conf index da9af42..a0aee3f 100644 --- a/etc/csc.conf +++ b/etc/csc.conf @@ -86,4 +86,12 @@ tls_enabled="[% kamailio.lb.tls.enable %]" /> + diff --git a/lib/csc/Controller/account.pm b/lib/csc/Controller/account.pm index f059175..46cb865 100644 --- a/lib/csc/Controller/account.pm +++ b/lib/csc/Controller/account.pm @@ -109,29 +109,14 @@ sub savepass : Local { return; } - my %messages; - - my $oldpass = $c->request->params->{oldpass}; - my $passwd1 = $c->request->params->{newpass1}; - my $passwd2 = $c->request->params->{newpass2}; - # $c->stash->{refill}{oldpass} = $oldpass; # $c->stash->{refill}{passwd1} = $passwd1; # $c->stash->{refill}{passwd2} = $passwd2; - if(!defined $oldpass or length $oldpass == 0) { - $messages{msgoldpass} = 'Client.Voip.MissingOldPass'; - } - if(!defined $passwd1 or length $passwd1 == 0) { - $messages{msgpasswd} = 'Client.Voip.MissingPass'; - } elsif(length $passwd1 < 6) { - $messages{msgpasswd} = 'Client.Voip.PassLength'; - } elsif(!defined $passwd2 or length $passwd2 == 0) { - $messages{msgpasswd} = 'Client.Voip.MissingPass2'; - } elsif($passwd1 ne $passwd2) { - $messages{msgpasswd} = 'Client.Voip.PassNoMatch'; - } - + my ($passwd1,$passwd2,$oldpass) = @{$c->request->params}{qw/newpass1 newpass2 oldpass/}; + my %messages = %{ csc::Utils::validate_password($c, {}, $passwd1, $passwd2, $oldpass) }; + use Data::Dumper; + $c->log->debug(Dumper \%messages); unless(keys %messages) { unless($c->model('Provisioning')->call_prov($c, 'voip', 'authenticate_webuser', { webusername => $c->session->{user}{webusername}, @@ -875,17 +860,19 @@ sub doaddsubscriber : Local { } $settings{sipuri} = $sipuri; - my $passwd1 = $c->request->params->{fpasswort1}; - my $passwd2 = $c->request->params->{fpasswort2}; - if(!defined $passwd1 or length $passwd1 == 0) { - $messages{msgpasswd} = 'Client.Voip.MissingPass'; - } elsif(length $passwd1 < 6) { - $messages{msgpasswd} = 'Client.Voip.PassLength'; - } elsif(!defined $passwd2) { - $messages{msgpasswd} = 'Client.Voip.MissingPass2'; - } elsif($passwd1 ne $passwd2) { - $messages{msgpasswd} = 'Client.Voip.PassNoMatch'; - } + my ($passwd1,$passwd2) = @{$c->request->params}{qw/fpasswort1 fpasswort2/}; + my $messages_pass = csc::Utils::validate_password($c, { no_old => 1 }, $passwd1, $passwd2); + %messages = (%messages,%$messages_pass); + + #if(!defined $passwd1 or length $passwd1 == 0) { + # $messages{msgpasswd} = 'Client.Voip.MissingPass'; + #} elsif(length $passwd1 < 6) { + # $messages{msgpasswd} = 'Client.Voip.PassLength'; + #} elsif(!defined $passwd2) { + # $messages{msgpasswd} = 'Client.Voip.MissingPass2'; + #} elsif($passwd1 ne $passwd2) { + # $messages{msgpasswd} = 'Client.Voip.PassNoMatch'; + #} unless(keys %messages) { my %create_settings = %settings; @@ -985,17 +972,20 @@ sub doaddextension : Local { } $settings{sipuri} = $sipuri; - my $passwd1 = $c->request->params->{fpasswort1}; - my $passwd2 = $c->request->params->{fpasswort2}; - if(!defined $passwd1 or length $passwd1 == 0) { - $messages{msgpasswd} = 'Client.Voip.MissingPass'; - } elsif(length $passwd1 < 6) { - $messages{msgpasswd} = 'Client.Voip.PassLength'; - } elsif(!defined $passwd2) { - $messages{msgpasswd} = 'Client.Voip.MissingPass2'; - } elsif($passwd1 ne $passwd2) { - $messages{msgpasswd} = 'Client.Voip.PassNoMatch'; - } + my ($passwd1,$passwd2) = @{$c->request->params}{qw/fpasswort1 fpasswort2/}; + my $messages_pass = csc::Utils::validate_password($c, { no_old => 1 }, $passwd1, $passwd2); + %messages = (%messages,%$messages_pass); + #my $passwd1 = $c->request->params->{fpasswort1}; + #my $passwd2 = $c->request->params->{fpasswort2}; + #if(!defined $passwd1 or length $passwd1 == 0) { + # $messages{msgpasswd} = 'Client.Voip.MissingPass'; + #} elsif(length $passwd1 < 6) { + # $messages{msgpasswd} = 'Client.Voip.PassLength'; + #} elsif(!defined $passwd2) { + # $messages{msgpasswd} = 'Client.Voip.MissingPass2'; + #} elsif($passwd1 ne $passwd2) { + # $messages{msgpasswd} = 'Client.Voip.PassNoMatch'; + #} unless(keys %messages) { my %create_settings = %settings; diff --git a/lib/csc/Utils.pm b/lib/csc/Utils.pm index c2106ba..1c67517 100644 --- a/lib/csc/Utils.pm +++ b/lib/csc/Utils.pm @@ -230,6 +230,80 @@ sub normalize_blockentry_for_subscriber { return $entry; } +sub validate_password{ + my ($c, $opt, $passwd1, $passwd2, $oldpasswd) = @_; + use Data::Dumper; + $c->log->debug(Dumper [caller()]); + $c->log->debug(Dumper [$opt, $passwd1, $passwd2, $oldpasswd]); + #$opt - possible keys are: no_old, no_second, messages_unified. All three are boolean and default undef. + #foreach(qw/no_old no_second messages_oldasnew messages_newasold/){ + # $opt->{$_} //= 0; + #} + $opt->{messages_newasold} //= 1; + my $messages = { + msgoldpass => [], + msgpasswd => [], + }; + my $cfg_passwd = $c->config->{security}; + + if(!$opt->{no_old} && ( !defined $oldpasswd or length $oldpasswd == 0 ) ) { + push @{$messages->{msgoldpass}}, 'MissingOldPass'; + } + if($cfg_passwd->{password_min_length} && length($passwd1) < $cfg_passwd->{password_min_length}) { + #Use old messages where is possible, if other not requested explicitly + if( !$opt->{messages_oldasnew} ) { + if( !defined $passwd1 or length $passwd1 == 0 ) { + push @{$messages->{msgpasswd}}, 'MissingPass'; + } else { + push @{$messages->{msgpasswd}}, 'PassLength'; + } + } else { + push @{$messages->{msgpasswd}},'password_min_length'; + } + } + + #save old priority of the checking + if(!$opt->{no_second}) { + if(!defined $passwd2 or length $passwd2 == 0) { + push @{$messages->{msgpasswd}}, 'MissingPass2'; + } elsif($passwd1 ne $passwd2) { + push @{$messages->{msgpasswd}}, 'PassNoMatch'; + } + } + + if($cfg_passwd->{password_max_length} && length($passwd1) > $cfg_passwd->{password_max_length}) { + push @{$messages->{msgpasswd}},'password_max_length'; + } + + if($cfg_passwd->{password_musthave_lowercase} && $passwd1 !~ /[a-z]/) { + push @{$messages->{msgpasswd}},'password_musthave_lowercase'; + } + if($cfg_passwd->{password_musthave_uppercase} && $passwd1 !~ /[A-Z]/) { + push @{$messages->{msgpasswd}},'password_musthave_uppercase'; + } + if($cfg_passwd->{password_musthave_digit} && $passwd1 !~ /[0-9]/) { + push @{$messages->{msgpasswd}},'password_musthave_digit'; + } + if($cfg_passwd->{password_musthave_specialchar} && $passwd1 !~ /[^0-9a-zA-Z]/) { + push @{$messages->{msgpasswd}},'password_musthave_specialchar'; + } + $c->log->debug( Dumper ['messages 1=', $messages] ); + + my %messages = map { + if(@{$messages->{$_}}) { + my $msg = $messages->{$_}->[0]; + if($opt->{messages_newasold}) { + $msg =~s/[_\.]+([a-z])/'\.'.uc($1)/gei; + $msg = ucfirst($msg); + } + $_ => 'Client.Voip.'.$msg; + }else{ + (); + } + } keys %$messages; + $c->log->debug( Dumper ['messages 2=', $messages] ); + return \%messages; +} # finito, l'amore 1;