blib/lib/Dancer/Plugin/EncodeID.pm

filter: Statementstmt 94.0% 63 / 67 Branchbran 68.4% 26 / 38 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 88.8% 8 / 9 Podpod n/a totaltotal 83.7% 98 / 117 CC 21 SCAR 20.5
CC
21
Cov
84%
__ANON__
20.0
__ANON__
8.0
__ANON__
7.0
CRAP 7.8
↑ Summary
1 package Dancer::Plugin::EncodeID;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 our $VERSION = '0.02';
6  
7 7 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Crypt::Blowfish;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 my $cipher = undef ;
12 7 my $padding_character = '!';
13  
14 6 ▶sub _create_cipher {
Subroutine _create_cipher: called CC=5 SCAR=16.7
15 ## Crate a new cipher, based on the 'secret' in the configuration
16 6 ▶ my $settings = plugin_setting or
Branch: 1 branch
BranchTrueFalseTotal
my $settings = plugin_setting() or die "Configuration Error: can't find plugin settings for Dancer::Plugin::EncodeID. Please see documentation regarding proper configuration." 6 0 6
17 die "Configuration Error: can't find plugin settings for Dancer::Plugin::EncodeID. Please see documentation regarding proper configuration.";
18  
19 my $secret = $settings->{secret} or
20 6 ▶ die "Configuration Error: can't find 'secret' key settings for Dancer::Plugin::EncodeID. Please see documentation regarding proper configuration.";
Branch: 1 branch
BranchTrueFalseTotal
my $secret = $settings->{"secret"} or die "Configuration Error: can't find 'secret' key settings for Dancer::Plugin::EncodeID. Please see documentation regarding proper configuration." 5 1 6
21  
22 5 ▶ $padding_character = $settings->{padding_character} || '!';
Condition50%1/2
$settings->{'padding_character'} || '!'
l!l
05
MC/DC0%0/1
$settings->{'padding_character'} || '!'
$settings->{'padding_character'}
Truth table50%1/2
A
A= $settings->{'padding_character'}
Aresult
11
00
23 5 ▶ die "Configuration error: padding_character must be 1 character long for Dancer::Plugin::EncodeID. Please see documentation regarding proper configuration." unless length($padding_character)==1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $padding_character == 1 0 5 5
24  
25 5 $cipher = new Crypt::Blowfish ( $secret ) ;
26 }
27  
28 139 ▶register encode_id => sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
29 139 my $cleartext_id = shift;
30 139 ▶ die "Missing Clear text ID parameter" unless defined $cleartext_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cleartext_id 0 139 139
31  
32 ## Prefix is optional, can be undef
33 139 my $prefix = shift ;
34 139 ▶ $cleartext_id = $prefix . $cleartext_id if defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prefix 2 137 139
35  
36 139 ▶ _create_cipher() unless $cipher;
Branch: 1 branch
BranchTrueFalseTotal
unless $cipher 5 134 139
37  
38 138 my $hash_id = "" ;
39  
40 #Special case - user asked to encode an empty string
41 138 ▶ $cleartext_id = $padding_character x '8' if length($cleartext_id)==0;
Branch: 1 branch
BranchTrueFalseTotal
if length $cleartext_id == 0 1 137 138
42  
43 138 ▶ while ( length($cleartext_id)>0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $cleartext_id > 0) 253 138 391
44 253 my $sub_text = substr($cleartext_id,0,8,'');
45 253 my $padded_str_id = $sub_text;
46 253 ▶ if (length($sub_text)<8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $sub_text < 8) 43 210 253
47 43 $padded_str_id = ( $padding_character x (8- length($sub_text) % 8 ) ). $sub_text ;
48 };
49 #print STDERR "Encoding '$padded_str_id'\n";
50 253 my $ciphertext = $cipher->encrypt($padded_str_id);
51 253 $hash_id .= unpack('H*', $ciphertext ) ;
52 }
53 138 return $hash_id;
54 7 };
55  
56 0 ▶register valid_encoded_id => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
57 0 ▶ my $encoded_id = shift or die "Missing Encoded ID parameter";
Branch: 1 branch
BranchTrueFalseTotal
my $encoded_id = shift() or die "Missing Encoded ID parameter" 0 0 0
58  
59 0 ▶ return 0 unless $encoded_id =~ /^[0-9A-F]+$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $encoded_id =~ /^[0-9A-F]+$/i 0 0 0
60 0 ▶ return 0 unless length($encoded_id)%16==0;
Branch: 1 branch
BranchTrueFalseTotal
unless length($encoded_id) % 16 == 0 0 0 0
61 0 return 1;
62 7 };
63  
64 136 ▶register decode_id => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
65 136 ▶ my $encoded_id = shift or die "Missing Encoded ID parameter";
Branch: 1 branch
BranchTrueFalseTotal
my $encoded_id = shift() or die "Missing Encoded ID parameter" 136 0 136
66 136 my $orig_encoded_id = $encoded_id;
67  
68 ## Prefix is optional, can be undef
69 136 my $prefix = shift ;
70  
71 136 ▶ _create_cipher() unless $cipher;
Branch: 1 branch
BranchTrueFalseTotal
unless $cipher 1 135 136
72  
73 136 ▶ die "Invalid Hash-ID value ($encoded_id)" unless $encoded_id =~ /^[0-9A-F]+$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $encoded_id =~ /^[0-9A-F]+$/i 0 136 136
74 136 ▶ die "Invalid Hash-ID value ($encoded_id) - must be a multiple of 8 bytes (16 hex digits)"
Branch: 1 branch
BranchTrueFalseTotal
unless length($encoded_id) % 16 == 0 0 136 136
75 unless length($encoded_id)%16==0;
76  
77 136 my $cleartext = "";
78  
79 136 ▶ while ( length($encoded_id)>0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $encoded_id > 0) 251 136 387
80 251 my $sub_text = substr($encoded_id,0,16,'');
81 251 my @list = $sub_text =~ /([0-9A-F]{2})/gi;
82 #print STDERR "Decoding: '$sub_text'\n";
83 251 my $ciphertext = pack('H2' x scalar(@list), @list) ;
84  
85 251 my $text = $cipher->decrypt($ciphertext);
86 251 $text =~ s/^$padding_character+//;
87 #print STDERR "Decoded: '$text'\n";
88 251 $cleartext .= $text;
89 };
90  
91 136 ▶ if (defined $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix) 2 134 136
92 ## Ensure the decoded ID contains the prefix
93 2 my $i = index $cleartext,$prefix;
94 2 ▶ if ($i != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i != 0) 1 1 2
95 1 die "Invalid Hash-ID value ($orig_encoded_id) - bad prefix" ;
96 }
97 #skip the prefix;
98 1 $cleartext = substr $cleartext, length($prefix);
99 }
100  
101 135 return $cleartext;
102 7 };
103  
104 7 register_plugin;
105  
106 # ABSTRACT: A Dancer plugin for Encoding/Obfuscating IDs in URLs
107  
108 7 1;
109 __END__
↑ Summary