File Coverage

blib/lib/Web/MicroID.pm
Criterion Covered Total %
statement 77 77 100.0
branch 19 24 79.1
condition 6 7 85.7
subroutine 17 17 100.0
pod 11 11 100.0
total 130 136 95.5


line stmt bran cond sub pod time code
1             package Web::MicroID;
2              
3 1     1   23319 use 5.008008;
  1         4  
  1         33  
4 1     1   5 use strict;
  1         2  
  1         41  
5 1     1   5 use warnings;
  1         6  
  1         45  
6 1     1   7 use Carp;
  1         8  
  1         89  
7 1     1   754 use Digest::SHA1;
  1         1983  
  1         47  
8 1     1   6 use Digest::MD5;
  1         2  
  1         900  
9              
10             our $VERSION = '0.02';
11              
12             =pod
13              
14             =head1 NAME
15              
16             Web::MicroID - An implementation of the MicroID standard
17              
18             =head1 VERSION
19              
20             This documentation refers to Web::MicroID version 0.02
21              
22             =head1 SYNOPSIS
23              
24             use Web::MicroID;
25              
26             $id = Web::MicroID->new();
27              
28             $id->individual('mailto:user@domain.tld');
29             $id->serv_prov('http://domain.tld/');
30              
31             =cut
32              
33             sub individual {
34 6     6 1 16 my $self = shift;
35 6         10 my $id = shift;
36              
37 6 100       16 if ($id) {
38 4 50       15 croak 'individual() not in the correct format' unless $id =~/:/;
39              
40             # Set ID, split it into parts and set them too
41 4         8 $self->[0]->{individual} = $id;
42             (
43 4         27 $self->[0]->{indv_uri}, $self->[0]->{indv_val}
44             ) = split /\:\/*/, $id;
45             }
46              
47             # Get any ID we may have
48 6         29 return $self->[0]->{individual};
49             }
50             sub indv_uri {
51 2     2 1 5 my $self = shift;
52              
53             # Get the URI of any ID we may have
54 2         10 return $self->[0]->{indv_uri};
55             }
56             sub indv_val {
57 2     2 1 4 my $self = shift;
58              
59             # Get the URI value of any ID we may have
60 2         16 return $self->[0]->{indv_val};
61             }
62             sub serv_prov {
63 6     6 1 13 my $self = shift;
64 6         8 my $id = shift;
65              
66 6 100       16 if ($id) {
67 4 50       14 croak 'serv_prov() not in the correct format' unless $id =~/:/;
68            
69             # Set ID, split it into parts and set them too
70 4         8 $self->[0]->{serv_prov} = $id;
71             (
72 4         22 $self->[0]->{serv_prov_uri}, $self->[0]->{serv_prov_val}
73             ) = split /\:\/*/, $id;
74 4         16 $self->[0]->{serv_prov_val} =~ s/\/$//;
75             }
76              
77             # Get any ID we may have
78 6         19 return $self->[0]->{serv_prov};
79             }
80             sub serv_prov_uri {
81 2     2 1 5 my $self = shift;
82              
83             # Get the URI of any ID we may have
84 2         11 return $self->[0]->{serv_prov_uri};
85             }
86             sub serv_prov_val {
87 2     2 1 5 my $self = shift;
88              
89             # Get the URI value of any ID we may have
90 2         11 return $self->[0]->{serv_prov_val};
91             }
92             sub algorithm {
93 5     5 1 9 my $self = shift;
94 5         12 my $id = shift;
95              
96             # Change the algorithm if a new one is provided
97 5   100     24 $self->[0]->{algorithm} = $id || 'sha1';
98              
99             # Get the alogorithm we're using
100 5         18 return $self->[0]->{algorithm};
101             }
102              
103             =pod
104              
105             # Generate a MicroID token
106             $micro_id = $id->generate();
107              
108             =cut
109              
110             sub generate {
111 9     9 1 14 my $self = shift;
112 9         11 my $id = $self->[0];
113              
114             # Check state
115 9 50       22 croak 'Must set individual() before calling generate()'
116             unless $id->{individual};
117 9 50       29 croak 'Must set serv_prov() before calling generate()'
118             unless $id->{serv_prov};
119 9 100       26 individual($self, $id->{individual}) unless $id->{indv_uri};
120 9 100       21 serv_prov($self, $id->{serv_prov}) unless $id->{serv_prov_uri};
121 9 100       21 algorithm($self) unless $id->{algorithm};
122              
123             # Call the correct algorithm constructor
124 9         7 my $algor;
125 9 100       20 if ($id->{algorithm} eq 'md5') {$algor = Digest::MD5->new}
  2         14  
  7         35  
126             else {$algor = Digest::SHA1->new}
127              
128             # Hash the ID's
129 9         72 my $indv = $algor->add($id->{individual})->hexdigest;
130 9         39 $algor->reset;
131 9         36 my $serv = $algor->add($id->{serv_prov} )->hexdigest;
132 9         599 $algor->reset;
133              
134             # Hash the ID's together and set as the legacy MicroID token
135 9         57 $self->[0]->{legacy} = $algor->add($indv . $serv)->hexdigest;
136              
137             # Assemble the MicroID token and set it
138 9         30 my $micro_id = join ':', (
139             $id->{indv_uri} . '+' . $id->{serv_prov_uri},
140             $id->{algorithm},
141             $self->[0]->{legacy}
142             );
143 9         17 $self->[0]->{micro_id} = $micro_id;
144              
145             # Get the MicroID token
146 9         40 return $micro_id;
147             }
148             sub legacy {
149 2     2 1 5 my $self = shift;
150              
151             # Get any legacy MicroID token
152 2         26 return $self->[0]->{legacy};
153             }
154              
155             =pod
156              
157             # Process (validate) a MicroID token
158             $test = $id->process($micro_id);
159              
160             =cut
161              
162             sub process {
163 6     6 1 13 my $self = shift;
164 6   66     19 my $process = shift || $self->[0]->{process};
165              
166 6 50       11 croak 'Must set process() before calling process()' unless $process;
167              
168 6         19 my @verify = split /:/, $process;
169 6         11 generate($self);
170 6 100       66 return 1 if pop @verify eq $self->[0]->{legacy};
171 1         4 return;
172             }
173             sub new {
174 4     4 1 17 my $class = shift;
175 4   100     18 my $conf = shift || {};
176 4         14 my $self = bless [$conf], $class;
177 4         11 return $self;
178             }
179              
180             __END__