Commit | Line | Data |
6d16c3ed |
1 | package Catalyst::Authentication::Credential::FBConnect; |
b73effaf |
2 | use Moose; |
b42cdcea |
3 | use MooseX::Types::Moose qw/ Bool /; |
4 | use MooseX::Types::Common::String qw/ NonEmptySimpleStr /; |
6d16c3ed |
5 | use WWW::Facebook::API; |
6 | use Catalyst::Exception (); |
b42cdcea |
7 | use namespace::autoclean; |
8 | |
9 | has debug => ( is => 'ro', isa => Bool, ); |
d6fd78f6 |
10 | has api_key => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 ); |
b42cdcea |
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' ); |
6d16c3ed |
14 | |
b73effaf |
15 | sub BUILDARGS { |
6d16c3ed |
16 | my ($class, $config, $c, $realm) = @_; |
17 | |
b42cdcea |
18 | return $config; |
b73effaf |
19 | } |
6d16c3ed |
20 | |
b73effaf |
21 | sub BUILD { |
22 | my ($self) = @_; |
23 | $self->fbconnect; # Ensure lazy value is built. |
24 | } |
6d16c3ed |
25 | |
b73effaf |
26 | sub _build_fbconnect { |
27 | my $self = shift; |
d6fd78f6 |
28 | |
b73effaf |
29 | WWW::Facebook::API->new( |
6d16c3ed |
30 | desktop => 0, |
b73effaf |
31 | map { $_ => $self->$_() } qw/ app_name api_key secret / |
32 | ); |
6d16c3ed |
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 |
c9db1450 |
50 | }; |
6d16c3ed |
51 | |
52 | my $user_obj = $realm->find_user( $user, $c ); |
c9db1450 |
53 | |
6d16c3ed |
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 { |
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 | |
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 | |
d6fd78f6 |
92 | <Plugin::Authentication> |
93 | default_realm facebook |
94 | <realms> |
95 | <facebook> |
6d16c3ed |
96 | <credential> |
d6fd78f6 |
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> |
6d16c3ed |
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 | |
1239ff28 |
137 | With contributions from: |
138 | |
139 | Tomas Doran E<lt>bobtfish@bobtfish.netE</gt> |
140 | |
141 | |
142 | |
6d16c3ed |
143 | =head1 COPYRIGHT |
144 | |
c9db1450 |
145 | Copyright (c) 2009 Sinapticode. All rights reserved |
6d16c3ed |
146 | |
147 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |
148 | |
149 | =cut |