File Coverage

blib/lib/Authen/OATH/KeyURI.pm
Criterion Covered Total %
statement 45 46 97.8
branch 15 16 93.7
condition 6 6 100.0
subroutine 11 11 100.0
pod 1 3 33.3
total 78 82 95.1


line stmt bran cond sub pod time code
1             package Authen::OATH::KeyURI;
2 2     2   1025 use 5.008001;
  2         6  
  2         79  
3 2     2   10 use strict;
  2         2  
  2         64  
4 2     2   16 use warnings;
  2         2  
  2         84  
5              
6             our $VERSION = "0.02";
7              
8 2     2   8 use base 'Class::Accessor::Fast';
  2         2  
  2         989  
9 2     2   5684 use Params::Validate qw(SCALAR);
  2         14522  
  2         132  
10 2     2   3451 use URI;
  2         9943  
  2         63  
11 2     2   928 use Convert::Base32 qw(encode_base32);
  2         2581  
  2         811  
12              
13             __PACKAGE__->mk_accessors(qw(
14             scheme
15             type
16             accountname
17             secret
18             issuer
19             algorithm
20             digits
21             counter
22             period
23             uri
24             is_encoded
25             ));
26              
27             sub new {
28 8     8 1 10357 my $class = shift;
29 8 50       38 my @args = @_ == 1 ? %{$_[0]} : @_;
  0         0  
30 8         444 my %params = Params::Validate::validate_with(
31             params => \@args,
32             spec => {
33             scheme => {
34             type => SCALAR,
35             default => q{otpauth},
36             optional => 1,
37             },
38             type => {
39             type => SCALAR,
40             default => q{totp},
41             optional => 1,
42             },
43             accountname => {
44             type => SCALAR,
45             },
46             secret => {
47             type => SCALAR,
48             },
49             issuer => {
50             type => SCALAR,
51             optional => 1,
52             },
53             algorithm => {
54             type => SCALAR,
55             optional => 1,
56             },
57             digits => {
58             type => SCALAR,
59             optional => 1,
60             },
61             counter => {
62             type => SCALAR,
63             optional => 1,
64             },
65             period => {
66             type => SCALAR,
67             optional => 1,
68             },
69             is_encoded => {
70             type => SCALAR,
71             default => 0,
72             optional => 1,
73             },
74             },
75             allow_extra => 0,
76             );
77              
78 8         89 my $self = bless \%params, $class;
79              
80             # TODO: more varidation
81            
82 8         25 return $self;
83             }
84              
85             sub as_string {
86 22     22 0 7494 my $self = shift;
87 22         40 $self->_generate_uri();
88 22         167 return $self->uri->as_string;
89             }
90              
91             sub as_uri {
92 8     8 0 2805 my $self = shift;
93 8         18 $self->_generate_uri();
94 8         65 return $self->uri;
95             }
96              
97             sub _generate_uri {
98 30     30   30 my $self = shift;
99              
100             # 1. Scheme
101 30         86 my $uri = URI->new;
102 30         4897 $uri->scheme($self->scheme);
103              
104             # 2. Type and Label
105 30 100       10668 my $label =
106             ($self->issuer) ?
107             $self->issuer . q{:} . $self->accountname :
108             $self->accountname;
109 30         296 $uri->path(q{//} . $self->type . q{/} . $label);
110              
111             # 3. Parameters
112 30 100       954 my $params = {
113             secret => ($self->is_encoded) ? $self->secret : encode_base32($self->secret),
114             };
115 30 100       1724 $params->{issuer} = $self->issuer if $self->issuer;
116 30 100       186 $params->{algorithm } = $self->algorithm if $self->algorithm ;
117 30 100       187 $params->{digits} = $self->digits if $self->digits;
118              
119             # hotp only
120 30 100 100     183 $params->{counter} = $self->counter
121             if ($self->counter && $self->type eq q{hotp});
122             # totp only
123 30 100 100     238 $params->{period} = $self->period
124             if ($self->period && $self->type eq q{totp});
125              
126 30         249 $uri->query_form($params);
127              
128 30         2102 $self->uri($uri);
129             }
130              
131             1;
132             __END__