Skip to content
Snippets Groups Projects
Tools.pm 4.84 KiB
package IdPAccountManager::Tools;

use Template;

my %log_levels = ('debug' => 0, 'info' => 1, 'trace' => 1, 'notice' => 2, 'error' => 3);


# This function generates a random password
sub generate_password{
        my $length_of_randomstring=10;# the length of 
         # the random string to generate
        
        # plusieurs tirages :
        # 1-tirage des caractères obligatoires : les mettre dans un tableau
        my @uppers=('A'..'N','P'..'Z');
        my @lowers=('a'..'k','m'..'z');
        my @punctuation=('%',';',':','!','?','&','$','*','(',')','{','}','[',']','<','>','.','=','-','#');
        my @numerics=('0'..'9');
        my @rndtab;
        push(@rndtab,$uppers[rand @uppers]);
        push(@rndtab,$lowers[rand @lowers]);
        push(@rndtab,$punctuation[rand @punctuation]);

        ## Pas de caractères 8bit pour l'antispam
        push(@rndtab,$numerics[rand @numerics]);
        
        # 2-tirage des caractères optionnels : les ajouter au tableau
        my @chars=('a'..'k','m'..'z','A'..'N','P'..'Z','0'..'9','_','%',';',':','!','?','&','$','*','(',')','{','}','[',']','<','>','.','=','-','#');
        foreach (6..$length_of_randomstring) {
                # rand @chars will generate a random 
                # number between 0 and scalar @chars
                push(@rndtab,$chars[rand @chars]);
        }
        
        # 3-ordonnancement de ceux-ci : les retirer aléatoirement du tableau en les concaténant dans une chaîne
        my $rndstring='';
        my $cpt=1;
        while($cpt<=$length_of_randomstring) {
                my $indice = rand @rndtab;
                $rndstring.=$rndtab[$indice];
                splice (@rndtab, $indice, 1);
                $cpt+=1;
        }
        return $rndstring;
}

## Updates simpleSamlPhp authsources.php configuration file
sub update_ssp_authsources {
    
    my $tt2 = Template->new({'INCLUDE_PATH' => $IdPAccountManager::Conf::global{'root_manager_dir'}.':'.$IdPAccountManager::Conf::global{'root_manager_dir'}.'/templates/accountProfiles'});
    my %args = ('accounts' => IdPAccountManager::TestAccount::list_test_accounts(),
                'conf' => \%IdPAccountManager::Conf::global);
    
    #chdir $IdPAccountManager::Conf::global{'root_manager_dir'};
    
    my $template_file = 'templates/accountProfiles/valid-accounts.php.tt2';
    my $output_file = 'conf/valid-accounts.php';
    #printf "Trace : in=%s, out=%s\n", $template_file, $output_file;
    unless ($tt2->process($template_file, \%args, $output_file)) {
        IdPAccountManager::Tools::do_log('error', "Failed to update valid-accounts.php: %s", $tt2->error());
        return undef;
    }
    
}

## Dump a variable's content
sub dump_var {
    my ($var, $level, $fd) = @_;
    
    if (ref($var)) {