line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
29
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
28
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
5
|
use base qw(Exporter); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
129
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
require Crypt::Rijndael; |
11
|
|
|
|
|
|
|
require Digest::SHA; |
12
|
|
|
|
|
|
|
require MIME::Base64; |
13
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
71
|
|
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
5
|
use vars qw(@EXPORT @EXPORT_OK); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
676
|
|
16
|
|
|
|
|
|
|
@EXPORT = @EXPORT_OK = qw(_unescape _escape _decode _encode _decrypt _validate _sign _timestamp _split); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub _unescape { |
19
|
0
|
|
|
0
|
|
|
return URI::Escape::uri_unescape( shift ); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub _escape { |
23
|
0
|
|
|
0
|
|
|
return URI::Escape::uri_escape( shift ); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub _decode { |
27
|
0
|
|
|
0
|
|
|
my $decoded = MIME::Base64::decode_base64( shift ); |
28
|
0
|
0
|
0
|
|
|
|
if ( length $decoded <= 16 || (length $decoded) % 16 != 0 ) { |
29
|
0
|
|
|
|
|
|
croak('Unable to decode token'); |
30
|
|
|
|
|
|
|
} |
31
|
0
|
|
|
|
|
|
return $decoded; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _encode { |
35
|
0
|
|
|
0
|
|
|
return MIME::Base64::encode_base64( shift ); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _decrypt { |
39
|
0
|
|
|
0
|
|
|
my $decoded = shift; |
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
|
|
|
0
|
|
|
my $decrypted = shift; |
51
|
0
|
|
|
|
|
|
my $signature_key = shift; |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
my ( $token, $signature ) = split /&sig=/, $decrypted; |
54
|
0
|
0
|
0
|
|
|
|
if ( !$token || !$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
|
0
|
|
|
|
|
if ( $signature ne $compare_signature ) { |
61
|
0
|
|
|
|
|
|
croak('Decrypted token does not match signature'); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
return $token; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _sign { |
68
|
0
|
|
|
0
|
|
|
my ( $token, $signature_key ) = @_; |
69
|
0
|
|
|
|
|
|
return Digest::SHA::hmac_sha256( $token, $signature_key ); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _timestamp { |
73
|
0
|
|
|
0
|
|
|
return time(); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub _split { |
77
|
0
|
|
|
0
|
|
|
my ( $s ) = @_; |
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
|
0
|
|
|
|
|
return wantarray ? %split : \%split; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
1; |
88
|
|
|
|
|
|
|
__END__ |