| 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
| |||||||||||||
| 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
| |||||||||||||
| 21 | |||||||||||||
| 22 | 5 | ▶ | $padding_character = $settings->{padding_character} || '!'; | ||||||||||
|
Condition50%1/2
$settings->{'padding_character'} || '!'
MC/DC0%0/1
$settings->{'padding_character'} || '!'
$settings->{'padding_character'}
Truth table50%1/2
A
A= $settings->{'padding_character'}
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 35 | |||||||||||||
| 36 | 139 | ▶ | _create_cipher() unless $cipher; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 42 | |||||||||||||
| 43 | 138 | ▶ | while ( length($cleartext_id)>0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 58 | |||||||||||||
| 59 | 0 | ▶ | return 0 unless $encoded_id =~ /^[0-9A-F]+$/i; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | 0 | ▶ | return 0 unless length($encoded_id)%16==0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 72 | |||||||||||||
| 73 | 136 | ▶ | die "Invalid Hash-ID value ($encoded_id)" unless $encoded_id =~ /^[0-9A-F]+$/i; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 136 | ▶ | die "Invalid Hash-ID value ($encoded_id) - must be a multiple of 8 bytes (16 hex digits)" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | unless length($encoded_id)%16==0; | ||||||||||||
| 76 | |||||||||||||
| 77 | 136 | my $cleartext = ""; | |||||||||||
| 78 | |||||||||||||
| 79 | 136 | ▶ | while ( length($encoded_id)>0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 92 | ## Ensure the decoded ID contains the prefix | ||||||||||||
| 93 | 2 | my $i = index $cleartext,$prefix; | |||||||||||
| 94 | 2 | ▶ | if ($i != 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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__ | ||||||||||||