| 1 | package Validate::Yubikey; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Validate::Yubikey - Validate Yubikey OTPs | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | use Validate::Yubikey; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | sub validate_callback { | ||||||||||||||||||||||||||||
| 14 | my $public_id = shift; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | return { | ||||||||||||||||||||||||||||
| 17 | iid => $iid, | ||||||||||||||||||||||||||||
| 18 | key => $key, | ||||||||||||||||||||||||||||
| 19 | count => $count, | ||||||||||||||||||||||||||||
| 20 | use => $use, | ||||||||||||||||||||||||||||
| 21 | lastuse => $lastuse, | ||||||||||||||||||||||||||||
| 22 | lastts => $lastts, | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub update_callback { | ||||||||||||||||||||||||||||
| 27 | my ($public_id, $data) = @_; | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub log_message { | ||||||||||||||||||||||||||||
| 31 | print shift, "\n"; | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | my $yubi = Validate::Yubikey->new( | ||||||||||||||||||||||||||||
| 35 | callback => \&validate_callback, | ||||||||||||||||||||||||||||
| 36 | update_callback => \&update_callback, | ||||||||||||||||||||||||||||
| 37 | log_callback => \&log_message, | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | my $otp_valid = $yubi->validate("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | The Yubikey is a hardware OTP token produced by Yubico (L<http://www.yubico.com>). | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | This module provides validation of Yubikey OTPs. It relies on you to specify | ||||||||||||||||||||||||||||
| 47 | callback functions that handle retrieving token information from somewhere and | ||||||||||||||||||||||||||||
| 48 | updating the persistent information associated with each token. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | use Crypt::Rijndael; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub hex2modhex { | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my $s = shift; | ||||||||||||||||||||||||||
|
Subroutine
hex2modhex: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 0 | $s =~ tr/0123456789abcdef/cbdefghijklnrtuv/; | |||||||||||||||||||||||||||
| 58 | 0 | return $s; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub modhex2hex { | ||||||||||||||||||||||||||||
| 62 | 1 | ▶ | my $s = shift; | ||||||||||||||||||||||||||
|
Subroutine
modhex2hex: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 1 | $s =~ tr/cbdefghijklnrtuv/0123456789abcdef/; | |||||||||||||||||||||||||||
| 64 | 1 | return $s; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub yubicrc { | ||||||||||||||||||||||||||||
| 68 | 1 | ▶ | my $data = shift; | ||||||||||||||||||||||||||
|
Subroutine
yubicrc: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 1 | my $crc = 0xffff; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | foreach my $h (unpack('H2' x 16, $data)) { | |||||||||||||||||||||||||||
| 72 | 16 | my $d = hex($h); | |||||||||||||||||||||||||||
| 73 | 16 | $crc = $crc ^ ($d & 0xff); | |||||||||||||||||||||||||||
| 74 | 16 | for (1..8) { | |||||||||||||||||||||||||||
| 75 | 128 | my $n = $crc & 1; | |||||||||||||||||||||||||||
| 76 | 128 | $crc = $crc >> 1; | |||||||||||||||||||||||||||
| 77 | 128 | ▶ | if ($n != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 64 | $crc = $crc ^ 0x8408; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | return $crc; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head1 METHODS | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 new | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Create a new Validate::Yubikey instance. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =over 4 | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =item callback | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Required. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =item update_callback | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Required. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =item log_callback | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Optional. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =back | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | sub new { | ||||||||||||||||||||||||||||
| 111 | 1 | ▶ | my ($class, %data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 1 | my $self = {}; | |||||||||||||||||||||||||||
| 113 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 1 | ▶ | if (exists $data{callback} && ref($data{callback}) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $data{"callback"} and ref $data{"callback"} eq "CODE"
MC/DC0%0/2
exists $data{"callback"} and ref $data{"callback"} eq "CODE"
exists $data{"callback"}
ref $data{"callback"} eq "CODE"
Truth table33%1/3
A and B
A= exists $data{"callback"}B= ref $data{"callback"} eq "CODE"
| |||||||||||||||||||||||||||||
| 116 | 1 | $self->{callback} = $data{callback}; | |||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||
| 118 | 0 | croak __PACKAGE__, '->new called without callback'; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | ▶ | if (exists $data{update_callback} && ref($data{update_callback}) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $data{"update_callback"} and ref $data{"update_callback"} eq "CODE"
MC/DC0%0/2
exists $data{"update_callback"} and ref $data{"update_callback"} eq "CODE"
exists $data{"update_callback"}
ref $data{"update_callback"} eq "CODE"
Truth table33%1/3
A and B
A= exists $data{"update_callback"}B= ref $data{"update_callback"} eq "CODE"
| |||||||||||||||||||||||||||||
| 122 | 1 | $self->{update_callback} = $data{update_callback}; | |||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||
| 124 | 0 | croak __PACKAGE__, '->new called without update_callback'; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | ▶ | if (exists $data{log_callback} && ref($data{log_callback}) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $data{"log_callback"} and ref $data{"log_callback"} eq "CODE"
MC/DC0%0/2
exists $data{"log_callback"} and ref $data{"log_callback"} eq "CODE"
exists $data{"log_callback"}
ref $data{"log_callback"} eq "CODE"
Truth table33%1/3
A and B
A= exists $data{"log_callback"}B= ref $data{"log_callback"} eq "CODE"
| |||||||||||||||||||||||||||||
| 128 | 1 | $self->{log_callback} = $data{log_callback}; | |||||||||||||||||||||||||||
| 129 | } else { | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $self->{log_callback} = sub {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 1 | ▶ | if ($data{max_age}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | $self->{max_age} = $data{max_age}; | |||||||||||||||||||||||||||
| 135 | } else { | ||||||||||||||||||||||||||||
| 136 | 1 | $self->{max_age} = 60; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | return $self; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 validate | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =over 4 | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =item Arguments: $otp, @callback_args | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item Return Value: $success | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =back | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Validate an OTP. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | sub validate { | ||||||||||||||||||||||||||||
| 157 | 1 | ▶ | my ($self, $otp, @cbargs) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=13 SCAR=33.7
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1 | ▶ | if ($otp =~ /^([cbdefghijklnrtuv]{0,16})([cbdefghijklnrtuv]{32})$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 1 | my ($public_id, $cipher) = ($1, $2); | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | my $token = $self->{callback}->($public_id, @cbargs); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 1 | ▶ | if (!$token) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | $self->{log_callback}->(sprintf('callback returned no token for pid %s', $public_id)); | |||||||||||||||||||||||||||
| 166 | 0 | return 0; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 1 | foreach my $k (qw/key count iid lastuse lastts use/) { | |||||||||||||||||||||||||||
| 170 | 6 | ▶ | if (!defined($token->{$k})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | carp "callback didn't return $k"; | |||||||||||||||||||||||||||
| 172 | 0 | return 0; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 1 | $cipher = &modhex2hex($cipher); | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 1 | my $crypt = Crypt::Rijndael->new(pack('H*', $token->{key})); | |||||||||||||||||||||||||||
| 179 | 1 | my $plaintext = $crypt->decrypt(pack('H*', $cipher)); | |||||||||||||||||||||||||||
| 180 | 1 | ▶ | unless (length $plaintext) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | carp 'decrypt failed'; | |||||||||||||||||||||||||||
| 182 | 0 | return 0; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 1 | my $plainhex = unpack('H*', $plaintext); | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 1 | ▶ | if (substr($plainhex, 0, length($token->{iid})) eq $token->{iid}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 1 | my $crc = &yubicrc($plaintext); | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 1 | ▶ | if ($crc == 0xf0b8) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 1 | my $count = hex(substr($plainhex, 14, 2).substr($plainhex, 12, 2)); | |||||||||||||||||||||||||||
| 191 | 1 | my $use = hex(substr($plainhex, 22, 2)); | |||||||||||||||||||||||||||
| 192 | 1 | my $low = substr($plainhex, 18, 2).substr($plainhex, 16, 2); | |||||||||||||||||||||||||||
| 193 | 1 | my $high = substr($plainhex, 20, 2); | |||||||||||||||||||||||||||
| 194 | 1 | my $ts = ((hex($high) << 16) + hex($low)) / 8; # XXX magic | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 1 | my $tinfo = sprintf('iid=%s, count=%d, use=%d, ts=%d', $token->{iid}, $count, $use, $ts); | |||||||||||||||||||||||||||
| 197 | 1 | my $tsnow = $token->{lastts} + (time() - $token->{lastuse}); | |||||||||||||||||||||||||||
| 198 | 1 | my $tsage = $tsnow - $ts; | |||||||||||||||||||||||||||
| 199 | 1 | $self->{update_callback}->($public_id, { lastuse => time(), lastts => $ts }); | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 1 | ▶ | if ($count < $token->{count}) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 202 | 0 | $self->{log_callback}->(sprintf('token %s failed: duplicate otp, count (%s)', $public_id, $tinfo)); | |||||||||||||||||||||||||||
| 203 | } elsif ($count == $token->{count}) { | ||||||||||||||||||||||||||||
| 204 | 0 | ▶ | if ($use <= $token->{use}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 205 | 0 | $self->{log_callback}->(sprintf('token %s failed: duplicate otp in same session (%s)', $public_id, $tinfo)); | |||||||||||||||||||||||||||
| 206 | } elsif ($tsage > $self->{max_age}) { | ||||||||||||||||||||||||||||
| 207 | 0 | $self->{log_callback}->(sprintf('token %s failed: expired otp is %d seconds old (%s)', $public_id, $tsage, $tinfo)); | |||||||||||||||||||||||||||
| 208 | } else { | ||||||||||||||||||||||||||||
| 209 | 0 | $self->{log_callback}->(sprintf('token %s ok, same session (%s)', $public_id, $tinfo)); | |||||||||||||||||||||||||||
| 210 | 0 | $self->{update_callback}->($public_id, { count => $count, use => $use }); | |||||||||||||||||||||||||||
| 211 | 0 | return 1; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | } elsif ($count > $token->{count}) { | ||||||||||||||||||||||||||||
| 214 | 1 | $self->{log_callback}->(sprintf('token %s ok (%s)', $public_id, $tinfo)); | |||||||||||||||||||||||||||
| 215 | 1 | $self->{update_callback}->($public_id, { count => $count, use => $use }); | |||||||||||||||||||||||||||
| 216 | 1 | return 1; | |||||||||||||||||||||||||||
| 217 | } else { | ||||||||||||||||||||||||||||
| 218 | 0 | $self->{log_callback}->(sprintf('something bad with token %s (%s)', $public_id, $tinfo)); | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | } else { | ||||||||||||||||||||||||||||
| 221 | 0 | $self->{log_callback}->(sprintf('token %s failed: corrupt otp (crc)', $public_id)); | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | } else { | ||||||||||||||||||||||||||||
| 224 | 0 | $self->{log_callback}->(sprintf('token %s failed: corrupt otp (internal id)', $public_id)); | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | } else { | ||||||||||||||||||||||||||||
| 227 | 0 | $self->{log_callback}->(sprintf('token %s failed: invalid otp', $public_id)); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 0 | return 0; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =head1 CALLBACKS | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =head2 callback | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =over 4 | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =item Receives: $public_id, @callback_args | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =item Returns: \%token_data | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =back | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | Called during validation when information about the token is required. | ||||||||||||||||||||||||||||
| 246 | Receives the public ID of the Yubikey. It's expected that your subroutine | ||||||||||||||||||||||||||||
| 247 | returns a hash reference containing the following keys: | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =over 4 | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =item iid - Internal ID | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =item key - Secret key | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =back | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | Plus the four values stored by the L<update_callback>. | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head2 update_callback | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =over 4 | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =item Receives: $public_id, \%token_data, @callback_args | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | =item Returns: nothing | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =back | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | Called to update the persistent storage of token parameters that enable replay | ||||||||||||||||||||||||||||
| 270 | protection. C<%token_data> will contain one or more of the following keys, | ||||||||||||||||||||||||||||
| 271 | which should be associated with the supplied C<$public_id>: | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | =over 4 | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =item count | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =item use | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =item lastuse | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =item lastts | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | =back | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | These should all be integers. | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =head2 log_callback | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | =over 4 | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | =item Receives: $log_message | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =item Returns: nothing | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =back | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | Called with messages produced during validation. If not supplied to L<new>, | ||||||||||||||||||||||||||||
| 298 | logging will disabled. | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =head1 EXAMPLE | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | Here's a simple program that uses L<DBIx::Class> to store token information. | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | package YKKSM::DB::Token; | ||||||||||||||||||||||||||||
| 305 | use base qw/DBIx::Class/; | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | __PACKAGE__->load_components(qw/PK::Auto Core/); | ||||||||||||||||||||||||||||
| 308 | __PACKAGE__->table('token'); | ||||||||||||||||||||||||||||
| 309 | __PACKAGE__->add_columns(qw/uid pid iid key count use lastuse lastts/); | ||||||||||||||||||||||||||||
| 310 | __PACKAGE__->set_primary_key('uid'); | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | package YKKSM::DB; | ||||||||||||||||||||||||||||
| 313 | use base qw/DBIx::Class::Schema/; | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | __PACKAGE__->load_classes(qw/Token/); | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | package YKTest; | ||||||||||||||||||||||||||||
| 318 | use Validate::Yubikey; | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | my $schema = YKKSM::DB->connect("dbi:SQLite:dbname=yktest.db"); | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | my $yk = Validate::Yubikey->new( | ||||||||||||||||||||||||||||
| 323 | callback => sub { | ||||||||||||||||||||||||||||
| 324 | my $pid = shift; | ||||||||||||||||||||||||||||
| 325 | my $token = $schema->resultset('Token')->find({ pid => $pid }); | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | if ($token) { | ||||||||||||||||||||||||||||
| 328 | return { | ||||||||||||||||||||||||||||
| 329 | iid => $token->iid, | ||||||||||||||||||||||||||||
| 330 | key => $token->key, | ||||||||||||||||||||||||||||
| 331 | count => $token->count, | ||||||||||||||||||||||||||||
| 332 | use => $token->use, | ||||||||||||||||||||||||||||
| 333 | lastuse => $token->lastuse, | ||||||||||||||||||||||||||||
| 334 | lastts => $token->lastts, | ||||||||||||||||||||||||||||
| 335 | }; | ||||||||||||||||||||||||||||
| 336 | } else { | ||||||||||||||||||||||||||||
| 337 | return undef; | ||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | }, | ||||||||||||||||||||||||||||
| 340 | update_callback => sub { | ||||||||||||||||||||||||||||
| 341 | my ($pid, $data) = @_; | ||||||||||||||||||||||||||||
| 342 | my $token = $schema->resultset('Token')->find({ pid => $pid }); | ||||||||||||||||||||||||||||
| 343 | if ($token) { | ||||||||||||||||||||||||||||
| 344 | $token->update($data); | ||||||||||||||||||||||||||||
| 345 | } else { | ||||||||||||||||||||||||||||
| 346 | die "asked to update nonexistent token $pid"; | ||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | }, | ||||||||||||||||||||||||||||
| 349 | log_callback => sub { | ||||||||||||||||||||||||||||
| 350 | print shift, "\n"; | ||||||||||||||||||||||||||||
| 351 | }, | ||||||||||||||||||||||||||||
| 352 | ); | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | if ($yk->validate($ARGV[0])) { | ||||||||||||||||||||||||||||
| 355 | print "success!\n"; | ||||||||||||||||||||||||||||
| 356 | } else { | ||||||||||||||||||||||||||||
| 357 | print "failure 8(\n"; | ||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | Ben Wilber <ben@desync.com> | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | But most of this module was derived from Yubico's PHP stuff. | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | This library is free software and may be distributed under the same terms | ||||||||||||||||||||||||||||
| 369 | as perl itself. | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | =cut | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 1 | 1; | |||||||||||||||||||||||||||