Strip trailing space
[catagits/Catalyst-Authentication-Credential-FBConnect.git] / lib / Catalyst / Authentication / Credential / FBConnect.pm
CommitLineData
6d16c3ed 1package Catalyst::Authentication::Credential::FBConnect;
2use strict;
3use warnings;
4
5use Moose;
6
7has _config => ( is => 'rw' );
8has debug => ( is => 'rw' );
9has key => ( is => 'rw' );
10has secret => ( is => 'rw' );
11has app_name => ( is => 'rw' );
12has fbconnect => ( is => 'rw' );
13
14use WWW::Facebook::API;
15use Catalyst::Exception ();
16
17sub 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 ) );
c9db1450 39
6d16c3ed 40 return $self;
41}
42
43sub 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
c9db1450 58 };
6d16c3ed 59
60 my $user_obj = $realm->find_user( $user, $c );
c9db1450 61
6d16c3ed 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
751;
76
77__END__
78
79=head1 NAME
80
81Catalyst::Authentication::Credential::FBConnect - Facebook credential for Catalyst::Plugin::Authentication framework.
82
83=head1 VERSION
84
850.01
86
87=head1 SYNOPSIS
88
89In MyApp.pm
90
91 use Catalyst qw/
92 Authentication
93 Session
94 Session::Store::FastMmap
95 Session::State::Cookie
96 /;
97
98
99In 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
116In 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
142Cosmin Budrica E<lt>cosmin@sinapticode.comE<gt>
143
144Bogdan Lucaciu E<lt>bogdan@sinapticode.comE<gt>
145
146=head1 COPYRIGHT
147
c9db1450 148Copyright (c) 2009 Sinapticode. All rights reserved
6d16c3ed 149
150This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
151
152=cut