Catalyst-Authentication-Credential-Flickr

 view release on metacpan or  search on metacpan

lib/Catalyst/Authentication/Credential/Flickr.pm  view on Meta::CPAN


    # Check for required params (yes, nasty)
    for my $param (qw/key secret perms/) {
        $self->$param($params->{$param}) or
            Catalyst::Exception->throw("$param not defined") 
    }

    # Create a Flickr::API instance
    $self->_flickr(Flickr::API->new({ key => $self->key, 
                                      secret => $self->secret }));

    return $self;
}

sub authenticate {
    my ( $self, $c, $realm, $authinfo ) = @_;

    my $frob = $c->req->params->{frob} or return;
    
    my $api_response = $self->_flickr->execute_method( 'flickr.auth.getToken',
        { frob => $frob, } );

    # Mapping the XML (why, oh why use XML::Parser::Lite::Tree ?)
    # Let's pray really hard that Flickr will never ever change the layout
    # of their XML. This _will_ break in that case.

    my $user = {};
    foreach my $node (@{$api_response->{tree}{children}[1]{children}}) {
        if(defined $node->{children} && $node->{children}[0]{content}) {
            $user->{$node->{name}} = $node->{children}[0]{content}
        }
        if(defined $node->{attributes}) {
            $user->{$_} = $node->{attributes}{$_} 
                for(keys %{$node->{attributes}});
        }
    }
    
    my $user_obj = $realm->find_user( $user, $c );
    return ref $user_obj ? $user_obj : undef;
}
    
sub authenticate_flickr_url {
    my ($self, $c) = @_;

    my $uri = $self->_flickr->request_auth_url($self->perms);
    return $uri;
}

=head1 NAME

Catalyst::Authentication::Credential::Flickr - Flickr authentication for Catalyst

=head1 SYNOPSIS

In MyApp.pm

 use Catalyst qw/
    Authentication
    Session
    Session::Store::FastMmap
    Session::State::Cookie
 /;
 
 MyApp->config(
     "Plugin::Authentication" => {
         default_realm => "flickr",
         realms => {
             flickr => {
                 credential => {
                     class => "Flickr",
                 },

                 key    => 'flickr-key-here',
                 secret => 'flickr-secret-here',
                 perms  => 'read',
             },
         },
     },
 );

And then in your Controller:

 sub login : Local {
    my ($self, $c) = @_;
    
    my $realm = $c->get_auth_realm('flickr');
    $c->res->redirect( $realm->credential->authenticate_flickr_url($c) );
 }

And finally the callback you specified in your API key request (e.g.
example.com/flickr/ ):

 sub flickr : Local {
    my ($self, $c) = @_;
    
    $c->authenticate();
    $c->res->redirect("/super/secret/member/area");
 }

=head1 DESCRIPTION

This module handles Flickr API authentication in a Catalyst application.

When L<Catalyst::Plugin::Authentication> 0.10 was released, the API had 
changed, resulting in broken code when using 
L<Catalyst::Plugin::Authentication::Credential::Flickr>.

This module tries to follow the guidelines of the new API and deprecate
L<Catalyst::Plugin::Authentication::Credential::Flickr>.

Code changes are needed, but are fairly minimal.

=head1 METHODS

As per guidelines of L<Catalyst::Plugin::Authentication>, there are two
mandatory methods, C<new> and C<authenticate>. Since this is not really
enough for the Flickr API, I've added one more (and an alias).

=head2 new()

Will not be called by you directly, but will use the configuration you



( run in 0.829 second using v1.01-cache-2.11-cpan-2398b32b56e )