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