File Coverage

blib/lib/Amon2/Auth/Site/Hatena.pm
Criterion Covered Total %
statement 18 44 40.9
branch 0 16 0.0
condition 0 8 0.0
subroutine 6 10 60.0
pod 0 4 0.0
total 24 82 29.2


line stmt bran cond sub pod time code
1             package Amon2::Auth::Site::Hatena;
2 1     1   1600 use Mouse;
  1         42038  
  1         38  
3              
4 1     1   8891 use JSON;
  1         2  
  1         10  
5 1     1   1136 use OAuth::Lite::Token;
  1         33597  
  1         118  
6 1     1   3763 use OAuth::Lite::Consumer;
  1         129471  
  1         12  
7 1     1   1189 use Woothee;
  1         23095  
  1         17  
8              
9             our $VERSION = '0.04';
10              
11             has consumer_key => (
12             is => 'ro',
13             isa => 'Str',
14             required => 1,
15             );
16              
17             has consumer_secret => (
18             is => 'ro',
19             isa => 'Str',
20             required => 1,
21             );
22              
23             has scope => (
24             is => 'ro',
25             isa => 'ArrayRef',
26             default => sub { +[qw(read_public)] },
27             );
28              
29             has user_info => (
30             is => 'rw',
31             isa => 'Bool',
32             default => 1,
33             );
34              
35             has ua => (
36             is => 'ro',
37             isa => 'OAuth::Lite::Consumer',
38             lazy => 1,
39             default => sub {
40             OAuth::Lite::Consumer->new(
41             consumer_key => $_[0]->consumer_key,
42             consumer_secret => $_[0]->consumer_secret,
43             site => $_[0]->site,
44             request_token_path => $_[0]->request_token_path,
45             access_token_path => $_[0]->access_token_path,
46             );
47             },
48             );
49              
50             has site => (
51             is => 'ro',
52             isa => 'Str',
53             default => 'https://www.hatena.com',
54             );
55              
56             has request_token_path => (
57             is => 'ro',
58             isa => 'Str',
59             default => '/oauth/initiate',
60             );
61              
62             has access_token_path => (
63             is => 'ro',
64             isa => 'Str',
65             default => '/oauth/token',
66             );
67              
68             has authorize_url => (
69             is => 'ro',
70             isa => 'Str',
71             default => 'https://www.hatena.ne.jp/oauth/authorize',
72             );
73              
74             has authorize_url_touch => (
75             is => 'ro',
76             isa => 'Str',
77             default => 'https://www.hatena.ne.jp/touch/oauth/authorize',
78             );
79              
80             has authorize_url_mobile => (
81             is => 'ro',
82             isa => 'Str',
83             default => 'http://www.hatena.ne.jp/mobile/oauth/authorize',
84             );
85              
86             has user_info_url => (
87             is => 'ro',
88             isa => 'Str',
89             default => 'http://n.hatena.com/applications/my.json',
90             );
91              
92             has redirect_url => (
93             is => 'ro',
94             isa => 'Str',
95             );
96              
97 1     1   392 no Mouse;
  1         3  
  1         11  
98             __PACKAGE__->meta->make_immutable;
99              
100 0     0 0   sub moniker { 'hatena' }
101              
102             sub detect_authorize_url_from {
103 0     0 0   my ($self, $c) = @_;
104              
105 0           my $category = Woothee->parse($c->req->env->{'HTTP_USER_AGENT'})->{category};
106 0 0         $category eq 'smartphone' ? $self->authorize_url_touch :
    0          
107             $category eq 'mobilephone' ? $self->authorize_url_mobile :
108             $self->authorize_url ;
109             }
110              
111             sub auth_uri {
112 0     0 0   my ($self, $c, $callback_uri) = @_;
113              
114 0           my $request_token = $self->ua->get_request_token(
115             callback_url => $callback_uri || $self->redirect_url,
116 0 0 0       scope => join(',', @{$self->scope}),
117             ) or die $self->ua->errstr;
118              
119 0           $c->session->set(auth_hatena => {
120             request_token => $request_token->token,
121             request_token_secret => $request_token->secret,
122             });
123              
124 0           $self->ua->{authorize_path} = $self->detect_authorize_url_from($c);
125 0           $self->ua->url_to_authorize(token => $request_token);
126             }
127              
128             sub callback {
129 0     0 0   my ($self, $c, $callback) = @_;
130 0           my $error = $callback->{on_error};
131              
132 0 0         my $verifier = $c->req->param('oauth_verifier')
133             or return $error->("Cannot get a `oauth_verifier' parameter");
134              
135 0   0       my $session = $c->session->get('auth_hatena') || {};
136 0           my $token = $session->{request_token};
137 0           my $token_secret = $session->{request_token_secret};
138              
139 0 0 0       return $error->('request_token, request_token_secret are both required')
140             if (!$token || !$token_secret);
141              
142 0           my $request_token = OAuth::Lite::Token->new(
143             token => $token,
144             secret => $token_secret,
145             );
146 0 0         my $access_token = $self->ua->get_access_token(
147             token => $request_token,
148             verifier => $verifier,
149             ) or return $error->($self->ua->errstr);
150              
151 0           my @args = ($access_token->token, $access_token->secret);
152              
153 0 0         if ($self->user_info) {
154 0           my $res = $self->ua->get($self->user_info_url);
155 0 0         return $error->($self->ua->errstr) if $res->is_error;
156              
157 0           my $data = decode_json($res->decoded_content);
158 0           push @args, $data;
159             }
160              
161 0           $callback->{on_finished}->(@args);
162             }
163              
164             1;
165              
166             __END__