Bump versions, changelog
[catagits/Catalyst-Authentication-Credential-OAuth.git] / lib / Catalyst / Authentication / Credential / OAuth.pm
CommitLineData
fc19db6b 1package Catalyst::Authentication::Credential::OAuth;
fc19db6b 2use Moose;
05873c3d 3use MooseX::Types::Moose qw/ Bool HashRef /;
4use MooseX::Types::Common::String qw/ NonEmptySimpleStr /;
fc19db6b 5use Net::OAuth;
6#$Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
fc19db6b 7use LWP::UserAgent;
05873c3d 8use HTTP::Request::Common;
fc19db6b 9use String::Random qw/ random_string /;
fc19db6b 10use Catalyst::Exception ();
05873c3d 11use namespace::autoclean;
fc19db6b 12
7d722318 13our $VERSION = '0.02';
14
05873c3d 15has debug => ( is => 'ro', isa => Bool );
16has providers => ( is => 'ro', isa => HashRef, required => 1 );
17has ua => ( is => 'ro', lazy_build => 1, init_arg => undef, isa => 'LWP::UserAgent' );
fc19db6b 18
05873c3d 19sub BUILDARGS {
20 my ($self, $config, $c, $realm) = @_;
fc19db6b 21
05873c3d 22 return $config;
23}
fc19db6b 24
05873c3d 25sub BUILD {
26 my ($self) = @_;
27 $self->ua; # Ensure lazy value is built.
fc19db6b 28}
29
05873c3d 30sub _build_ua {
31 my $self = shift;
2cd406c8 32
33 LWP::UserAgent->new;
05873c3d 34}
fc19db6b 35
36sub authenticate {
37 my ($self, $c, $realm, $auth_info) = @_;
fc19db6b 38
05873c3d 39 Catalyst::Exception->throw( "Provider is not defined." )
40 unless defined $auth_info->{provider} || defined $self->providers->{ $auth_info->{provider} };
fc19db6b 41
05873c3d 42 my $provider = $self->providers->{ $auth_info->{provider} };
43
44 for ( qw/ consumer_key consumer_secret request_token_endpoint access_token_endpoint user_auth_endpoint / ) {
45 Catalyst::Exception->throw( $_ . " is not defined for provider ". $auth_info->{provider} )
46 unless $provider->{$_};
47 }
48
49 my %defaults = (
50 consumer_key => $provider->{consumer_key},
51 consumer_secret => $provider->{consumer_secret},
52 timestamp => time,
53 nonce => random_string( 'ccccccccccccccccccc' ),
54 request_method => 'GET',
55 signature_method => 'HMAC-SHA1',
56 oauth_version => '1.0a',
57 callback => $c->uri_for( $c->action, $c->req->captures, @{ $c->req->args } )->as_string
58 );
fc19db6b 59
60 $c->log_debug( "authenticate() called from " . $c->request->uri ) if $self->debug;
61
05873c3d 62 my $oauth_token = $c->req->method eq 'GET'
63 ? $c->req->query_params->{oauth_token}
64 : $c->req->body_params->{oauth_token};
fc19db6b 65
05873c3d 66 if( $oauth_token ) {
fc19db6b 67
05873c3d 68 my $response = Net::OAuth->response( 'user auth' )->from_hash( $c->req->params );
fc19db6b 69
70 my $request = Net::OAuth->request( 'access token' )->new(
05873c3d 71 %defaults,
fc19db6b 72 token => $response->token,
73 token_secret => '',
05873c3d 74 request_url => $provider->{access_token_endpoint},
fc19db6b 75 );
fc19db6b 76 $request->sign;
77
05873c3d 78 my $ua_response = $self->ua->request( GET $request->to_url );
fc19db6b 79 Catalyst::Exception->throw( $ua_response->status_line.' '.$ua_response->content )
80 unless $ua_response->is_success;
05873c3d 81
fc19db6b 82 $response = Net::OAuth->response( 'access token' )->from_post_body( $ua_response->content );
83
2cd406c8 84 my $user = +{
fc19db6b 85 token => $response->token,
86 token_secret => $response->token_secret,
87 extra_params => $response->extra_params
88 };
89
90 my $user_obj = $realm->find_user( $user, $c );
91
92 return $user_obj if ref $user_obj;
93
94 $c->log->debug( 'Verified OAuth identity failed' ) if $self->debug;
95
96 return;
97 }
98 else {
fc19db6b 99 my $request = Net::OAuth->request( 'request token' )->new(
05873c3d 100 %defaults,
101 request_url => $provider->{request_token_endpoint}
fc19db6b 102 );
fc19db6b 103 $request->sign;
104
05873c3d 105 my $ua_response = $self->ua->request( GET $request->to_url );
106
fc19db6b 107 Catalyst::Exception->throw( $ua_response->status_line.' '.$ua_response->content )
108 unless $ua_response->is_success;
109
110 my $response = Net::OAuth->response( 'request token' )->from_post_body( $ua_response->content );
111
fc19db6b 112 $request = Net::OAuth->request( 'user auth' )->new(
05873c3d 113 %defaults,
fc19db6b 114 token => $response->token,
115 );
116
05873c3d 117 $c->res->redirect( $request->to_url( $provider->{user_auth_endpoint} ) );
fc19db6b 118 }
119
120}
121
122
123
1241;
125
126
127__END__
128
129=head1 NAME
130
131Catalyst::Authentication::Credential::OAuth - OAuth credential for Catalyst::Plugin::Authentication framework.
132
133=head1 VERSION
134
7d722318 1350.02
fc19db6b 136
137=head1 SYNOPSIS
138
139In MyApp.pm
140
05873c3d 141 use Catalyst qw/
142 Authentication
143 Session
144 Session::Store::FastMmap
145 Session::State::Cookie
146 /;
fc19db6b 147
148
149In myapp.conf
150
05873c3d 151 <Plugin::Authentication>
152 default_realm oauth
153 <realms>
154 <oauth>
fc19db6b 155 <credential>
05873c3d 156 class OAuth
157 <providers>
158 <example.com>
159 consumer_key my_app_key
160 consumer_secret my_app_secret
161 request_token_endpoint http://example.com/oauth/request_token
162 access_token_endpoint http://example.com/oauth/access_token
163 user_auth_endpoint http://example.com/oauth/authorize
164 </example.com>
165 </providers>
166 </credential>
167 </oauth>
168 </realms>
169 </Plugin::Authentication>
fc19db6b 170
171
172In controller code,
173
05873c3d 174 sub oauth : Local {
175 my ($self, $c) = @_;
fc19db6b 176
05873c3d 177 if( $c->authenticate( { provider => 'example.com' } ) ) {
178 #do something with $c->user
179 }
180 }
fc19db6b 181
182
183
184=head1 USER METHODS
185
186=over 4
187
188=item $c->user->token
189
190=item $c->user->token_secret
191
05873c3d 192=item $c->user->extra_params - whatever other params the provider sends back
fc19db6b 193
194=back
195
196=head1 AUTHOR
197
198Cosmin Budrica E<lt>cosmin@sinapticode.comE<gt>
199
200Bogdan Lucaciu E<lt>bogdan@sinapticode.comE<gt>
201
a726a7ce 202With contributions from:
203
204 Tomas Doran E<lt>bobtfish@bobtfish.netE</gt>
205
206
207=head1 BUGS
208
209Only tested with twitter
210
fc19db6b 211=head1 COPYRIGHT
212
2cd406c8 213Copyright (c) 2009 Sinapticode. All rights reserved
fc19db6b 214
215This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
216
217=cut