| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*- | 
| 2 |  |  |  |  |  |  | # vim:ts=8:sw=2:et:sta:sts=2 | 
| 3 |  |  |  |  |  |  | ######### | 
| 4 |  |  |  |  |  |  | # Author: rmp | 
| 5 |  |  |  |  |  |  | # | 
| 6 |  |  |  |  |  |  | package ClearPress::authenticator::session; | 
| 7 | 1 |  |  | 1 |  | 772 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 27 |  | 
| 8 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 22 |  | 
| 9 | 1 |  |  | 1 |  | 375 | use Crypt::CBC; | 
|  | 1 |  |  |  |  | 4067 |  | 
|  | 1 |  |  |  |  | 31 |  | 
| 10 | 1 |  |  | 1 |  | 6 | use base qw(ClearPress::authenticator); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 400 |  | 
| 11 | 1 |  |  | 1 |  | 560 | use Readonly; | 
|  | 1 |  |  |  |  | 3163 |  | 
|  | 1 |  |  |  |  | 46 |  | 
| 12 | 1 |  |  | 1 |  | 7 | use Carp; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 45 |  | 
| 13 | 1 |  |  | 1 |  | 5 | use MIME::Base64 qw(encode_base64 decode_base64); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 71 |  | 
| 14 | 1 |  |  | 1 |  | 377 | use YAML::Tiny qw(Load Dump); | 
|  | 1 |  |  |  |  | 5668 |  | 
|  | 1 |  |  |  |  | 564 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | our $VERSION = q[477.1.2]; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | Readonly::Scalar our $KEY => q[topsecretkey]; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub authen_token { | 
| 21 | 3 |  |  | 3 | 1 | 5811 | my ($self, $token) = @_; | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 3 |  |  |  |  | 18 | return $self->decode_token($token); | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | sub encode_token { | 
| 27 | 1 |  |  | 1 | 1 | 11 | my ($self, $user_hash) = @_; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 1 |  |  |  |  | 6 | my $user_yaml = Dump($user_hash); | 
| 30 | 1 |  |  |  |  | 252 | my $encrypted = $self->cipher->encrypt($user_yaml); | 
| 31 | 1 |  |  |  |  | 1364 | my $encoded   = encode_base64($encrypted); | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 1 |  |  |  |  | 5 | return $encoded; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub decode_token { | 
| 37 | 4 |  |  | 4 | 1 | 17 | my ($self, $token) = @_; | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 4 |  |  |  |  | 20 | my $decoded = q[]; | 
| 40 |  |  |  |  |  |  | eval { | 
| 41 | 4 |  |  |  |  | 28 | $decoded = decode_base64($token); | 
| 42 | 4 | 50 |  |  |  | 10 | } or do { | 
| 43 | 0 |  |  |  |  | 0 | carp q[Failed to decode token]; | 
| 44 | 0 |  |  |  |  | 0 | return; | 
| 45 |  |  |  |  |  |  | }; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 4 |  |  |  |  | 12 | my $decrypted = q[]; | 
| 48 |  |  |  |  |  |  | eval { | 
| 49 | 4 |  |  |  |  | 13 | $decrypted = $self->cipher->decrypt($decoded); | 
| 50 | 4 | 100 |  |  |  | 9 | } or do { | 
| 51 | 1 |  |  |  |  | 473 | carp q[Failed to decrypt token]; | 
| 52 | 1 |  |  |  |  | 134 | return; | 
| 53 |  |  |  |  |  |  | }; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 3 |  |  |  |  | 1146 | my $deyamled; | 
| 56 |  |  |  |  |  |  | eval { | 
| 57 | 3 |  |  |  |  | 13 | $deyamled = Load($decrypted); | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 3 | 100 |  |  |  | 9 | } or do { | 
| 60 | 1 |  |  |  |  | 465 | carp q[Failed to de-YAML token]; | 
| 61 | 1 |  |  |  |  | 111 | return; | 
| 62 |  |  |  |  |  |  | }; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 2 |  |  |  |  | 784 | return $deyamled; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub key { | 
| 68 | 8 |  |  | 8 | 1 | 33 | my ($self, $key) = @_; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 8 | 100 |  |  |  | 26 | if($key) { | 
| 71 | 1 |  |  |  |  | 4 | $self->{key} = $key; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 8 | 100 |  |  |  | 25 | if($self->{key}) { | 
| 75 | 3 |  |  |  |  | 12 | return $self->{key}; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 5 |  |  |  |  | 32 | return $KEY; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | sub cipher { | 
| 82 | 8 |  |  | 8 | 1 | 684 | my $self = shift; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 8 | 100 |  |  |  | 39 | if(!$self->{cipher}) { | 
| 85 | 4 |  |  |  |  | 16 | $self->{cipher} = Crypt::CBC->new( | 
| 86 |  |  |  |  |  |  | -cipher => 'Blowfish', | 
| 87 |  |  |  |  |  |  | -key    => $self->key, | 
| 88 |  |  |  |  |  |  | ); | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 8 |  |  |  |  | 2548 | return $self->{cipher}; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | 1; | 
| 95 |  |  |  |  |  |  | __END__ |