s/key/api_key/, update pod
[catagits/Catalyst-Authentication-Credential-FBConnect.git] / lib / Catalyst / Authentication / Credential / FBConnect.pm
CommitLineData
6d16c3ed 1package Catalyst::Authentication::Credential::FBConnect;
b73effaf 2use Moose;
b42cdcea 3use MooseX::Types::Moose qw/ Bool /;
4use MooseX::Types::Common::String qw/ NonEmptySimpleStr /;
6d16c3ed 5use WWW::Facebook::API;
6use Catalyst::Exception ();
b42cdcea 7use namespace::autoclean;
8
9has debug => ( is => 'ro', isa => Bool, );
d6fd78f6 10has api_key => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
b42cdcea 11has secret => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
12has app_name => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
13has fbconnect => ( is => 'ro', lazy_build => 1, init_arg => undef, isa => 'WWW::Facebook::API' );
6d16c3ed 14
b73effaf 15sub BUILDARGS {
6d16c3ed 16 my ($class, $config, $c, $realm) = @_;
17
b42cdcea 18 return $config;
b73effaf 19}
6d16c3ed 20
b73effaf 21sub BUILD {
22 my ($self) = @_;
23 $self->fbconnect; # Ensure lazy value is built.
24}
6d16c3ed 25
b73effaf 26sub _build_fbconnect {
27 my $self = shift;
d6fd78f6 28
b73effaf 29 WWW::Facebook::API->new(
6d16c3ed 30 desktop => 0,
b73effaf 31 map { $_ => $self->$_() } qw/ app_name api_key secret /
32 );
6d16c3ed 33}
34
35sub authenticate {
36 my ($self, $c, $realm, $auth_info) = @_;
37
38 my $token = $c->req->method eq 'GET'
39 ? $c->req->query_params->{'auth_token'}
40 : $c->req->body_params->{'auth_token'};
41
42 if( defined $token ) {
43
44 $self->fbconnect->auth->get_session( $token );
45
46 my $user = +{
47 session_uid => $self->fbconnect->session_uid,
48 session_key => $self->fbconnect->session_key,
49 session_expires => $self->fbconnect->session_expires
c9db1450 50 };
6d16c3ed 51
52 my $user_obj = $realm->find_user( $user, $c );
c9db1450 53
6d16c3ed 54 return $user_obj if ref $user_obj;
55
56 $c->log->debug( 'Verified FBConnect itentity failed' ) if $self->debug;
57
58 return;
59 }
60 else {
7bbc3e95 61 $c->res->redirect( $self->fbconnect->get_login_url( next => $c->uri_for( $c->action, $c->req->captures, @{ $c->req->args } ) ) );
6d16c3ed 62 }
63
64}
65
661;
67
68__END__
69
70=head1 NAME
71
72Catalyst::Authentication::Credential::FBConnect - Facebook credential for Catalyst::Plugin::Authentication framework.
73
74=head1 VERSION
75
760.01
77
78=head1 SYNOPSIS
79
80In MyApp.pm
81
82 use Catalyst qw/
83 Authentication
84 Session
85 Session::Store::FastMmap
86 Session::State::Cookie
87 /;
88
89
90In myapp.conf
91
d6fd78f6 92 <Plugin::Authentication>
93 default_realm facebook
94 <realms>
95 <facebook>
6d16c3ed 96 <credential>
d6fd78f6 97 class FBConnect
98 api_key my_app_key
99 secret my_app_secret
100 app_name my_app_name
101 </credential>
102 </facebook>
103 </realms>
104 </Plugin::Authentication>
6d16c3ed 105
106
107In controller code,
108
109 sub facebook : Local {
110 my ($self, $c) = @_;
111
112 if( $c->authenticate() ) {
113 #do something with $c->user
114 }
115 }
116
117
118
119=head1 USER METHODS
120
121=over 4
122
123=item $c->user->session_uid
124
125=item $c->user->session_key
126
127=item $c->user->session_expires
128
129=back
130
131=head1 AUTHOR
132
133Cosmin Budrica E<lt>cosmin@sinapticode.comE<gt>
134
135Bogdan Lucaciu E<lt>bogdan@sinapticode.comE<gt>
136
137=head1 COPYRIGHT
138
c9db1450 139Copyright (c) 2009 Sinapticode. All rights reserved
6d16c3ed 140
141This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
142
143=cut