| 1 | # Copyright 2008 Andrew M. Jenkinson, all rights reserved | ||||||||||||||||||||||||||||
| 2 | # Use and/or distribution is permitted only with prior consent | ||||||||||||||||||||||||||||
| 3 | package WWW::Live::Auth::Utils; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | require Crypt::Rijndael; | |||||||||||||||||||||||||||
| 11 | 1 | require Digest::SHA; | |||||||||||||||||||||||||||
| 12 | 1 | require MIME::Base64; | |||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use vars qw(@EXPORT @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | @EXPORT = @EXPORT_OK = qw(_unescape _escape _decode _encode _decrypt _validate _sign _timestamp _split); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub _unescape { | ||||||||||||||||||||||||||||
| 19 | 0 | ▶ | return URI::Escape::uri_unescape( shift ); | ||||||||||||||||||||||||||
|
Subroutine
_unescape: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub _escape { | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | return URI::Escape::uri_escape( shift ); | ||||||||||||||||||||||||||
|
Subroutine
_escape: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub _decode { | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $decoded = MIME::Base64::decode_base64( shift ); | ||||||||||||||||||||||||||
|
Subroutine
_decode: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | if ( length $decoded <= 16 || (length $decoded) % 16 != 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $decoded <= 16 or length($decoded) % 16 != 0
MC/DC0%0/2
length $decoded <= 16 or length($decoded) % 16 != 0
length $decoded <= 16
length($decoded) % 16 != 0
Truth table0%0/3
A or B
A= length $decoded <= 16B= length($decoded) % 16 != 0
| |||||||||||||||||||||||||||||
| 29 | 0 | croak('Unable to decode token'); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 0 | return $decoded; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub _encode { | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | return MIME::Base64::encode_base64( shift ); | ||||||||||||||||||||||||||
|
Subroutine
_encode: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub _decrypt { | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $decoded = shift; | ||||||||||||||||||||||||||
|
Subroutine
_decrypt: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 40 | 0 | my $encryption_key = shift; | |||||||||||||||||||||||||||
| 41 | 0 | my $iv = substr($decoded, 0, 16); | |||||||||||||||||||||||||||
| 42 | 0 | my $encrypted = substr($decoded, 16); | |||||||||||||||||||||||||||
| 43 | 0 | my $cipher = Crypt::Rijndael->new( $encryption_key, | |||||||||||||||||||||||||||
| 44 | Crypt::Rijndael::MODE_CBC() ); | ||||||||||||||||||||||||||||
| 45 | 0 | $cipher->set_iv($iv); | |||||||||||||||||||||||||||
| 46 | 0 | return $cipher->decrypt( $encrypted ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub _validate { | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my $decrypted = shift; | ||||||||||||||||||||||||||
|
Subroutine
_validate: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 51 | 0 | my $signature_key = shift; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my ( $token, $signature ) = split /&sig=/, $decrypted; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ( !$token || !$signature ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$token and $signature
MC/DC0%0/2
$token and $signature
$token
$signature
Truth table0%0/3
A and B
A= $tokenB= $signature
| |||||||||||||||||||||||||||||
| 55 | 0 | croak('Unable to validate decrypted token'); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 0 | $signature = _decode( _unescape( $signature ) ); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | my $compare_signature = _sign( $token, $signature_key ); | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ( $signature ne $compare_signature ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | croak('Decrypted token does not match signature'); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | return $token; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub _sign { | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | my ( $token, $signature_key ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_sign: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 69 | 0 | return Digest::SHA::hmac_sha256( $token, $signature_key ); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub _timestamp { | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | return time(); | ||||||||||||||||||||||||||
|
Subroutine
_timestamp: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub _split { | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my ( $s ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_split: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 78 | 0 | my %split = (); | |||||||||||||||||||||||||||
| 79 | 0 | my @pairs = split /&/, $s; | |||||||||||||||||||||||||||
| 80 | 0 | for ( @pairs ) { | |||||||||||||||||||||||||||
| 81 | 0 | my ( $key, $val ) = split /=/, $_; | |||||||||||||||||||||||||||
| 82 | 0 | $split{$key} = $val; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | return wantarray ? %split : \%split; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | 1; | |||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||