File Coverage

blib/lib/Amon2/Auth/Site/Twitter.pm
Criterion Covered Total %
statement 15 37 40.5
branch 0 6 0.0
condition n/a
subroutine 5 9 55.5
pod 0 3 0.0
total 20 55 36.3


line stmt bran cond sub pod time code
1 1     1   866 use strict;
  1         2  
  1         23  
2 1     1   3 use warnings;
  1         1  
  1         23  
3 1     1   3 use utf8;
  1         1  
  1         5  
4              
5             package Amon2::Auth::Site::Twitter;
6 1     1   22 use Mouse;
  1         1  
  1         5  
7 1     1   644 use Net::Twitter::Lite::WithAPIv1_1;
  1         15080  
  1         269  
8              
9 0     0 0   sub moniker { 'twitter' }
10              
11             has consumer_key => (
12             is => 'ro',
13             isa => 'Str',
14             required => 1,
15             );
16             has consumer_secret => (
17             is => 'ro',
18             isa => 'Str',
19             required => 1,
20             );
21              
22             sub _nt {
23 0     0     my ($self) = @_;
24 0           my $nt = Net::Twitter::Lite::WithAPIv1_1->new(
25             consumer_key => $self->consumer_key,
26             consumer_secret => $self->consumer_secret,
27             ssl => 1,
28             );
29 0           return $nt;
30             }
31              
32             sub auth_uri {
33 0     0 0   my ($self, $c, $callback_uri) = @_;
34              
35 0           my $nt = $self->_nt();
36 0           my $redirect_uri = $nt->get_authorization_url(callback => $callback_uri);
37 0           $c->session->set( auth_twitter => [ $nt->request_token, $nt->request_token_secret, ] );
38 0           return $redirect_uri;
39             }
40              
41             sub callback {
42 0     0 0   my ($self, $c, $callback) = @_;
43              
44             my $cookie = $c->session->get('auth_twitter')
45 0 0         or return $callback->{on_error}->("Session error");
46              
47 0           my $nt = $self->_nt();
48 0           $nt->request_token($cookie->[0]);
49 0           $nt->request_token_secret($cookie->[1]);
50 0 0         if (my $denied = $c->req->param('denied')) {
51 0           return $callback->{on_error}->("Access denied");
52             }
53 0           my $verifier = $c->req->param('oauth_verifier');
54 0           my ($access_token, $access_token_secret, $user_id, $screen_name) = eval {
55 0           $nt->request_access_token(verifier => $verifier);
56             };
57 0 0         if ($@) {
58             # Net::Twitter::Lite throws exception like following
59             # GET https://twitter.com/oauth/access_token failed: 401 Unauthorized at /Users/tokuhirom/perl5/perlbrew/perls/perl-5.15.2/lib/site_perl/5.15.2/Net/Twitter/Lite.pm line 237.
60 0           return $callback->{on_error}->($@);
61             } else {
62 0           return $callback->{on_finished}->($access_token, $access_token_secret, $user_id, $screen_name);
63             }
64             }
65              
66             1;
67             __END__
68              
69             =head1 NAME
70              
71             Amon2::Auth::Site::Twitter - Twitter integration for Amon2
72              
73             =head1 SYNOPSIS
74              
75              
76             __PACKAGE__->load_plugin('Web::Auth', {
77             module => 'Twitter',
78             on_finished => sub {
79             my ($c, $access_token, $access_token_secret, $user_id, $screen_name)
80             = @_;
81             $c->session->set('name' => $screen_name);
82             $c->session->set('site' => 'twitter');
83             return $c->redirect('/');
84             }
85             });
86              
87             =head1 DESCRIPTION
88              
89             This is a twitter authentication module for Amon2. You can call a twitter APIs with this module.
90              
91             =head1 ATTRIBUTES
92              
93             =over 4
94              
95             =item consumer_key
96              
97             =item consumer_secret
98              
99             =back
100              
101             =head1 METHODS
102              
103             =over 4
104              
105             =item C<< $auth->auth_uri($c:Amon2::Web, $callback_uri : Str) :Str >>
106              
107             Get a authenticate URI.
108              
109             =item C<< $auth->callback($c:Amon2::Web, $callback:HashRef) : Plack::Response >>
110              
111             Process the authentication callback dispatching.
112              
113             C<< $callback >> MUST have two keys.
114              
115             =over 4
116              
117             =item on_error
118              
119             on_error callback function is called if an error was occurred.
120              
121             The arguments are following:
122              
123             sub {
124             my ($c, $error_message) = @_;
125             ...
126             }
127              
128             =item on_finished
129              
130             on_finished callback function is called if an authentication was finished.
131              
132             The arguments are following:
133              
134             sub {
135             my ($c, $access_token, $access_token_secret, $user_id, $screen_name)
136             = @_;
137             ...
138             }
139              
140             =back
141              
142             =back
143              
144