2010-02-27 17:20:11 +01:00
|
|
|
##@file
|
2010-02-27 23:37:59 +01:00
|
|
|
# Twitter authentication backend file
|
2010-02-27 17:20:11 +01:00
|
|
|
|
|
|
|
##@class
|
2010-02-27 23:37:59 +01:00
|
|
|
# Twitter authentication backend class.
|
2010-02-27 17:20:11 +01:00
|
|
|
package Lemonldap::NG::Portal::AuthTwitter;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Lemonldap::NG::Portal::Simple;
|
|
|
|
|
2012-03-24 15:47:47 +01:00
|
|
|
our $VERSION = '1.2.0';
|
2010-09-23 17:09:27 +02:00
|
|
|
our $initDone;
|
2010-02-27 17:20:11 +01:00
|
|
|
|
2010-10-30 21:25:38 +02:00
|
|
|
BEGIN {
|
|
|
|
eval {
|
|
|
|
require threads::shared;
|
|
|
|
threads::shared::share($initDone);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-02-27 17:20:11 +01:00
|
|
|
## @apmethod int authInit()
|
|
|
|
# @return Lemonldap::NG::Portal constant
|
|
|
|
sub authInit {
|
|
|
|
my $self = shift;
|
2010-09-23 17:09:27 +02:00
|
|
|
return PE_OK if ($initDone);
|
|
|
|
|
2010-02-27 17:20:11 +01:00
|
|
|
unless ( $self->{twitterKey} and $self->{twitterSecret} ) {
|
|
|
|
$self->abort( 'Bad configuration',
|
|
|
|
'twitterKey and twitterSecret parameters are required' );
|
|
|
|
}
|
2010-09-23 17:09:27 +02:00
|
|
|
eval { require Net::Twitter };
|
|
|
|
$self->abort("Unable to load Net::Twitter: $@") if ($@);
|
|
|
|
|
|
|
|
$initDone = 1;
|
2010-02-27 17:20:11 +01:00
|
|
|
PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @apmethod int extractFormInfo()
|
2010-02-27 23:37:59 +01:00
|
|
|
# Authenticate users by Twitter and set user
|
2010-02-27 17:20:11 +01:00
|
|
|
# @return Lemonldap::NG::Portal constant
|
|
|
|
sub extractFormInfo {
|
|
|
|
my $self = shift;
|
2010-02-27 23:37:59 +01:00
|
|
|
|
|
|
|
# Build Net::Twitter object
|
2010-10-07 13:05:36 +02:00
|
|
|
$self->{_twitter} = Net::Twitter->new(
|
2010-02-27 17:20:11 +01:00
|
|
|
traits => [qw/API::REST OAuth/],
|
|
|
|
consumer_key => $self->{twitterKey},
|
|
|
|
consumer_secret => $self->{twitterSecret},
|
|
|
|
clientname => $self->{twitterAppName} || 'Lemonldap::NG'
|
|
|
|
);
|
2010-02-27 23:37:59 +01:00
|
|
|
|
|
|
|
# 1. Request to authenticate
|
2010-02-27 17:20:11 +01:00
|
|
|
unless ( $self->param('twitterback') ) {
|
|
|
|
$self->lmLog( 'Redirection to Twitter', 'debug' );
|
|
|
|
my $url;
|
2010-02-27 23:37:59 +01:00
|
|
|
|
|
|
|
# 1.1 Try to get token to dialog with Twitter
|
|
|
|
eval {
|
|
|
|
$url =
|
2010-10-07 13:05:36 +02:00
|
|
|
$self->{_twitter}->get_authorization_url(
|
2010-02-27 23:37:59 +01:00
|
|
|
callback => "$self->{portal}?twitterback=1&url="
|
|
|
|
. $self->get_url() );
|
|
|
|
};
|
|
|
|
|
|
|
|
# If 401 is returned => application not declared on Twitter
|
2010-02-27 17:20:11 +01:00
|
|
|
if ($@) {
|
|
|
|
if ( $@ =~ /\b401\b/ ) {
|
|
|
|
$self->abort('Twitter application undeclared');
|
|
|
|
}
|
|
|
|
$self->lmLog( "Net::Twitter error: $@", 'error' );
|
|
|
|
return PE_ERROR;
|
|
|
|
}
|
2010-02-27 23:37:59 +01:00
|
|
|
|
|
|
|
# 1.2 Store token key and secret in cookies
|
|
|
|
push @{ $self->{cookie} },
|
|
|
|
$self->cookie(
|
|
|
|
-name => '_twitTok',
|
2010-10-07 13:05:36 +02:00
|
|
|
-value => $self->{_twitter}->request_token,
|
2010-02-27 23:37:59 +01:00
|
|
|
-expires => '+3m'
|
|
|
|
),
|
|
|
|
$self->cookie(
|
|
|
|
-name => '_twitSec',
|
2010-10-07 13:05:36 +02:00
|
|
|
-value => $self->{_twitter}->request_token_secret,
|
2010-02-27 23:37:59 +01:00
|
|
|
-expires => '+3m'
|
|
|
|
);
|
|
|
|
|
|
|
|
# 1.3 Redirect user to Twitter
|
|
|
|
$self->redirect( -uri => $url );
|
2010-02-27 17:20:11 +01:00
|
|
|
$self->quit();
|
|
|
|
}
|
2010-02-27 23:37:59 +01:00
|
|
|
|
|
|
|
# 2. User is back from Twitter
|
2010-02-27 17:20:11 +01:00
|
|
|
my $request_token = $self->param('oauth_token');
|
|
|
|
my $verifier = $self->param('oauth_verifier');
|
2010-02-27 23:37:59 +01:00
|
|
|
unless ( $request_token and $verifier ) {
|
|
|
|
$self->lmLog( 'Twitter OAuth protocol error', 'error' );
|
2010-02-27 17:20:11 +01:00
|
|
|
return PE_ERROR;
|
|
|
|
}
|
2010-02-27 23:37:59 +01:00
|
|
|
|
|
|
|
# 2.1 Reconnect to Twitter
|
2010-02-27 17:20:11 +01:00
|
|
|
(
|
|
|
|
$self->{sessionInfo}->{_access_token},
|
|
|
|
$self->{sessionInfo}->{_access_token_secret}
|
2010-02-27 23:37:59 +01:00
|
|
|
)
|
2010-10-07 13:05:36 +02:00
|
|
|
= $self->{_twitter}->request_access_token(
|
2010-02-27 23:37:59 +01:00
|
|
|
token => $self->cookie('_twitTok'),
|
|
|
|
token_secret => $self->cookie('_twitSec'),
|
|
|
|
verifier => $verifier
|
|
|
|
);
|
|
|
|
|
|
|
|
# 2.2 Ask for user_timeline : I've not found an other way to access to user
|
|
|
|
# datas !
|
2010-10-07 13:05:36 +02:00
|
|
|
my $status = eval { $self->{_twitter}->user_timeline( { count => 1 } ) };
|
2010-02-27 23:37:59 +01:00
|
|
|
|
|
|
|
# 2.3 Check if user has accepted authentication
|
2010-02-27 17:20:11 +01:00
|
|
|
if ($@) {
|
|
|
|
if ( $@ =~ /\b401\b/ ) {
|
|
|
|
$self->userError('Twitter authentication refused');
|
|
|
|
return PE_BADCREDENTIALS;
|
|
|
|
}
|
|
|
|
$self->lmLog( "Net::Twitter error: $@", 'error' );
|
|
|
|
}
|
2010-02-27 23:37:59 +01:00
|
|
|
|
|
|
|
# 2.4 Set $self->{user} to twitter.com/<username>
|
2010-10-07 13:05:36 +02:00
|
|
|
$self->{_twitterUser} = $status->[0]->{user};
|
|
|
|
$self->{user} = 'twitter.com/' . $status->{_twitterUser}->{screen_name};
|
2010-02-27 17:20:11 +01:00
|
|
|
$self->lmLog( "Good Twitter authentication for $self->{user}", 'debug' );
|
2010-02-27 23:37:59 +01:00
|
|
|
|
|
|
|
# Force redirection to avoid displaying OAuth datas
|
2010-02-27 17:20:11 +01:00
|
|
|
$self->{mustRedirect} = 1;
|
2010-02-27 23:37:59 +01:00
|
|
|
|
|
|
|
# Clean temporaries cookies
|
|
|
|
push @{ $self->{cookie} },
|
|
|
|
$self->cookie( -name => '_twitTok', -value => 0, -expires => '-3m' ),
|
|
|
|
$self->cookie( -name => '_twitSec', -value => 0, -expires => '-3m' );
|
2010-02-27 17:20:11 +01:00
|
|
|
PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @apmethod int setAuthSessionInfo()
|
2010-04-14 17:37:57 +02:00
|
|
|
# Set authenticationLevel and Twitter attributes.
|
2010-02-27 17:20:11 +01:00
|
|
|
# @return Lemonldap::NG::Portal constant
|
|
|
|
sub setAuthSessionInfo {
|
|
|
|
my $self = shift;
|
|
|
|
|
2010-02-27 23:37:59 +01:00
|
|
|
# TODO: set a parameter to choose this
|
2010-02-27 17:20:11 +01:00
|
|
|
foreach (qw(screen_name location lang name url)) {
|
2010-10-07 13:05:36 +02:00
|
|
|
$self->{sessionInfo}->{$_} = $self->{_twitterUser}->{$_};
|
2010-02-27 17:20:11 +01:00
|
|
|
}
|
2010-04-14 17:37:57 +02:00
|
|
|
|
2010-09-01 18:06:01 +02:00
|
|
|
$self->{sessionInfo}->{authenticationLevel} = $self->{twitterAuthnLevel};
|
2010-04-14 17:37:57 +02:00
|
|
|
|
2010-02-27 17:20:11 +01:00
|
|
|
PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @apmethod int authenticate()
|
|
|
|
# Does nothing.
|
|
|
|
# @return Lemonldap::NG::Portal constant
|
|
|
|
sub authenticate {
|
|
|
|
PE_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-01 10:59:39 +02:00
|
|
|
## @apmethod int authFinish()
|
|
|
|
# Does nothing.
|
|
|
|
# @return Lemonldap::NG::Portal constant
|
|
|
|
sub authFinish {
|
|
|
|
PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @apmethod int authLogout()
|
|
|
|
# Does nothing
|
|
|
|
# @return Lemonldap::NG::Portal constant
|
|
|
|
sub authLogout {
|
|
|
|
PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @apmethod boolean authForce()
|
|
|
|
# Does nothing
|
|
|
|
# @return result
|
|
|
|
sub authForce {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-24 15:47:47 +01:00
|
|
|
## @method string getDisplayType
|
|
|
|
# @return display type
|
|
|
|
sub getDisplayType {
|
|
|
|
return "logo";
|
|
|
|
}
|
|
|
|
|
2010-02-27 17:20:11 +01:00
|
|
|
1;
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
=encoding utf8
|
|
|
|
|
2010-02-27 23:37:59 +01:00
|
|
|
Lemonldap::NG::Portal::AuthTwitter - Perl extension for building Lemonldap::NG
|
|
|
|
compatible portals with Twitter authentication.
|
2010-02-27 17:20:11 +01:00
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
use Lemonldap::NG::Portal::SharedConf;
|
|
|
|
my $portal = new Lemonldap::NG::Portal::Simple(
|
|
|
|
configStorage => {...}, # See Lemonldap::NG::Portal
|
2010-02-27 23:37:59 +01:00
|
|
|
authentication => 'Twitter',
|
2010-02-27 17:20:11 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if($portal->process()) {
|
|
|
|
# Write here the menu with CGI methods. This page is displayed ONLY IF
|
|
|
|
# the user was not redirected here.
|
2013-10-17 21:21:45 +02:00
|
|
|
print $portal->header('text/html; charset=utf-8'); # DON'T FORGET THIS (see CGI(3))
|
2010-02-27 17:20:11 +01:00
|
|
|
print "...";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# If the user enters here, IT MEANS THAT CAS REDIRECTION DOES NOT WORK
|
2013-10-17 21:21:45 +02:00
|
|
|
print $portal->header('text/html; charset=utf-8'); # DON'T FORGET THIS (see CGI(3))
|
2010-02-27 17:20:11 +01:00
|
|
|
print "<html><body><h1>Unable to work</h1>";
|
|
|
|
print "This server isn't well configured. Contact your administrator.";
|
|
|
|
print "</body></html>";
|
|
|
|
}
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This library just overload few methods of Lemonldap::NG::Portal::Simple to use
|
2010-02-27 23:37:59 +01:00
|
|
|
Twitter authentication mechanism.
|
2010-02-27 17:20:11 +01:00
|
|
|
|
|
|
|
See L<Lemonldap::NG::Portal::Simple> for usage and other methods.
|
|
|
|
|
|
|
|
=head1 SEE ALSO
|
|
|
|
|
|
|
|
L<Lemonldap::NG::Portal>, L<Lemonldap::NG::Portal::Simple>,
|
2010-10-26 08:08:16 +02:00
|
|
|
L<http://lemonldap-ng.org/>
|
2010-02-27 17:20:11 +01:00
|
|
|
|
|
|
|
=head1 AUTHOR
|
|
|
|
|
2013-01-31 06:33:10 +01:00
|
|
|
=over
|
|
|
|
|
|
|
|
=item Clement Oudot, E<lt>clem.oudot@gmail.comE<gt>
|
|
|
|
|
|
|
|
=item Xavier Guimard, E<lt>x.guimard@free.frE<gt>
|
|
|
|
|
|
|
|
=back
|
2010-02-27 17:20:11 +01:00
|
|
|
|
|
|
|
=head1 BUG REPORT
|
|
|
|
|
|
|
|
Use OW2 system to report bug or ask for features:
|
2010-10-26 08:08:16 +02:00
|
|
|
L<http://jira.ow2.org>
|
2010-02-27 17:20:11 +01:00
|
|
|
|
|
|
|
=head1 DOWNLOAD
|
|
|
|
|
|
|
|
Lemonldap::NG is available at
|
|
|
|
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>
|
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE
|
|
|
|
|
2013-01-31 06:33:10 +01:00
|
|
|
=over
|
|
|
|
|
|
|
|
=item Copyright (C) 2010 by Xavier Guimard, E<lt>x.guimard@free.frE<gt>
|
|
|
|
|
|
|
|
=item Copyright (C) 2010, 2012 by Clement Oudot, E<lt>clem.oudot@gmail.comE<gt>
|
|
|
|
|
|
|
|
=back
|
2010-02-27 17:20:11 +01:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify
|
2013-01-31 06:33:10 +01:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see L<http://www.gnu.org/licenses/>.
|
2010-02-27 17:20:11 +01:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
|