1 package Catalyst::Authentication::Credential::OAuth;
7 has _config => ( is => 'rw' );
8 has realm => ( is => 'ro' );
9 has debug => ( is => 'rw' );
10 has defaults => ( is => 'rw' );
11 has provider => ( is => 'rw' );
13 our $VERSION = "0.01";
16 #$Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
17 use HTTP::Request::Common;
19 use String::Random qw/ random_string /;
21 use Catalyst::Exception ();
24 my ($class, $config, $c, $realm) = @_;
26 my $self = { _config => {
33 $self->debug( $self->_config->{debug} );
36 request_method => 'GET',
37 signature_method => 'HMAC-SHA1',
38 nonce => random_string( 'ccccccccccccccccccc' ),
46 my ($self, $c, $realm, $auth_info) = @_;
48 Catalyst::Exception->throw( "Provider is not defined." )
49 unless defined $self->_config->{ $auth_info->{oauth_provider} };
51 $self->provider( $self->_config->{ $auth_info->{oauth_provider} } );
56 consumer_key => $self->provider->{key},
57 consumer_secret => $self->provider->{secret},
58 callback => $c->uri_for( $c->action )->as_string,
61 $c->log_debug( "authenticate() called from " . $c->request->uri ) if $self->debug;
63 my $ua = LWP::UserAgent->new;
65 if( $c->req->params->{oauth_token} ) {
67 my $response = Net::OAuth->response( 'user auth' )->from_hash( $c->req->params );
69 Catalyst::Exception->throw( "access_token_endpoint not defined" )
70 unless $self->provider->{access_token_endpoint};
72 my $request = Net::OAuth->request( 'access token' )->new(
74 token => $response->token,
76 request_url => $self->provider->{access_token_endpoint},
81 my $ua_response = $ua->request( GET $request->to_url );
83 Catalyst::Exception->throw( $ua_response->status_line.' '.$ua_response->content )
84 unless $ua_response->is_success;
86 $response = Net::OAuth->response( 'access token' )->from_post_body( $ua_response->content );
89 token => $response->token,
90 token_secret => $response->token_secret,
91 extra_params => $response->extra_params
94 my $user_obj = $realm->find_user( $user, $c );
96 return $user_obj if ref $user_obj;
98 $c->log->debug( 'Verified OAuth identity failed' ) if $self->debug;
104 Catalyst::Exception->throw( "request_token_endpoint not defined" )
105 unless $self->provider->{request_token_endpoint};
107 my $request = Net::OAuth->request( 'request token' )->new(
108 %{ $self->defaults },
109 request_url => $self->provider->{request_token_endpoint}
114 my $ua_response = $ua->request( GET $request->to_url );
116 Catalyst::Exception->throw( $ua_response->status_line.' '.$ua_response->content )
117 unless $ua_response->is_success;
119 my $response = Net::OAuth->response( 'request token' )->from_post_body( $ua_response->content );
121 Catalyst::Exception->throw( "user_auth_endpoint not defined" )
122 unless $self->provider->{user_auth_endpoint};
124 $request = Net::OAuth->request( 'user auth' )->new(
125 %{ $self->defaults },
126 token => $response->token,
129 $c->res->redirect( $request->to_url( $self->provider->{user_auth_endpoint} ) );
143 Catalyst::Authentication::Credential::OAuth - OAuth credential for Catalyst::Plugin::Authentication framework.
156 Session::Store::FastMmap
157 Session::State::Cookie
163 <Plugin::Authentication>
173 request_token_endpoint http://example.com/oauth/request_token
174 access_token_endpoint http://example.com/oauth/access_token
175 user_auth_endpoint http://example.com/oauth/authorize
179 </Plugin::Authentication>
187 if( $c->authenticate( { oauth_provider => 'example_provider' } ) ) {
188 #do something with $c->user
198 =item $c->user->token
200 =item $c->user->token_secret
202 =item $c->user->extra_params - whatever other parameters the provider sends back
208 Cosmin Budrica E<lt>cosmin@sinapticode.comE<gt>
210 Bogdan Lucaciu E<lt>bogdan@sinapticode.comE<gt>
214 Copyright (c) 2009 Sinapticode. All rights reserved
216 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.