ClearPress::authenticator::session - ClearPress::authenticator::session documentation


ClearPress documentation Contained in the ClearPress distribution.

Index


Code Index:

NAME

Top

ClearPress::authenticator::session

VERSION

Top

$LastChangedRevision: 348 $

SYNOPSIS

Top

DESCRIPTION

Top

SUBROUTINES/METHODS

Top

authen_token - validate a token, usually from cookie

  my @aResults = $oSession->authen_token($sToken);

encode_token - encrypt and base64 encode user information

  my $sEncoded = $oSession->encode_token($hrUserData);

decode_token - decode and decrypt a token

  my $hrUserData = $oSession->decode_token($sEncoded);

key - get/set accessor for cipher key (optionally configured during construction)

  my $sKey = $oSession->key();

cipher - a configure Crypt::CBC object

  my $oCipher = $oSession->cipher();

DIAGNOSTICS

Top

CONFIGURATION AND ENVIRONMENT

Top

DEPENDENCIES

Top

strict
warnings
Crypt::CBC
base
ClearPress::authenticator
Readonly
Carp
MIME::Base64
YAML::Syck

INCOMPATIBILITIES

Top

BUGS AND LIMITATIONS

Top

AUTHOR

Top

$Author: Roger Pettett$

LICENSE AND COPYRIGHT

Top


ClearPress documentation Contained in the ClearPress distribution.

#########
# Author:        rmp
# Last Modified: $Date: 2010-01-04 13:02:42 +0000 (Mon, 04 Jan 2010) $
# Id:            $Id: session.pm 348 2010-01-04 13:02:42Z zerojinx $
# Source:        $Source$
# $HeadURL: https://clearpress.svn.sourceforge.net/svnroot/clearpress/trunk/lib/ClearPress/authenticator/session.pm $
#
package ClearPress::authenticator::session;
use strict;
use warnings;
use Crypt::CBC;
use base qw(ClearPress::authenticator);
use Readonly;
use Carp;
use MIME::Base64 qw(encode_base64 decode_base64);
use YAML::Tiny qw(Load Dump);

our $VERSION = do { my ($r) = q$Revision: 348 $ =~ /(\d+)/smx; $r; };

Readonly::Scalar our $KEY => q[topsecretkey];

sub authen_token {
  my ($self, $token) = @_;

  return $self->decode_token($token);
}

sub encode_token {
  my ($self, $user_hash) = @_;

  my $user_yaml = Dump($user_hash);
  my $encrypted = $self->cipher->encrypt($user_yaml);
  my $encoded   = encode_base64($encrypted);

  return $encoded;
}

sub decode_token {
  my ($self, $token) = @_;

  my $decoded = q[];
  eval {
    $decoded = decode_base64($token);
  } or do {
    carp q[Failed to decode token];
    return;
  };

  my $decrypted = q[];
  eval {
    $decrypted = $self->cipher->decrypt($decoded);
  } or do {
    carp q[Failed to decrypt token];
    return;
  };

  my $deyamled;
  eval {
    $deyamled = Load($decrypted);

  } or do {
    carp q[Failed to de-YAML token];
    return;
  };

  return $deyamled;
}

sub key {
  my ($self, $key) = @_;

  if($key) {
    $self->{key} = $key;
  }

  if($self->{key}) {
    return $self->{key};
  }

  return $KEY;
}

sub cipher {
  my $self = shift;

  if(!$self->{cipher}) {
    $self->{cipher} = Crypt::CBC->new(
				      -cipher => 'Blowfish',
				      -key    => $self->key,
				     );
  }

  return $self->{cipher};
}

1;
__END__