blib/lib/Catalyst/Authentication/Credential/Facebook.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 71 CC 4 SCAR 20.8
CC
4
Cov
0%
authenticate
20.0
BEGIN
2.0
new
2.0
CRAP 8.0
1 package Catalyst::Authentication::Credential::Facebook;
2  
3 use strict;
4 use warnings;
5 use base qw( Class::Accessor::Fast );
6 use Data::Dumper;
7  
8 BEGIN {
9 __PACKAGE__->mk_accessors(qw/_facebook/);
10 }
11  
12 our $VERSION = "0.01";
13  
14 use Catalyst::Exception ();
15 use WWW::Facebook::API;
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 # Create a WWW::Facebook::API instance
26 $self->_facebook(
27 WWW::Facebook::API->new(
28 'desktop' => 0,
29 'format' => 'JSON',
30 'parse' => 1,
31 %{ $c->config->{'facebook'} || { } },
32 )
33 );
34  
35 return $self;
36 }
37  
38 sub authenticate {
39 my ( $self, $c, $realm, $authinfo ) = @_;
40  
41 $self->_facebook->query( $c->request );
42  
43 # get facebook_id if there is one
44 my $fb_user = $self->_facebook->canvas->get_fb_params();
45 $c->log->debug('facebook_id from facebook: '.$fb_user->{'user'});
46  
47 if (!$authinfo || $fb_user->{'user'} && !$authinfo->{'facebook_id'}) { $authinfo->{'facebook_id'} = $fb_user->{'user'}; }
48  
49 if ( $authinfo->{'facebook_id'} ) {
50 my $user_obj = $realm->find_user($authinfo, $c);
51  
52 if (ref $user_obj) {
53 return $user_obj;
54 }
55 }
56  
57 return undef;
58 }
59
60 =head1 NAME
61  
62 Catalyst::Authentication::Credential::Facebook - Facebook authentication for Catalyst
63  
64 =head1 SYNOPSIS
65  
66 In MyApp.pm
67  
68 use Catalyst qw/
69 Authentication
70 Session
71 Session::Store::FastMmap
72 Session::State::Cookie
73 Facebook
74 /;
75
76 MyApp->config(
77 "Plugin::Authentication" => {
78 default_realm => "facebook",
79 realms => {
80 'facebook' => {
81 credential => {
82 class => "Facebook",
83 },
84 },
85 },
86 },
87 );
88  
89 And then in your Controller:
90  
91 sub login : Local {
92 my ($self, $c) = @_;
93
94 if (my $user = $c->authenticate(undef,'facebook')) {
95 # user is logged in - redirect or do something
96 }
97 else {
98 # user has no account in your system
99 # detect Facebook credentials and create an account
100 # or do comething else
101 }
102 }
103  
104 =head1 DESCRIPTION
105  
106 This module handles Facebook Platform authentication in a Catalyst application.
107  
108 =head1 METHODS
109  
110 As per guidelines of L<Catalyst::Plugin::Authentication>, there are two
111 mandatory methods, C<new> and C<authenticate>.
112  
113 =head2 new()
114  
115 Will not be called by you directly, but will use the configuration you
116 provide (see above). WWW::Facebook::API is required, but we also suggest you install Catalyst::Plugin::Facebook for ultimate Facebook integration after the user is authenticated.
117  
118 =head2 authenticate( )
119  
120 Handles the authentication. Nothing more, nothing less. It returns
121 a L<Catalyst::Authentication::User::Hash> (this is a user object from your database - the user table should have a column called "facebook_id", which the authenticated Facebook user is checked against)
122  
123 =head1 AUTHOR
124  
125 Jesse Stay
126 E<lt>jesse@staynalive.comE<gt>
127 L<http://staynalive.com>
128  
129 =head1 COPYRIGHT
130  
131 This program is free software; you can redistribute
132 it and/or modify it under the same terms as Perl itself.
133  
134 The full text of the license can be found in the
135 LICENSE file included with this module.
136  
137 =head1 SEE ALSO
138  
139 L<Catalyst::Plugin::Authentication>, L<WWW::Facebook::API>
140  
141 =head1 BUGS
142  
143 C<Bugs? Impossible!>. Please report bugs to L<http://rt.cpan.org/Ticket/Create.html?Queue=Catalyst-Authentication-Credential-Facebook>
144  
145 =head1 THANKS
146  
147 Special thanks to David Romano and Clayton Scott for writing and maintaining WWW::Facebook::API
148  
149 =cut
150  
151 1;