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