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
1 package Catalyst::Authentication::Credential::FBConnect;
2 use Moose;
3 use namespace::autoclean;
4
5 has debug => ( is => 'ro' );
6 has key => ( is => 'ro' );
7 has secret => ( is => 'ro' );
8 has app_name => ( is => 'ro' );
9 has fbconnect => ( is => 'ro', lazy_build => 1, init_arg => undef );
10
11 has realm => ( is => 'ro', required => 1, weak_ref => 1 );
12
13 use WWW::Facebook::API;
14 use Catalyst::Exception ();
15
16 sub BUILDARGS {
17         my ($class, $config, $c, $realm) = @_;
18
19     return {
20         %{ $config },
21         %{ $realm->{config} }, # Ewww, gross hack to steal the realms config too.
22         realm => $realm,
23     };
24 }
25
26 sub BUILD {
27     my ($self) = @_;
28     $self->fbconnect; # Ensure lazy value is built.
29 }
30
31 sub _build_fbconnect {
32     my $self = shift;
33         WWW::Facebook::API->new(
34                 desktop => 0,
35                 map { $_ => $self->$_() } qw/ app_name api_key secret /
36         );
37 }
38
39 sub 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
54                 };
55
56                 my $user_obj = $realm->find_user( $user, $c );
57
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
71 1;
72
73 __END__
74
75 =head1 NAME
76
77 Catalyst::Authentication::Credential::FBConnect - Facebook credential for Catalyst::Plugin::Authentication framework.
78
79 =head1 VERSION
80
81 0.01
82
83 =head1 SYNOPSIS
84
85 In MyApp.pm
86
87  use Catalyst qw/
88       Authentication
89       Session
90       Session::Store::FastMmap
91       Session::State::Cookie
92  /;
93
94
95 In 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
112 In 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
138 Cosmin Budrica E<lt>cosmin@sinapticode.comE<gt>
139
140 Bogdan Lucaciu E<lt>bogdan@sinapticode.comE<gt>
141
142 =head1 COPYRIGHT
143
144 Copyright (c) 2009 Sinapticode. All rights reserved
145
146 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
147
148 =cut