Commit | Line | Data |
6d16c3ed |
1 | package Catalyst::Authentication::Credential::FBConnect; |
b73effaf |
2 | use Moose; |
3 | use namespace::autoclean; |
6d16c3ed |
4 | |
b73effaf |
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 ); |
6d16c3ed |
10 | |
b73effaf |
11 | has realm => ( is => 'ro', required => 1, weak_ref => 1 ); |
6d16c3ed |
12 | |
13 | use WWW::Facebook::API; |
14 | use Catalyst::Exception (); |
15 | |
b73effaf |
16 | sub BUILDARGS { |
6d16c3ed |
17 | my ($class, $config, $c, $realm) = @_; |
18 | |
b73effaf |
19 | return { |
20 | %{ $config }, |
21 | %{ $realm->{config} }, # Ewww, gross hack to steal the realms config too. |
22 | realm => $realm, |
23 | }; |
24 | } |
6d16c3ed |
25 | |
b73effaf |
26 | sub BUILD { |
27 | my ($self) = @_; |
28 | $self->fbconnect; # Ensure lazy value is built. |
29 | } |
6d16c3ed |
30 | |
b73effaf |
31 | sub _build_fbconnect { |
32 | my $self = shift; |
33 | WWW::Facebook::API->new( |
6d16c3ed |
34 | desktop => 0, |
b73effaf |
35 | map { $_ => $self->$_() } qw/ app_name api_key secret / |
36 | ); |
6d16c3ed |
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 |
c9db1450 |
54 | }; |
6d16c3ed |
55 | |
56 | my $user_obj = $realm->find_user( $user, $c ); |
c9db1450 |
57 | |
6d16c3ed |
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 | |
c9db1450 |
144 | Copyright (c) 2009 Sinapticode. All rights reserved |
6d16c3ed |
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 |