File Coverage

lib/Auth/YubiKey/Client/Web.pm
Criterion Covered Total %
statement 21 34 61.7
branch n/a
condition n/a
subroutine 7 9 77.7
pod 2 2 100.0
total 30 45 66.6


line stmt bran cond sub pod time code
1             package Auth::YubiKey::Client::Web;
2             {
3             $Auth::YubiKey::Client::Web::VERSION = '0.0.1';
4             }
5             {
6             $Auth::YubiKey::Client::Web::DIST = 'Auth-YubiKey-Client-Web';
7             }
8 1     1   75783 use Moo;
  1         33324  
  1         6  
9              
10 1     1   2060 use Carp;
  1         2  
  1         88  
11 1     1   947 use Digest::HMAC_SHA1 qw(hmac_sha1_hex hmac_sha1);
  1         7232  
  1         63  
12 1     1   22225 use HTTP::Tiny;
  1         89248  
  1         47  
13 1     1   1193 use MIME::Base64;
  1         927  
  1         80  
14 1     1   923 use URI::Escape;
  1         1831  
  1         74  
15              
16 1     1   664 use Auth::YubiKey::Client::Web::Response;
  1         3  
  1         451  
17              
18             has id => (
19             is => 'ro',
20             isa => sub { Carp::confess( 'id must be defined' ) unless defined $_[0] },
21             required => 1,
22             );
23              
24             has api_key => (
25             is => 'ro',
26             isa => sub { Carp::confess( 'api_key must be defined' ) unless defined $_[0] },
27             required => 1,
28             );
29              
30             # https://code.google.com/p/yubikey-val-server-php/wiki/GettingStartedWritingClients
31             has verify_url => (
32             is => 'ro',
33             default => 'http://api2.yubico.com/wsapi/2.0/verify?',
34             );
35              
36             has ua => (
37             is => 'ro',
38             default => sub {
39             HTTP::Tiny->new(
40             agent => __PACKAGE__,
41             );
42             }
43             );
44              
45              
46             sub nonce {
47 0     0 1   my $data = rand() . $$ . {} . time;
48 0           my $key = "@INC";
49 0           my $digest = hmac_sha1_hex($data, $key);
50             };
51              
52             sub verify_otp {
53 0     0 1   my $self = shift;
54 0           my $otp = shift;
55            
56 0           my $nonce = nonce();
57 0           chomp($otp);
58              
59             # Start generating the parameters
60 0           my $params;
61 0           $params = sprintf(
62             'id=%d&nonce=%s&otp=%s×tamp=1',
63             $self->id,
64             $nonce,
65             uri_escape($otp)
66             );
67 0           $params .= sprintf (
68             '&h=%s',
69             uri_escape(
70             encode_base64(hmac_sha1($params,
71             decode_base64($self->api_key)), ''))
72             );
73            
74 0           my $url = $self->verify_url . $params; #join('&', @param_blobs);
75              
76 0           my $response = $self->ua->get( $url );
77              
78 0           my $yubi_response = Auth::YubiKey::Client::Web::Response->new(
79             request_apikey => $self->api_key,
80             request_otp => $otp,
81             request_nonce => $nonce,
82             request_response => $response->{content},
83             );
84             }
85              
86              
87             1;
88             # ABSTRACT: Authenticate using the Yubico Web API
89              
90             =pod
91              
92             =head1 NAME
93              
94             Auth::YubiKey::Client::Web - Authenticate using the Yubico Web API
95              
96             =head1 VERSION
97              
98             version 0.0.1
99              
100             =head1 SYNOPSIS
101              
102             use Auth::YubiKey::Client::Web;
103              
104             my $yubiauth = Auth::YubiKey::Client::Web->new(
105             id => $id,
106             api_key => $apikey,
107             );
108              
109             my $result = $yubiauth->verify_otp($input);
110              
111             if ($result->is_success) {
112             say 'Good to go';
113             say 'user-id: ' . $result->public_id;
114             }
115             else {
116             say 'Oh dear: ' . $result->status;
117             }
118              
119             =head2 METHODS
120              
121             =head3 nonce()
122              
123             This function returns a
124             L for use in the
125             validation step,
126              
127             my $nonce = nonce();
128              
129             =head3 verify_otp($self, $otp)
130              
131             Given an OTP make a call to the remote service and validate the value
132             provided.
133              
134             This method returns an L object which
135             can be queried for the validity of the request.
136              
137             my $response = $self->verify_otp( $otp );
138             if ($response->is_success) {
139             # yay!
140             }
141             else {
142             # boo!
143             }
144              
145             =head1 API KEY
146              
147             To use this module you will require an API key from Yubico. You can
148             get a key by visiting the following page and entering the required
149             information:
150              
151             =over 4
152              
153             =item L
154              
155             =back
156              
157             =head1 FURTHER READING
158              
159             Here are some related, useful or interesting links:
160              
161             =over 4
162              
163             =item L
164              
165             =item L
166              
167             =back
168              
169             =head1 AUTHOR
170              
171             Chisel
172              
173             =head1 COPYRIGHT AND LICENSE
174              
175             This software is copyright (c) 2013 by Chisel Wright.
176              
177             This is free software; you can redistribute it and/or modify it under
178             the same terms as the Perl 5 programming language system itself.
179              
180             =cut
181              
182             __END__