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