« blib/lib/Lemonldap/NG/Portal/Auth/_WebForm.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/2F/Yubikey.pm »
| 1 | package Lemonldap::NG::Portal::Auth::Twitter; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Lemonldap::NG::Common::FormEncode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Lemonldap::NG::Common::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Lemonldap::NG::Portal::Main::Constants qw(PE_OK PE_ERROR); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | extends 'Lemonldap::NG::Portal::Main::Auth'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our $VERSION = '2.19.0'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # INITIALIZATION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | has twitterRequestTokenURL => ( | ||||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||
| 17 | lazy => 1, | ||||||||||||||||||||||||||||
| 18 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 19 | $_[0]->conf->{twitterRequestTokenURL} | ||||||||||||||||||||||||||||
| 20 | 0 | ▶ | || 'https://api.twitter.com/oauth/request_token'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | 1 | ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | has twitterAuthorizeURL => ( | ||||||||||||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||||||||||
| 26 | lazy => 1, | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 28 | $_[0]->conf->{twitterAuthorizeURL} | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | || 'https://api.twitter.com/oauth/authorize'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 1 | ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | has twitterAccessTokenURL => ( | ||||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||
| 35 | lazy => 1, | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 37 | $_[0]->conf->{twitterAccessTokenURL} | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | || 'https://api.twitter.com/oauth/access_token'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 1 | ); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # return LWP::UserAgent object | ||||||||||||||||||||||||||||
| 43 | has ua => ( | ||||||||||||||||||||||||||||
| 44 | is => 'rw', | ||||||||||||||||||||||||||||
| 45 | lazy => 1, | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | builder => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # TODO : LWP options to use a proxy for example | ||||||||||||||||||||||||||||
| 49 | 0 | my $ua = Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} ); | |||||||||||||||||||||||||||
| 50 | 0 | $ua->env_proxy(); | |||||||||||||||||||||||||||
| 51 | 0 | return $ua; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 1 | ); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | unless ( $self->conf->{twitterKey} and $self->conf->{twitterSecret} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->conf->{"twitterKey"} and $self->conf->{"twitterSecret"}
MC/DC0%0/2
$self->conf->{"twitterKey"} and $self->conf->{"twitterSecret"}
$self->conf->{"twitterKey"}
$self->conf->{"twitterSecret"}
Truth table0%0/3
A and B
A= $self->conf->{"twitterKey"}B= $self->conf->{"twitterSecret"}
| |||||||||||||||||||||||||||||
| 58 | 0 | $self->error('twitterKey and twitterSecret parameters are required'); | |||||||||||||||||||||||||||
| 59 | 0 | return 0; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | eval { | |||||||||||||||||||||||||||
| 62 | 0 | require Net::OAuth; | |||||||||||||||||||||||||||
| 63 | 0 | $Net::OAuth::PROTOCOL_VERSION = &Net::OAuth::PROTOCOL_VERSION_1_0A(); | |||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $self->error("Unable to load Net::OAuth: $@"); | |||||||||||||||||||||||||||
| 67 | 0 | return 0; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 0 | return 1; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub extractFormInfo { | ||||||||||||||||||||||||||
|
Subroutine
extractFormInfo: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 0 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||
| 74 | 0 | my $nonce = time; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # 1. Request to authenticate | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | unless ( $req->param('twitterback') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | $self->logger->debug('Redirection to Twitter'); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # 1.1 Try to get token to dialog with Twitter | ||||||||||||||||||||||||||||
| 81 | 0 | my $callback_url = $self->p->portal; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Twitter callback parameter | ||||||||||||||||||||||||||||
| 84 | 0 | my %prm = ( twitterback => 1 ); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # Add request state parameters | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ( $req->data->{_url} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $prm{url} = $req->data->{_url}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Forward hidden fields | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ( exists $req->{portalHiddenFormValues} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | $self->logger->debug("Add hidden values to Twitter redirect URL"); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | foreach ( keys %{ $req->{portalHiddenFormValues} } ) { | |||||||||||||||||||||||||||
| 97 | 0 | $prm{$_} = $req->{portalHiddenFormValues}->{$_}; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | $callback_url .= | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | ( $callback_url =~ /\?/ ? '&' : '?' ) . build_urlencoded(%prm); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | my $request = Net::OAuth->request("request token")->new( | ||||||||||||||||||||||||||||
| 104 | consumer_key => $self->conf->{twitterKey}, | ||||||||||||||||||||||||||||
| 105 | consumer_secret => $self->conf->{twitterSecret}, | ||||||||||||||||||||||||||||
| 106 | 0 | request_url => $self->twitterRequestTokenURL, | |||||||||||||||||||||||||||
| 107 | request_method => 'POST', | ||||||||||||||||||||||||||||
| 108 | signature_method => 'HMAC-SHA1', | ||||||||||||||||||||||||||||
| 109 | timestamp => time, | ||||||||||||||||||||||||||||
| 110 | nonce => $nonce, | ||||||||||||||||||||||||||||
| 111 | callback => $callback_url, | ||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | $request->sign; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | my $request_url = $request->to_url; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | $self->logger->debug("POST $request_url to Twitter"); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | my $res = $self->ua()->post($request_url); | |||||||||||||||||||||||||||
| 121 | 0 | $self->logger->debug( "Twitter response: " . $res->as_string ); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ( $res->is_success ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | my $response = Net::OAuth->response('request token') | |||||||||||||||||||||||||||
| 125 | ->from_post_body( $res->content ); | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # 1.2 Store token key and secret in cookies (available 180s) | ||||||||||||||||||||||||||||
| 128 | 0 | $req->addCookie( | |||||||||||||||||||||||||||
| 129 | $self->p->cookie( | ||||||||||||||||||||||||||||
| 130 | name => '_twitSec', | ||||||||||||||||||||||||||||
| 131 | value => $response->token_secret, | ||||||||||||||||||||||||||||
| 132 | max_age => 180, | ||||||||||||||||||||||||||||
| 133 | ) | ||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # 1.3 Redirect user to Twitter | ||||||||||||||||||||||||||||
| 137 | 0 | $req->urldc( $self->twitterAuthorizeURL | |||||||||||||||||||||||||||
| 138 | . "?oauth_token=" | ||||||||||||||||||||||||||||
| 139 | . $response->token ); | ||||||||||||||||||||||||||||
| 140 | 0 | $self->logger->debug( "Redirect user to " . $req->{urldc} ); | |||||||||||||||||||||||||||
| 141 | 0 | $req->continue(1); | |||||||||||||||||||||||||||
| 142 | 0 | $req->steps( [] ); | |||||||||||||||||||||||||||
| 143 | 0 | return PE_OK; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | else { | ||||||||||||||||||||||||||||
| 146 | 0 | $self->logger->error( | |||||||||||||||||||||||||||
| 147 | 'Twitter OAuth protocol error: ' . $res->content ); | ||||||||||||||||||||||||||||
| 148 | 0 | return PE_ERROR; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # 2. User is back from Twitter | ||||||||||||||||||||||||||||
| 153 | 0 | my $request_token = $req->param('oauth_token'); | |||||||||||||||||||||||||||
| 154 | 0 | my $verifier = $req->param('oauth_verifier'); | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | unless ( $request_token and $verifier ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$request_token and $verifier
MC/DC0%0/2
$request_token and $verifier
$request_token
$verifier
Truth table0%0/3
A and B
A= $request_tokenB= $verifier
| |||||||||||||||||||||||||||||
| 156 | 0 | $self->logger->error('Twitter OAuth protocol error'); | |||||||||||||||||||||||||||
| 157 | 0 | return PE_ERROR; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | $self->logger->debug( | ||||||||||||||||||||||||||||
| 161 | 0 | "Get token $request_token and verifier $verifier from Twitter"); | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # 2.1 Reconnect to Twitter | ||||||||||||||||||||||||||||
| 164 | my $access = Net::OAuth->request("access token")->new( | ||||||||||||||||||||||||||||
| 165 | consumer_key => $self->conf->{twitterKey}, | ||||||||||||||||||||||||||||
| 166 | consumer_secret => $self->conf->{twitterSecret}, | ||||||||||||||||||||||||||||
| 167 | 0 | request_url => $self->twitterAccessTokenURL, | |||||||||||||||||||||||||||
| 168 | request_method => 'POST', | ||||||||||||||||||||||||||||
| 169 | signature_method => 'HMAC-SHA1', | ||||||||||||||||||||||||||||
| 170 | verifier => $verifier, | ||||||||||||||||||||||||||||
| 171 | token => $request_token, | ||||||||||||||||||||||||||||
| 172 | token_secret => $self->p->cookie( name => '_twitSec' ), | ||||||||||||||||||||||||||||
| 173 | timestamp => time, | ||||||||||||||||||||||||||||
| 174 | nonce => $nonce, | ||||||||||||||||||||||||||||
| 175 | ); | ||||||||||||||||||||||||||||
| 176 | 0 | $access->sign; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | my $access_url = $access->to_url; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | $self->logger->debug("POST $access_url to Twitter"); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | my $res_access = $self->ua()->post($access_url); | |||||||||||||||||||||||||||
| 183 | 0 | $self->logger->debug( "Twitter response: " . $res_access->as_string ); | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if ( $res_access->is_success ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 0 | my $response = Net::OAuth->response('access token') | |||||||||||||||||||||||||||
| 187 | ->from_post_body( $res_access->content ); | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | # Get user_id and screename | ||||||||||||||||||||||||||||
| 190 | 0 | $req->data->{_twitterUserId} = $response->{extra_params}->{user_id}; | |||||||||||||||||||||||||||
| 191 | $req->data->{_twitterScreenName} = | ||||||||||||||||||||||||||||
| 192 | 0 | $response->{extra_params}->{screen_name}; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | $self->logger->debug( "Get user id " | ||||||||||||||||||||||||||||
| 195 | . $req->data->{_twitterUserId} | ||||||||||||||||||||||||||||
| 196 | . " and screen name " | ||||||||||||||||||||||||||||
| 197 | 0 | . $req->data->{_twitterScreenName} ); | |||||||||||||||||||||||||||
| 198 | $req->user( | ||||||||||||||||||||||||||||
| 199 | 0 | $response->{extra_params}->{ $self->conf->{'twitterUserField'} } ); | |||||||||||||||||||||||||||
| 200 | 0 | $self->logger->debug("Good Twitter authentication for $req->{user}"); | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | else { | ||||||||||||||||||||||||||||
| 203 | 0 | $self->logger->error( | |||||||||||||||||||||||||||
| 204 | 'Twitter OAuth protocol error: ' . $res_access->content ); | ||||||||||||||||||||||||||||
| 205 | 0 | return PE_ERROR; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | # Force redirection to avoid displaying OAuth data | ||||||||||||||||||||||||||||
| 209 | 0 | $req->{mustRedirect} = 1; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # Clean temporaries cookies | ||||||||||||||||||||||||||||
| 212 | 0 | $req->addCookie( | |||||||||||||||||||||||||||
| 213 | $self->cookie( | ||||||||||||||||||||||||||||
| 214 | name => '_twitSec', | ||||||||||||||||||||||||||||
| 215 | value => 0, | ||||||||||||||||||||||||||||
| 216 | expires => 'Wed, 21 Oct 2015 00:00:00 GMT' | ||||||||||||||||||||||||||||
| 217 | ) | ||||||||||||||||||||||||||||
| 218 | ); | ||||||||||||||||||||||||||||
| 219 | 0 | return PE_OK; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 0 | ▶ | sub setAuthSessionInfo { | ||||||||||||||||||||||||||
|
Subroutine
setAuthSessionInfo: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 223 | 0 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | $req->{sessionInfo}->{authenticationLevel} = | ||||||||||||||||||||||||||||
| 226 | 0 | $req->data->{twitterAuthnLevel}; | |||||||||||||||||||||||||||
| 227 | 0 | $req->{sessionInfo}->{_twitterUserId} = $req->data->{_twitterUserId}; | |||||||||||||||||||||||||||
| 228 | $req->{sessionInfo}->{_twitterScreenName} = | ||||||||||||||||||||||||||||
| 229 | 0 | $req->data->{_twitterScreenName}; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 0 | return PE_OK; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 0 | ▶ | sub authenticate { | ||||||||||||||||||||||||||
|
Subroutine
authenticate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 235 | 0 | return PE_OK; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | ▶ | sub authLogout { | ||||||||||||||||||||||||||
|
Subroutine
authLogout: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 239 | 0 | return PE_OK; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 0 | ▶ | sub getDisplayType { | ||||||||||||||||||||||||||
|
Subroutine
getDisplayType: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 243 | 0 | return "logo"; | |||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 1 | 1; | |||||||||||||||||||||||||||