Skip to content
Snippets Groups Projects
TestAccount.pm 6.24 KiB
Newer Older
package IdPAccountManager::TestAccount;

## Copyright (c) GEANT
## This software was developed by RENATER. The research leading to these results has received funding
## from the European Community¹s Seventh Framework Programme (FP7/2007-2013) under grant agreement nº 238875 (GÉANT).

use IdPAccountManager::Data::TestAccount;
use IdPAccountManager::Data::TestAccount::Manager;

use IdPAccountManager::Tools;
use POSIX qw(strftime);
    ## Set error mode  to non fatal
    IdPAccountManager::Data::TestAccount::Manager->error_mode('return');
    my ($pkg, %args) = @_;

    my $self = {};

    bless $self, $pkg;
    ## Object may be created either with a hashref as argument or an IdPAccountManager::Data::TestAccount object
    ## Second case is usefull when fetching a set of IdPAccountManager::Data::TestAccount via IdPAccountManager::Data::TestAccount::Manager
    if (ref($_[0]) eq 'IdPAccountManager::Data::TestAccount') {
        $self->{'persistent'} = $_[0];
    } else {
        $self->{'persistent'} =
          IdPAccountManager::Data::TestAccount->new(%args);
    my ($self, $attribute) = @_;
    ## User password is not stored in DB
    if ($attribute eq 'user_password') {
        return $self->{$attribute};
    } else {
        return $self->{'persistent'}->$attribute;
    my ($self, %args) = @_;
    ## If no id is defined, it is a new account
    unless (defined $self->{'persistent'}->id) {
        $self->{'persistent'}->creation_date(time);
        $self->{'persistent'}->expiration_date(
            time + ($args{'accounts_validity_period'} * 3600 * 24));
        $self->{'user_password'} =
          IdPAccountManager::Tools::generate_password();
        $self->{'persistent'}->user_password_hash(
            IdPAccountManager::Tools::sha256_hash($self->{'user_password'}));
    unless ($self->{'persistent'}->save()) {
        return undef;
    }
}

## Delete a test account
sub delete {
    my ($self) = @_;
    unless ($self->{'persistent'}->delete()) {
        return undef;
    }
}

## Print the content of a test account
sub print {
    my ($self, $fd) = @_;
    $fd = \*STDOUT unless $fd;
    printf $fd
"Account ID=%s; password_hash=%s; sp_entityid=%s; account_profile=%s; creation_date=%s; expiration_date=%s\n",
      $self->get('id'),          $self->get('user_password_hash'),
      $self->get('sp_entityid'), $self->get('account_profile'),
      POSIX::strftime('%Y:%m:%d', localtime($self->get('creation_date'))),
      POSIX::strftime('%Y:%m:%d', localtime($self->get('expiration_date')));
}

## list all test accounts
## Class method
sub list_test_accounts {
    my (%args) = @_;
    my $persistent_accounts =
      IdPAccountManager::Data::TestAccount::Manager->get_testaccounts(%args);
    my $accounts;
    foreach my $persistent_account (@{$persistent_accounts}) {
        my $account = IdPAccountManager::TestAccount->new($persistent_account);
        push @$accounts, $account;
    }
## create test accounts for all active account profiles
sub create_test_accounts_for_sp {
    my (%args) = @_;
    unless ($args{'sp_entityid'}) {
        return undef;
    }
    foreach my $profile (@{ $args{'account_profiles'} }) {
        my $test_account = IdPAccountManager::TestAccount->new(
            account_profile => $profile,
            sp_entityid     => $args{'sp_entityid'}
        );
        unless (defined $test_account) {
            return undef;
        unless ($test_account->save()) {
            return undef;
        }
        push @test_accounts, $test_account;
    }

    return @test_accounts;
}

__END__

=head1 NAME

IdPAccountManager::TestAccount - Manage test user accounts for the Test Identity Provider

=head1 SYNOPSIS

  my $test_account = IdPAccountManager::TestAccount->new(account_profile => 'student1',
                                                      sp_entityid => 'https://test.federation.renater.fr/test/ressource');
  unless (defined $test_account) {
    die "Failed to create test account";
  }
    
  unless ($test_account->save()) {
    die "Failed to create test account";
  }
    
  printf "Account created:\n\tuserid: user%d\n\tpassword: %s\n", $test_account->get('id'), $test_account->get('user_password');

=head1 DESCRIPTION

The Test Account manager instanciates test accounts associated to a SAML Identity Provider.
This module allows to manage the test accounts.

=head1 SUBROUTINES/METHODS

=over 8

=item C<new ARGS>

Class method. Create a new IdPAccountManager::TestAccount object.
Example:
    
    my $test_account = IdPAccountManager::TestAccount->new(account_profile => 'student1',
                                                          sp_entityid => 'https://test.federation.renater.fr/test/ressource');

Supported arguments include:

=over 12

=item C<account_profile>

ID of the account profile to be used.

=item C<sp_entityid>

EntityID (SAML ID) of the Service Provider associated to the test account.

=back

=item C<create_test_accounts_for_sp ARGS>

Class method. Create test accounts for supported account profiles.

Supported arguments include:

=over 12

=item C<sp_entityid>

EntityID (SAML ID) of the Service Provider associated to the test account.

=back

=item C<delete>

Deletes the test account in the database.

=item C<get> ATTR_NAME

Returns the value of the specified ATTR_NAME attribute of the test account.

=item C<list_test_accounts ARGS>

Class method. List all test accounts in database.

Supported arguments include:

=over 12

=item C<sp_entityid>

Entityid of a SAML Service Provider to list only test accounts linked to this Service Provider.

=item C<account_profile>

Test account profile to list only test accounts linked based on this profile.

=back

=item C<print FD> 

Dumps the content of the test account to the specified FD file handler (default to STDOUT)

=item C<save>

Save the test account in the database.

=back

=head1 AUTHOR

Olivier Salaün (olivier.salaun@renater.fr)

=head1 LICENSE

Copyright (c) GEANT
This software was developed by RENATER. The research leading to these results has received funding
from the European Community¹s Seventh Framework Programme (FP7/2007-2013) under grant agreement nº 238875 (GÉANT).