Comments
[catagits/Catalyst-Authentication-Credential-FBConnect.git] / lib / Catalyst / Authentication / Credential / FBConnect.pm
CommitLineData
6d16c3ed 1package Catalyst::Authentication::Credential::FBConnect;
2use strict;
3use warnings;
4
5c5e442d 5use Moose; # Moose gave you strict and warnings already
6d16c3ed 6
5c5e442d 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 :/
10has _config => ( is => 'rw' ); # Do these actually need to be rw??
6d16c3ed 11has debug => ( is => 'rw' );
12has key => ( is => 'rw' );
13has secret => ( is => 'rw' );
14has app_name => ( is => 'rw' );
15has fbconnect => ( is => 'rw' );
16
17use WWW::Facebook::API;
18use Catalyst::Exception ();
19
5c5e442d 20sub new { # Writing your own ->new method makes Moose sad, implement BUILDARGS
21 # and BUILD instead
6d16c3ed 22 my ($class, $config, $c, $realm) = @_;
23
24 my $self = { _config => {
25 %{ $config },
5c5e442d 26 %{ $realm->{config} } # Ewww, gross hack to steal the realms config too.
6d16c3ed 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 ) );
c9db1450 43
6d16c3ed 44 return $self;
45}
46
47sub 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
c9db1450 62 };
6d16c3ed 63
64 my $user_obj = $realm->find_user( $user, $c );
c9db1450 65
6d16c3ed 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
791;
80
81__END__
82
83=head1 NAME
84
85Catalyst::Authentication::Credential::FBConnect - Facebook credential for Catalyst::Plugin::Authentication framework.
86
87=head1 VERSION
88
890.01
90
91=head1 SYNOPSIS
92
93In MyApp.pm
94
95 use Catalyst qw/
96 Authentication
97 Session
98 Session::Store::FastMmap
99 Session::State::Cookie
100 /;
101
102
103In 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
120In 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
146Cosmin Budrica E<lt>cosmin@sinapticode.comE<gt>
147
148Bogdan Lucaciu E<lt>bogdan@sinapticode.comE<gt>
149
150=head1 COPYRIGHT
151
c9db1450 152Copyright (c) 2009 Sinapticode. All rights reserved
6d16c3ed 153
154This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
155
156=cut