Is this more correct?
[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 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         WWW::Facebook::API->new(
29                 desktop => 0,
30                 map { $_ => $self->$_() } qw/ app_name api_key secret /
31         );
32 }
33
34 sub authenticate {
35         my ($self, $c, $realm, $auth_info) = @_;
36
37         my $token = $c->req->method eq 'GET'
38                 ? $c->req->query_params->{'auth_token'}
39                 : $c->req->body_params->{'auth_token'};
40
41         if( defined $token ) {
42
43                 $self->fbconnect->auth->get_session( $token );
44
45                 my $user = +{
46                         session_uid => $self->fbconnect->session_uid,
47                         session_key => $self->fbconnect->session_key,
48                         session_expires => $self->fbconnect->session_expires
49                 };
50
51                 my $user_obj = $realm->find_user( $user, $c );
52
53                 return $user_obj if ref $user_obj;
54
55                 $c->log->debug( 'Verified FBConnect itentity failed' ) if $self->debug;
56
57                 return;
58         }
59         else {
60
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                 </credential>
99                 key my_app_key
100                 secret my_app_secret
101                 app_name my_app_name
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