Skip to content
Snippets Groups Projects
Tools.pm 3.2 KiB
Newer Older
package IdPAccountManager::Tools;

Guillaume ROUSSE's avatar
Guillaume ROUSSE committed
use strict;
use warnings;

use Digest::SHA;
use Template;
use Template::Stash;
use IdPAccountManager::Data::TestAccount::Manager;

INIT {
    ## a TT2 virtual method to get a variable type
    $Template::Stash::LIST_OPS->{isa} = sub {
        my ($list, $type) = @_;
        return 1 if ($type eq 'ARRAY');
        return 0;
    };
    $Template::Stash::SCALAR_OPS->{isa} = sub {
        my ($list, $type) = @_;
# get SHA256 hash for a string
sub sha256_hash {
    my ($s) = @_;
    return Digest::SHA::sha256_base64($s);
# 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 ($templates_dir, $output) = @_;
    my $tt2 = Template->new({
Guillaume ROUSSE's avatar
Guillaume ROUSSE committed
    my $template = 'accountProfiles/valid-accounts.php.tt2';
    my $data = {
        accounts => IdPAccountManager::Data::TestAccount::Manager->get_testaccounts(),
Guillaume ROUSSE's avatar
Guillaume ROUSSE committed
    };
Guillaume ROUSSE's avatar
Guillaume ROUSSE committed
    return $tt2->process($template, $data, $output);
IdPAccountManager::Tools - Set of subroutines usefull for the Test Account manager
The Test Account manager instanciates test accounts associated to a SAML Identity Provider.
This module gathers a set of usefull subroutines.

=head1 FUNCTIONS
=item generate_password()

Returns a random password following some security guidelines.

=item update_ssp_authsources()

Update simpleSAMLphp authsources.php configuration file with the currently valid test accounts.

=back