File Coverage

lib/Net/Google/Authenticate.pm
Criterion Covered Total %
statement 21 56 37.5
branch 0 18 0.0
condition n/a
subroutine 7 16 43.7
pod 3 3 100.0
total 31 93 33.3


line stmt bran cond sub pod time code
1             package Net::Google::Authenticate;
2              
3             # ABSTRACT: would go here
4              
5 3     3   55900 use warnings;
  3         7  
  3         114  
6 3     3   17 use strict;
  3         6  
  3         137  
7              
8             our $VERSION = '0.03'; # VERSION
9              
10              
11 3     3   17 use Carp;
  3         7  
  3         283  
12              
13             ## no critic( Tics::ProhibitUseBase )
14 3     3   17 use base qw( Class::Accessor Class::ErrorHandler );
  3         6  
  3         2032  
15              
16             ## no critic( Modules::RequireExplicitInclusion )
17             __PACKAGE__->mk_accessors( qw(
18              
19             Email Passwd source _auth
20              
21             ) );
22             ## use critic
23              
24              
25 0     0     sub _valid_accountType { return qw( HOSTED GOOGLE HOSTED_OR_GOOGLE ) }
26              
27 0     0     sub _default_accountType { return 'HOSTED_OR_GOOGLE' } ## no critic( Subroutines::ProhibitUnusedPrivateSubroutines )
28              
29             sub accountType { ## no critic( Subroutines::RequireArgUnpacking )
30              
31 0     0 1   my $self = shift;
32              
33 0 0         return $self->SUPER::get( 'accountType' )
34             unless @_;
35              
36 0           my $type = uc shift;
37              
38 0           my @valid = _valid_accountType;
39              
40 0           return $self->error( "Invalid accountType: $type" )
41 0 0         unless grep { $type eq $_ } @valid;
42              
43 0           return $self->SUPER::set( 'accountType', $type );
44              
45             }
46              
47              
48 0     0     sub _valid_service { return qw( cl blogger gbase wise apps lh2 xapi ) }
49              
50 0     0     sub _default_service { return 'xapi' } ## no critic( Subroutines::ProhibitUnusedPrivateSubroutines )
51              
52             sub service { ## no strict( Subroutines::RequireArgUnpacking )
53              
54 0     0 1   my $self = shift;
55              
56 0 0         return $self->SUPER::get( 'service' )
57             unless @_;
58              
59 0           my $code = lc shift;
60              
61 0           my @known = _valid_service;
62              
63 0           $self->error( "Unknown service code: $code" )
64 0 0         unless grep { $code eq $_ } @known;
65              
66 0           return $self->SUPER::set( 'service', $code );
67              
68             }
69              
70             #=head2 logintoken (optional)
71             #
72             #Not implemented at this time.
73             #
74             #=head2 logincaptcha (optional)
75             #
76             #Not implemented at this time.
77              
78              
79             sub login { ## no critic( Subroutines::RequireFinalReturn )
80              
81 0     0 1   my $self = shift;
82              
83 0           my @required = qw( accountType Email Passwd service source );
84              
85             ## no critic( TestingAndDebugging::ProhibitNoWarnings )
86 3     3   4037 no warnings 'uninitialized';
  3         6  
  3         278  
87              
88 0           my $missing = join ', ', grep { $self->$_ eq '' } @required;
  0            
89              
90 0 0         return $self->error( "Missing required fields: $missing" )
91             if $missing;
92              
93 3     3   16 use warnings 'uninitialized';
  3         6  
  3         184  
94              
95 0           my %params = map { ( $_, $self->$_ ) } @required;
  0            
96              
97 3     3   12 no warnings 'uninitialized';
  3         6  
  3         1215  
98              
99 0           my $r = $self->_ua->post( 'https://www.google.com/accounts/ClientLogin', \%params );
100              
101 0 0         if ( $r->code == 403 ) { ## no critic( ValuesAndExpressions::ProhibitMagicNumbers )
    0          
102              
103 0           my ( $error ) = $r->content =~ m!Error=(.+)(\s+|$)!i;
104              
105 0           return $self->error( "Invalid login: $error (" . _error_code( $error ) . ')' );
106              
107             } elsif ( $r->code == 200 ) { ## no critic( ValuesAndExpressions::ProhibitMagicNumbers )
108              
109 0           my ( $auth ) = $r->content =~ m!Auth=(.+)(\s+|$)!i;
110              
111 0 0         croak 'PANIC: Got a valid response from Google, but can\'t find Auth string'
112             if $auth eq '';
113              
114 0           $self->_auth( $auth );
115              
116             } else {
117              
118             # If we get here then something's up with Google's website
119             # http://code.google.com/apis/accounts/AuthForInstalledApps.html#Response
120             # or else with our connection.
121              
122 0           croak 'PANIC: Got unexpected response (' . $r->code . ')';
123              
124             }
125             } ## end sub login
126              
127              
128             {
129             my %codes = (
130              
131             'BadAuthentication' => 'The login request used a username or password that is not recognized.',
132              
133             'NotVerified' => 'The account email address has not been verified. The user will need to '
134             . 'access their Google account directly to resolve the issue before logging '
135             . 'in using a non-Google application.',
136              
137             'TermsNotAgreed' => 'The user has not agreed to terms. The user will need to access their '
138             . 'Google account directly to resolve the issue before logging in using a '
139             . 'non-Google application.',
140              
141             'CaptchaRequired' => 'A CAPTCHA is required. (A response with this error code will also contain '
142             . 'an image URL and a CAPTCHA token.)',
143              
144             'Unknown' => 'The error is unknown or unspecified; the request contained invalid input ' . 'or was malformed.',
145              
146             'AccountDeleted' => 'The user account has been deleted.',
147              
148             'AccountDisabled' => 'The user account has been disabled.',
149              
150             'ServiceDisabled' => 'The user\'s access to the specified service has been disabled. (The user '
151             . 'account may still be valid.)',
152              
153             'ServiceUnavailable' => 'The service is not available; try again later.',
154              
155             );
156              
157             sub _error_code {
158 0 0   0     return exists $codes{ $_[1] } ? $codes{ $_[1] } : $codes{ 'Unknown' };
159             } ## no critic( Subroutines::RequireArgUnpacking )
160              
161 0     0     sub _codes { return %codes } ## no critic( Subroutines::ProhibitUnusedPrivateSubroutines )
162              
163             }
164              
165             1; # End of Net::Google::GData
166              
167             __END__