| 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 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Crypt::CBC; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use base qw(ClearPress::authenticator); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use MIME::Base64 qw(encode_base64 decode_base64); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | ▶ | use YAML::Tiny qw(Load Dump); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 2 | our $VERSION = q[2026.07.25]; | |||||||||
| 17 | |||||||||||
| 18 | 2 | Readonly::Scalar our $KEY => q[topsecretkey]; | |||||||||
| 19 | |||||||||||
| 20 | 3 | ▶ | sub authen_token { | ||||||||
|
Subroutine
authen_token: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | 3 | my ($self, $token) = @_; | |||||||||
| 22 | |||||||||||
| 23 | 3 | return $self->decode_token($token); | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 1 | ▶ | sub encode_token { | ||||||||
|
Subroutine
encode_token: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 27 | 1 | my ($self, $user_hash) = @_; | |||||||||
| 28 | |||||||||||
| 29 | 1 | my $user_yaml = Dump($user_hash); | |||||||||
| 30 | 1 | my $encrypted = $self->cipher->encrypt($user_yaml); | |||||||||
| 31 | 1 | my $encoded = encode_base64($encrypted); | |||||||||
| 32 | |||||||||||
| 33 | 1 | return $encoded; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 4 | ▶ | sub decode_token { | ||||||||
|
Subroutine
decode_token: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 37 | 4 | my ($self, $token) = @_; | |||||||||
| 38 | |||||||||||
| 39 | 4 | my $decoded = q[]; | |||||||||
| 40 | eval { | ||||||||||
| 41 | 4 | $decoded = decode_base64($token); | |||||||||
| 42 | 4 | ▶ | } or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | carp q[Failed to decode token]; | |||||||||
| 44 | 0 | return; | |||||||||
| 45 | }; | ||||||||||
| 46 | |||||||||||
| 47 | 4 | my $decrypted = q[]; | |||||||||
| 48 | eval { | ||||||||||
| 49 | 4 | $decrypted = $self->cipher->decrypt($decoded); | |||||||||
| 50 | 4 | ▶ | } or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 1 | carp q[Failed to decrypt token]; | |||||||||
| 52 | 1 | return; | |||||||||
| 53 | }; | ||||||||||
| 54 | |||||||||||
| 55 | 3 | my $deyamled; | |||||||||
| 56 | eval { | ||||||||||
| 57 | 3 | $deyamled = Load($decrypted); | |||||||||
| 58 | |||||||||||
| 59 | 3 | ▶ | } or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 1 | carp q[Failed to de-YAML token]; | |||||||||
| 61 | 1 | return; | |||||||||
| 62 | }; | ||||||||||
| 63 | |||||||||||
| 64 | 2 | return $deyamled; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 8 | ▶ | sub key { | ||||||||
|
Subroutine
key: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 68 | 8 | my ($self, $key) = @_; | |||||||||
| 69 | |||||||||||
| 70 | 8 | ▶ | if($key) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 1 | $self->{key} = $key; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 8 | ▶ | if($self->{key}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 3 | return $self->{key}; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 5 | return $KEY; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 8 | ▶ | sub cipher { | ||||||||
|
Subroutine
cipher: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 82 | 8 | my $self = shift; | |||||||||
| 83 | |||||||||||
| 84 | 8 | ▶ | if(!$self->{cipher}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 4 | $self->{cipher} = Crypt::CBC->new( | |||||||||
| 86 | -key => $self->key, | ||||||||||
| 87 | ); | ||||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 8 | return $self->{cipher}; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 2 | 1; | |||||||||
| 94 | __END__ | ||||||||||