X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Authentication-Credential-FBConnect.git;a=blobdiff_plain;f=lib%2FCatalyst%2FAuthentication%2FCredential%2FFBConnect.pm;fp=lib%2FCatalyst%2FAuthentication%2FCredential%2FFBConnect.pm;h=779c486f4a4882e9891ff52c1252fc90a0181776;hp=0000000000000000000000000000000000000000;hb=6d16c3eddeb8a46a5ccd24fe8693a47c28c2acd3;hpb=6e7b9fb03d221c85208ca97b9a01d37cbd53061f diff --git a/lib/Catalyst/Authentication/Credential/FBConnect.pm b/lib/Catalyst/Authentication/Credential/FBConnect.pm new file mode 100644 index 0000000..779c486 --- /dev/null +++ b/lib/Catalyst/Authentication/Credential/FBConnect.pm @@ -0,0 +1,152 @@ +package Catalyst::Authentication::Credential::FBConnect; +use strict; +use warnings; + +use Moose; + +has _config => ( is => 'rw' ); +has debug => ( is => 'rw' ); +has key => ( is => 'rw' ); +has secret => ( is => 'rw' ); +has app_name => ( is => 'rw' ); +has fbconnect => ( is => 'rw' ); + +use WWW::Facebook::API; +use Catalyst::Exception (); + +sub new { + my ($class, $config, $c, $realm) = @_; + + my $self = { _config => { + %{ $config }, + %{ $realm->{config} } + } }; + + bless $self, $class; + + $self->debug( $self->_config->{debug} ); + + $self->key( $self->_config->{key} ); + $self->secret( $self->_config->{secret} ); + $self->app_name( $self->_config->{app_name} ); + + $self->fbconnect( WWW::Facebook::API->new( + desktop => 0, + app_name => $self->app_name, + api_key => $self->key, + secret => $self->secret + ) ); + + return $self; +} + +sub authenticate { + my ($self, $c, $realm, $auth_info) = @_; + + my $token = $c->req->method eq 'GET' + ? $c->req->query_params->{'auth_token'} + : $c->req->body_params->{'auth_token'}; + + if( defined $token ) { + + $self->fbconnect->auth->get_session( $token ); + + my $user = +{ + session_uid => $self->fbconnect->session_uid, + session_key => $self->fbconnect->session_key, + session_expires => $self->fbconnect->session_expires + }; + + my $user_obj = $realm->find_user( $user, $c ); + + return $user_obj if ref $user_obj; + + $c->log->debug( 'Verified FBConnect itentity failed' ) if $self->debug; + + return; + } + else { + + $c->res->redirect( $self->fbconnect->get_login_url( next => $c->uri_for( $c->action ) ) ); + } + +} + +1; + +__END__ + +=head1 NAME + +Catalyst::Authentication::Credential::FBConnect - Facebook credential for Catalyst::Plugin::Authentication framework. + +=head1 VERSION + +0.01 + +=head1 SYNOPSIS + +In MyApp.pm + + use Catalyst qw/ + Authentication + Session + Session::Store::FastMmap + Session::State::Cookie + /; + + +In myapp.conf + + + default_realm facebook + + + + class FBConnect + + key my_app_key + secret my_app_secret + app_name my_app_name + + + + + +In controller code, + + sub facebook : Local { + my ($self, $c) = @_; + + if( $c->authenticate() ) { + #do something with $c->user + } + } + + + +=head1 USER METHODS + +=over 4 + +=item $c->user->session_uid + +=item $c->user->session_key + +=item $c->user->session_expires + +=back + +=head1 AUTHOR + +Cosmin Budrica Ecosmin@sinapticode.comE + +Bogdan Lucaciu Ebogdan@sinapticode.comE + +=head1 COPYRIGHT + +Copyright (c) 2009 Sinapticode. All rights reserved + +This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. + +=cut