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 MooseX::Types::Moose qw/ Bool /;
+use MooseX::Types::Common::String qw/ NonEmptySimpleStr /;
use WWW::Facebook::API;
use Catalyst::Exception ();
+use namespace::autoclean;
-sub new {
- my ($class, $config, $c, $realm) = @_;
+has debug => ( is => 'ro', isa => Bool, );
+has api_key => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
+has secret => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
+has app_name => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
+has fbconnect => ( is => 'ro', lazy_build => 1, init_arg => undef, isa => 'WWW::Facebook::API' );
- my $self = { _config => {
- %{ $config },
- %{ $realm->{config} }
- } };
+sub BUILDARGS {
+ my ($class, $config, $c, $realm) = @_;
- bless $self, $class;
+ return $config;
+}
- $self->debug( $self->_config->{debug} );
+sub BUILD {
+ my ($self) = @_;
+ $self->fbconnect; # Ensure lazy value is built.
+}
- $self->key( $self->_config->{key} );
- $self->secret( $self->_config->{secret} );
- $self->app_name( $self->_config->{app_name} );
+sub _build_fbconnect {
+ my $self = shift;
- $self->fbconnect( WWW::Facebook::API->new(
+ WWW::Facebook::API->new(
desktop => 0,
- app_name => $self->app_name,
- api_key => $self->key,
- secret => $self->secret
- ) );
-
- return $self;
+ map { $_ => $self->$_() } qw/ app_name api_key secret /
+ );
}
sub authenticate {
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 ) ) );
+ $c->res->redirect( $self->fbconnect->get_login_url( next => $c->uri_for( $c->action, $c->req->captures, @{ $c->req->args } ) ) );
}
}
In myapp.conf
- <Plugin::Authentication>
- default_realm facebook
- <realms>
- <facebook>
+ <Plugin::Authentication>
+ default_realm facebook
+ <realms>
+ <facebook>
<credential>
- class FBConnect
- </credential>
- key my_app_key
- secret my_app_secret
- app_name my_app_name
- </facebook>
- </realms>
-</Plugin::Authentication>
+ class FBConnect
+ api_key my_app_key
+ secret my_app_secret
+ app_name my_app_name
+ </credential>
+ </facebook>
+ </realms>
+ </Plugin::Authentication>
In controller code,
Bogdan Lucaciu E<lt>bogdan@sinapticode.comE<gt>
+With contributions from:
+
+ Tomas Doran E<lt>bobtfish@bobtfish.netE</gt>
+
+
+
=head1 COPYRIGHT
-Copyright (c) 2009 Sinapticode. All rights reserved
+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.