our
@ISA
= (
qw(Lemonldap::NG::Portal::_Browser)
);
our
$VERSION
=
'1.9.3'
;
our
$initDone
;
BEGIN {
eval
{
threads::shared::share(
$initDone
);
};
}
sub
authInit {
my
$self
=
shift
;
return
PE_OK
if
(
$initDone
);
unless
(
$self
->{twitterKey} and
$self
->{twitterSecret} ) {
$self
->abort(
'Bad configuration'
,
'twitterKey and twitterSecret parameters are required'
);
}
eval
{
$Net::OAuth::PROTOCOL_VERSION
=
&Net::OAuth::PROTOCOL_VERSION_1_0A
();
};
$self
->abort(
"Unable to load Net::OAuth: $@"
)
if
($@);
$initDone
= 1;
PE_OK;
}
sub
extractFormInfo {
my
$self
=
shift
;
my
$nonce
=
time
;
$self
->{twitterRequestTokenURL} ||=
$self
->{twitterAccessTokenURL} ||=
unless
(
$self
->param(
'twitterback'
) ) {
$self
->lmLog(
'Redirection to Twitter'
,
'debug'
);
my
$callback_url
=
$self
->url();
$callback_url
.=
(
$callback_url
=~ /\?/ ?
'&'
:
'?'
) .
"twitterback=1"
;
if
(
$self
->{_url} ) {
my
$url_param
=
'url='
. uri_escape(
$self
->{_url} );
$callback_url
.= (
$callback_url
=~ /\?/ ?
'&'
:
'?'
) .
$url_param
;
}
if
(
$self
->param(
$self
->{authChoiceParam} ) ) {
my
$url_param
=
$self
->{authChoiceParam} .
'='
. uri_escape(
$self
->param(
$self
->{authChoiceParam} ) );
$callback_url
.= (
$callback_url
=~ /\?/ ?
'&'
:
'?'
) .
$url_param
;
}
if
(
exists
$self
->{portalHiddenFormValues} ) {
$self
->lmLog(
"Add hidden values to Twitter redirect URL"
,
'debug'
);
foreach
(
keys
%{
$self
->{portalHiddenFormValues} } ) {
$callback_url
.=
(
$callback_url
=~ /\?/ ?
'&'
:
'?'
)
.
$_
.
'='
. uri_escape(
$self
->{portalHiddenFormValues}->{
$_
} );
}
}
my
$request
= Net::OAuth->request(
"request token"
)->new(
consumer_key
=>
$self
->{twitterKey},
consumer_secret
=>
$self
->{twitterSecret},
request_url
=>
$self
->{twitterRequestTokenURL},
request_method
=>
'POST'
,
signature_method
=>
'HMAC-SHA1'
,
timestamp
=>
time
,
nonce
=>
$nonce
,
callback
=>
$callback_url
,
);
$request
->sign;
my
$request_url
=
$request
->to_url;
$self
->lmLog(
"POST $request_url to Twitter"
,
'debug'
);
my
$res
=
$self
->ua()->post(
$request_url
);
$self
->lmLog(
"Twitter response: "
.
$res
->as_string,
'debug'
);
if
(
$res
->is_success ) {
my
$response
= Net::OAuth->response(
'request token'
)
->from_post_body(
$res
->content );
push
@{
$self
->{cookie} },
$self
->cookie(
-name
=>
'_twitSec'
,
-value
=>
$response
->token_secret,
-expires
=>
'+3m'
);
my
$authorize_url
=
$self
->{twitterAuthorizeURL} .
"?oauth_token="
.
$response
->token;
$self
->redirect(
-uri
=>
$authorize_url
);
$self
->quit();
}
else
{
$self
->lmLog(
'Twitter OAuth protocol error: '
.
$res
->content,
'error'
);
return
PE_ERROR;
}
}
my
$request_token
=
$self
->param(
'oauth_token'
);
my
$verifier
=
$self
->param(
'oauth_verifier'
);
unless
(
$request_token
and
$verifier
) {
$self
->lmLog(
'Twitter OAuth protocol error'
,
'error'
);
return
PE_ERROR;
}
$self
->lmLog(
"Get token $request_token and verifier $verifier from Twitter"
,
'debug'
);
my
$access
= Net::OAuth->request(
"access token"
)->new(
consumer_key
=>
$self
->{twitterKey},
consumer_secret
=>
$self
->{twitterSecret},
request_url
=>
$self
->{twitterAccessTokenURL},
request_method
=>
'POST'
,
signature_method
=>
'HMAC-SHA1'
,
verifier
=>
$verifier
,
token
=>
$request_token
,
token_secret
=>
$self
->cookie(
'_twitSec'
),
timestamp
=>
time
,
nonce
=>
$nonce
,
);
$access
->sign;
my
$access_url
=
$access
->to_url;
$self
->lmLog(
"POST $access_url to Twitter"
,
'debug'
);
my
$res_access
=
$self
->ua()->post(
$access_url
);
$self
->lmLog(
"Twitter response: "
.
$res_access
->as_string,
'debug'
);
if
(
$res_access
->is_success ) {
my
$response
= Net::OAuth->response(
'access token'
)
->from_post_body(
$res_access
->content );
$self
->{_twitterUserId} =
$response
->{extra_params}->{user_id};
$self
->{_twitterScreenName} =
$response
->{extra_params}->{screen_name};
$self
->lmLog(
"Get user id "
.
$self
->{_twitterUserId}
.
" and screen name "
.
$self
->{_twitterScreenName},
'debug'
);
}
else
{
$self
->lmLog(
'Twitter OAuth protocol error: '
.
$res_access
->content,
'error'
);
return
PE_ERROR;
}
$self
->{user} =
$self
->{_twitterScreenName};
$self
->lmLog(
"Good Twitter authentication for $self->{user}"
,
'debug'
);
$self
->{mustRedirect} = 1;
push
@{
$self
->{cookie} },
$self
->cookie(
-name
=>
'_twitSec'
,
-value
=> 0,
-expires
=>
'-3m'
);
PE_OK;
}
sub
setAuthSessionInfo {
my
$self
=
shift
;
$self
->{sessionInfo}->{authenticationLevel} =
$self
->{twitterAuthnLevel};
$self
->{sessionInfo}->{
'_user'
} =
$self
->{user};
$self
->{sessionInfo}->{_twitterUserId} =
$self
->{_twitterUserId};
$self
->{sessionInfo}->{_twitterScreenName} =
$self
->{_twitterScreenName};
PE_OK;
}
sub
authenticate {
PE_OK;
}
sub
authFinish {
PE_OK;
}
sub
authLogout {
PE_OK;
}
sub
authForce {
return
0;
}
sub
getDisplayType {
return
"logo"
;
}
1;