Is this more correct?
[catagits/Catalyst-Authentication-Credential-FBConnect.git] / lib / Catalyst / Authentication / Credential / FBConnect.pm
CommitLineData
6d16c3ed 1package Catalyst::Authentication::Credential::FBConnect;
b73effaf 2use Moose;
b42cdcea 3use MooseX::Types::Moose qw/ Bool /;
4use MooseX::Types::Common::String qw/ NonEmptySimpleStr /;
6d16c3ed 5use WWW::Facebook::API;
6use Catalyst::Exception ();
b42cdcea 7use namespace::autoclean;
8
9has debug => ( is => 'ro', isa => Bool, );
10has key => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
11has secret => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
12has app_name => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
13has fbconnect => ( is => 'ro', lazy_build => 1, init_arg => undef, isa => 'WWW::Facebook::API' );
6d16c3ed 14
b73effaf 15sub BUILDARGS {
6d16c3ed 16 my ($class, $config, $c, $realm) = @_;
17
b42cdcea 18 return $config;
b73effaf 19}
6d16c3ed 20
b73effaf 21sub BUILD {
22 my ($self) = @_;
23 $self->fbconnect; # Ensure lazy value is built.
24}
6d16c3ed 25
b73effaf 26sub _build_fbconnect {
27 my $self = shift;
28 WWW::Facebook::API->new(
6d16c3ed 29 desktop => 0,
b73effaf 30 map { $_ => $self->$_() } qw/ app_name api_key secret /
31 );
6d16c3ed 32}
33
34sub 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
c9db1450 49 };
6d16c3ed 50
51 my $user_obj = $realm->find_user( $user, $c );
c9db1450 52
6d16c3ed 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
7bbc3e95 61 $c->res->redirect( $self->fbconnect->get_login_url( next => $c->uri_for( $c->action, $c->req->captures, @{ $c->req->args } ) ) );
6d16c3ed 62 }
63
64}
65
661;
67
68__END__
69
70=head1 NAME
71
72Catalyst::Authentication::Credential::FBConnect - Facebook credential for Catalyst::Plugin::Authentication framework.
73
74=head1 VERSION
75
760.01
77
78=head1 SYNOPSIS
79
80In MyApp.pm
81
82 use Catalyst qw/
83 Authentication
84 Session
85 Session::Store::FastMmap
86 Session::State::Cookie
87 /;
88
89
90In 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
107In 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
133Cosmin Budrica E<lt>cosmin@sinapticode.comE<gt>
134
135Bogdan Lucaciu E<lt>bogdan@sinapticode.comE<gt>
136
137=head1 COPYRIGHT
138
c9db1450 139Copyright (c) 2009 Sinapticode. All rights reserved
6d16c3ed 140
141This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
142
143=cut