Finance-Bank-Cahoot
view release on metacpan or search on metacpan
lib/Finance/Bank/Cahoot/CredentialsProvider/CryptFile.pm view on Meta::CPAN
# Copyright (c) 2007 Jon Connell.
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
package Finance::Bank::Cahoot::CredentialsProvider::CryptFile;
use base qw(Finance::Bank::Cahoot::CredentialsProvider);
use strict;
use warnings 'all';
use vars qw($VERSION);
$VERSION = '1.07';
use Carp qw(croak);
use Crypt::CBC;
use English '-no_match_vars';
use File::Slurp qw(slurp);
use IO::File;
sub _init
{
my ($self, $options) = @_;
croak 'No key provided' if not defined $options->{key};
my $cipher = Crypt::CBC->new(-key => $options->{key},
-cipher => 'DES_PP');
my $keyfile = $options->{keyfile};
$keyfile = $ENV{HOME}.'/.cahoot' if not defined $keyfile;
if (-e $keyfile) {
my $fh = new IO::File $keyfile, 'r'
or croak "Can't open $keyfile for reading: $OS_ERROR";
my $data = slurp $fh;
my $plaintext = $cipher->decrypt($data);
for (split /\n/, $plaintext) {
my ($k, $v) = split /\t/;
$self->{$k} = $v;
}
$fh->close;
}
if (defined $options->{fallback}) {
my $fallback_class = 'Finance::Bank::Cahoot::CredentialsProvider::'.$options->{fallback};
eval "use $fallback_class"; ## no critic
croak 'Invalid fallback provider '.$options->{fallback} if $EVAL_ERROR;
my $fallback_args = { credentials => $self->{_credentials},
options => $options->{fallback_options} };
eval "\$self->{_fallback} = $fallback_class->new(\%{\$fallback_args})"; ## no critic
croak 'Fallback provider '.$options->{fallback}.' failed to initialise' if $EVAL_ERROR;
}
my $do_update = 0;
foreach my $credential (@{$self->{_credentials}}) {
if (not defined $self->{$credential}) {
croak 'No fallback provider given and '.$credential.' is not in keyfile'
if not defined $self->{_fallback};
$self->{$credential} = $self->{_fallback}->$credential;
$do_update = 1;
}
}
if ($do_update) {
my $fh = new IO::File $keyfile, 'w'
or croak "Can't open $keyfile for writing: $OS_ERROR";
my @ciphers;
foreach my $credential (@{$self->{_credentials}}) {
push @ciphers, $credential."\t".$self->{$credential};
}
my $ciphertext = $cipher->encrypt(join "\n", @ciphers);
$fh->print($ciphertext);
$fh->close;
}
return $self;
}
sub get
{
my ($self, $credential, $offset) = @_;
return substr $self->{$credential}, $offset, 1
if defined $offset;
return $self->{$credential};
}
( run in 0.628 second using v1.01-cache-2.11-cpan-df04353d9ac )