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