File Coverage

blib/lib/Catalyst/Authentication/Credential/Flickr.pm
Criterion Covered Total %
statement 14 16 87.5
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 20 22 90.9


line stmt bran cond sub pod time code
1             package Catalyst::Authentication::Credential::Flickr;
2              
3 1     1   12941 use strict;
  1         1  
  1         23  
4 1     1   3 use warnings;
  1         1  
  1         24  
5 1     1   3 use base qw( Class::Accessor::Fast );
  1         4  
  1         447  
6              
7             BEGIN {
8 1     1   2656 __PACKAGE__->mk_accessors(qw/_xml _flickr key secret perms/);
9             }
10              
11             our $VERSION = "0.01_01";
12              
13 1     1   542 use Catalyst::Exception ();
  1         438261  
  1         29  
14 1     1   599 use Flickr::API;
  0            
  0            
15             use XML::Simple;
16              
17             sub new {
18             my ($class, $config, $c, $realm) = @_;
19             my $self = {};
20             bless $self, $class;
21              
22             # Hack to make lookup of the configuration parameters less painful
23             my $params = { %{ $config }, %{ $realm->{config} } };
24              
25             # Check for required params (yes, nasty)
26             for my $param (qw/key secret perms/) {
27             $self->$param($params->{$param}) or
28             Catalyst::Exception->throw("$param not defined")
29             }
30              
31             # Create a Flickr::API instance
32             $self->_flickr(Flickr::API->new({ key => $self->key,
33             secret => $self->secret }));
34              
35             # Create a XML::Simple instance
36             $self->_xml(XML::Simple->new());
37              
38             return $self;
39             }
40              
41             sub authenticate {
42             my ( $self, $c, $realm, $authinfo ) = @_;
43              
44             my $frob = $c->req->params->{frob} or return;
45            
46             my $api_response = $self->_flickr->execute_method( 'flickr.auth.getToken',
47             { frob => $frob, } );
48              
49             my $xml = $self->_xml->XMLin($api_response->decoded_content);
50              
51             # "Flatten" returned XML a little
52             foreach(keys %{$xml->{auth}{user}}) {
53             $xml->{auth}{$_} = delete $xml->{auth}{user}{$_};
54             }
55             delete $xml->{auth}{user};
56            
57             my $user_obj = $realm->find_user( $xml->{auth}, $c );
58             return ref $user_obj ? $user_obj : undef;
59             }
60            
61             sub authenticate_flickr_url {
62             my ($self, $c) = @_;
63              
64             my $uri = $self->_flickr->request_auth_url($self->perms);
65             return $uri;
66             }
67              
68             =head1 NAME
69              
70             Catalyst::Authentication::Credential::Flickr - Flickr authentication for Catalyst
71              
72             =head1 SYNOPSIS
73              
74             In MyApp.pm
75              
76             use Catalyst qw/
77             Authentication
78             Session
79             Session::Store::FastMmap
80             Session::State::Cookie
81             /;
82            
83             MyApp->config(
84             "Plugin::Authentication" => {
85             default_realm => "flickr",
86             realms => {
87             flickr => {
88             credential => {
89             class => "Flickr",
90             },
91              
92             key => 'flickr-key-here',
93             secret => 'flickr-secret-here',
94             perms => 'read',
95             },
96             },
97             },
98             );
99              
100             And then in your Controller:
101              
102             sub login : Local {
103             my ($self, $c) = @_;
104            
105             my $realm = $c->get_auth_realm('flickr');
106             $c->res->redirect( $realm->credential->authenticate_flickr_url($c) );
107             }
108              
109             And finally the callback you specified in your API key request (e.g.
110             example.com/flickr/ ):
111              
112             sub flickr : Local {
113             my ($self, $c) = @_;
114            
115             $c->authenticate();
116             $c->res->redirect("/super/secret/member/area");
117             }
118              
119             =head1 DESCRIPTION
120              
121             When L<Catalyst::Plugin::Authentication> 0.10 was released, the API had
122             changed, resulting in broken code for some of my projects. They claim to
123             have some backward compatibility however, so it was most likely just my
124             own bad coding that broke stuff.
125              
126             Anyways, L<Catalyst::Plugin::Authentication::Credential::Flickr> didn't
127             do what I wanted it to do and so I decided to rewrite the code, based on
128             the new API.
129              
130             This version tries to follow the guidelines of the new API and should almost
131             be a drop in replacement for C::P::Authentication::Credential::Flickr. Well,
132             at least code changes are kept to a minimal.
133              
134             =head1 METHODS
135              
136             As per guidelines of L<Catalyst::Plugin::Authentication>, there are two
137             mandatory methods, C<new> and C<authenticate>. Since this is not really
138             enough for the Flickr API, I've added one more (and an alias).
139              
140             =head2 new()
141              
142             Will not be called by you directly, but will use the configuration you
143             provide (see above). Mandatory parameters are C<key>, C<secret> and
144             C<perms>. Please see L<Flickr::API> for more details on them.
145              
146             =head2 request_auth_url( $c )
147              
148             This method will return the authentication URL. Bounce your users there
149             before calling the C<authentication> method.
150              
151             =head2 authenticate_flickr_url( $c )
152              
153             Alias for C<request_auth_url> ('cause C::P::Authentication::Credential::Flickr
154             used to call the method like this).
155              
156             =head2 authenticate()
157              
158             Handles the authentication. Nothing more, nothing less.
159              
160             =head1 AUTHOR
161              
162             M. Blom
163             E<lt>blom@cpan.orgE<gt>
164             L<http://menno.b10m.net/perl/>
165              
166             =head1 COPYRIGHT
167              
168             This program is free software; you can redistribute
169             it and/or modify it under the same terms as Perl itself.
170              
171             The full text of the license can be found in the
172             LICENSE file included with this module.
173              
174             =head1 SEE ALSO
175              
176             L<Catalyst::Plugin::Authentication>, L<Flickr::API>
177              
178             =head1 BUGS
179              
180             C<Bugs? Impossible!>. Please report bugs to L<http://rt.cpan.org/Ticket/Create.html?Queue=Catalyst-Authentication-Credentials-Flickr>
181              
182             =head1 THANKS
183              
184             Thanks go out Daisuke Murase for writing C::P::A::Credential::Flickr,
185             Ashley Pond for inspiration and help in C::A::Credential::OpenID,
186             Cal Henderson for Flickr::API.
187              
188             =cut
189              
190             1;