Catalyst-Authentication-Credential-OAuth

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

        <Plugin::Authentication>
            default_realm   oauth
            <realms>
                <oauth>
                    <credential>
                        class       OAuth
                        <providers>
                            <example.com>
                                consumer_key             my_app_key
                                consumer_secret          my_app_secret
                                request_token_endpoint   http://example.com/oauth/request_token
                                access_token_endpoint    http://example.com/oauth/access_token
                                user_auth_endpoint       http://example.com/oauth/authorize
                            </example.com>
                        </providers>
                    </credential>
                </oauth>
            </realms>
        </Plugin::Authentication>

    In controller code,

        sub oauth : Local {

lib/Catalyst/Authentication/Credential/OAuth.pm  view on Meta::CPAN

}

sub authenticate {
	my ($self, $c, $realm, $auth_info) = @_;

    Catalyst::Exception->throw( "Provider is not defined." )
        unless defined $auth_info->{provider} || defined $self->providers->{ $auth_info->{provider} };

    my $provider = $self->providers->{ $auth_info->{provider} };

    for ( qw/ consumer_key consumer_secret request_token_endpoint access_token_endpoint user_auth_endpoint / ) {
        Catalyst::Exception->throw( $_ . " is not defined for provider ". $auth_info->{provider} )
            unless $provider->{$_};
    }

    my %defaults = (
	consumer_key => $provider->{consumer_key},
	consumer_secret => $provider->{consumer_secret},
        timestamp => time,
        nonce => random_string( 'ccccccccccccccccccc' ),
        request_method => 'GET',

lib/Catalyst/Authentication/Credential/OAuth.pm  view on Meta::CPAN

        : $c->req->body_params->{oauth_token};

	if( $oauth_token ) {

		my $response = Net::OAuth->response( 'user auth' )->from_hash( $c->req->params );

		my $request = Net::OAuth->request( 'access token' )->new(
			%defaults,
			token => $response->token,
			token_secret => '',
			request_url => $provider->{access_token_endpoint},
			verifier => $c->req->params->{oauth_verifier},
		);
		$request->sign;

		my $ua_response = $self->ua->request( GET $request->to_url );
		Catalyst::Exception->throw( $ua_response->status_line.' '.$ua_response->content )
			unless $ua_response->is_success;

		$response = Net::OAuth->response( 'access token' )->from_post_body( $ua_response->content );

lib/Catalyst/Authentication/Credential/OAuth.pm  view on Meta::CPAN


		return $user_obj if ref $user_obj;

		$c->log->debug( 'Verified OAuth identity failed' ) if $self->debug;

		return;
	}
	else {
		my $request = Net::OAuth->request( 'request token' )->new(
			%defaults,
			request_url => $provider->{request_token_endpoint}
		);
		$request->sign;

		my $ua_response = $self->ua->request( GET $request->to_url );

		Catalyst::Exception->throw( $ua_response->status_line.' '.$ua_response->content )
			unless $ua_response->is_success;

		my $response = Net::OAuth->response( 'request token' )->from_post_body( $ua_response->content );

		$request = Net::OAuth->request( 'user auth' )->new(
			%defaults,
			token => $response->token,
		);

		$c->res->redirect( $request->to_url( $provider->{user_auth_endpoint} ) );
	}

}



1;


__END__

lib/Catalyst/Authentication/Credential/OAuth.pm  view on Meta::CPAN

    <Plugin::Authentication>
        default_realm	oauth
        <realms>
            <oauth>
                <credential>
                    class	OAuth
                    <providers>
                        <example.com>
                            consumer_key             my_app_key
                            consumer_secret          my_app_secret
                            request_token_endpoint   http://example.com/oauth/request_token
                            access_token_endpoint    http://example.com/oauth/access_token
                            user_auth_endpoint       http://example.com/oauth/authorize
                        </example.com>
                    </providers>
                </credential>
            </oauth>
        </realms>
    </Plugin::Authentication>


In controller code,



( run in 0.876 second using v1.01-cache-2.11-cpan-b61123c0432 )