blib/lib/Lemonldap/NG/Portal/Plugins/ContextSwitching.pm untested

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 243 CC 30 SCAR 38.9
CC
30
Cov
0%
run
90.0
display
56.0
_switchContext
56.0
CRAP 49.1
1 package Lemonldap::NG::Portal::Plugins::ContextSwitching;
2  
3 use strict;
4 use Mouse;
5 use Lemonldap::NG::Portal::Main::Constants qw(
6 PE_OK
7 PE_ERROR
8 PE_NOTOKEN
9 PE_REDIRECT
10 PE_TOKENEXPIRED
11 PE_MALFORMEDUSER
12 PE_BADCREDENTIALS
13 PE_SESSIONEXPIRED
14 PE_IMPERSONATION_SERVICE_NOT_ALLOWED
15 );
16  
17 our $VERSION = '2.22.0';
18  
19 extends qw(
20 Lemonldap::NG::Portal::Main::Plugin
21 Lemonldap::NG::Portal::Lib::_tokenRule
22 Lemonldap::NG::Portal::Lib::OtherSessions
23 );
24  
25 # INITIALIZATION
26  
27 has ott => (
28 is => 'rw',
29 lazy => 1,
30 default => sub {
31 my $ott =
32 $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
33 $ott->timeout( $_[0]->{conf}->{formTimeout} );
34 return $ott;
35 }
36 );
37 has rule => ( is => 'rw', default => sub { 0 } );
38 has idRule => ( is => 'rw', default => sub { 1 } );
39 has unrestrictedUsersRule => ( is => 'rw', default => sub { 0 } );
40  
41 # Prefixed sessions id attribute
42 has prefixedSId => (
43 is => 'ro',
44 lazy => 1,
45 default => sub {
46 my $self = $_[0];
47 return $self->conf->{contextSwitchingPrefix} . '_session_id';
48 }
49 );
50  
51 sub init {
52 my ($self) = @_;
53 $self->addAuthRoute( switchcontext => 'run', ['POST'] )
54 ->addAuthRoute( switchcontext => 'display', ['GET'] );
55  
56 # Parse ContextSwitching rules
57 $self->rule(
58 $self->p->buildRule(
59 $self->conf->{contextSwitchingRule},
60 'contextSwitching'
61 )
62 );
63 return 0 unless $self->rule;
64  
65 $self->idRule(
66 $self->p->buildRule(
67 $self->conf->{contextSwitchingIdRule},
68 'contextSwitchingId'
69 )
70 );
71 return 0 unless $self->idRule;
72  
73 $self->unrestrictedUsersRule(
74 $self->p->buildRule(
75 $self->conf->{contextSwitchingUnrestrictedUsersRule},
76 'contextSwitchingUnrestrictedUsers'
77 )
78 );
79 return 0 unless $self->unrestrictedUsersRule;
80  
81 return 1;
82 }
83  
84 # RUNNING METHOD
85  
86 sub display {
87 my ( $self, $req ) = @_;
88 my ( $realSession, $realSessionId );
89 if ( $realSessionId = $req->userData->{ $self->prefixedSId } ) {
90 unless ( $realSession = $self->p->getApacheSession($realSessionId) ) {
91 $self->userLogger->info(
92 "ContextSwitching: session $realSessionId expired");
93 return $self->p->doPE( $req, PE_SESSIONEXPIRED );
94 }
95 }
96  
97 # Check access rules
98 unless ( $self->rule->( $req, $req->userData )
99 || $req->userData->{ $self->prefixedSId } )
100 {
101 $self->userLogger->warn('ContextSwitching service NOT authorized');
102 return $self->p->doPE( $req, PE_IMPERSONATION_SERVICE_NOT_ALLOWED );
103 }
104  
105 if ( $req->userData->{ $self->prefixedSId } ) {
106 $self->logger->debug('Request to stop ContextSwitching');
107 if ( $self->conf->{contextSwitchingStopWithLogout} ) {
108 $self->userLogger->notice("Stop ContextSwitching for $req->{user}");
109 $self->userLogger->info("Remove real session $realSessionId");
110 $realSession->remove;
111 return $self->p->do( $req,
112 [ @{ $self->p->beforeLogout }, 'authLogout', 'deleteSession' ]
113 );
114  
115 }
116 else {
117 $req = $self->_abortImpersonation( $req, $req->{user},
118 $realSession->data->{ $self->conf->{whatToTrace} }, 0 );
119 $self->p->updateSession( $req, $req->userData );
120 return $self->p->doPE( $req, PE_REDIRECT );
121 }
122 }
123  
124 # Display form
125 my $params = {
126 FORM_ACTION => $self->p->relativeUrl( $req, 'switchcontext' ),
127 MSG => 'contextSwitching_ON',
128 ALERTE => 'alert-danger',
129 IMPERSONATION => $self->conf->{contextSwitchingRule},
130 TOKEN => (
131 $self->ottRule->( $req, {} )
132 ? $self->ott->createToken()
133 : ''
134 )
135 };
136  
137 return $self->p->sendHtml( $req, 'contextSwitching', params => $params, );
138 }
139  
140 sub run {
141 my ( $self, $req ) = @_;
142 my $statut = PE_OK;
143 my $realId = $req->userData->{ $self->conf->{whatToTrace} };
144 my $spoofId = $req->param('spoofId') || ''; # ContextSwitching required ?
145 my $unUser = $self->unrestrictedUsersRule->( $req, $req->userData ) || 0;
146  
147 # Check token
148 if ( $self->ottRule->( $req, {} ) ) {
149 my $token = $req->param('token');
150 unless ($token) {
151 $self->userLogger->warn('ContextSwitching called without token');
152 return $self->p->doPE( $req, PE_NOTOKEN );
153 }
154 unless ( $self->ott->getToken($token) ) {
155 $self->userLogger->warn(
156 'ContextSwitching called with an expired/bad token');
157 return $self->p->doPE( $req, PE_TOKENEXPIRED );
158 }
159 }
160  
161 # Check activation rule
162 unless ( $self->rule->( $req, $req->userData ) ) {
163 $self->userLogger->warn('ContextSwitching service NOT authorized');
164 $spoofId = '';
165 return $self->p->doPE( $req, PE_IMPERSONATION_SERVICE_NOT_ALLOWED );
166 }
167  
168 # ContextSwitching required -> Check user Id
169 if ( $spoofId && $spoofId ne $req->{user} ) {
170 $self->logger->debug("Spoofed Id: $spoofId");
171 unless ( $spoofId =~ /$self->{conf}->{userControl}/ ) {
172 $self->userLogger->warn('Malformed spoofed Id');
173 $self->logger->debug(
174 "ContextSwitching tried with spoofed Id: $spoofId");
175 return $self->p->doPE( $req, PE_MALFORMEDUSER );
176 }
177 }
178 else {
179 $self->logger->debug("contextSwitching NOT required");
180 $req->urldc( $req->portal );
181 return $self->p->doPE( $req, PE_OK );
182 }
183  
184 # Create spoofed session
185 $req = $self->_switchContext( $req, $spoofId, $unUser );
186 $statut =
187 ( $req->error == PE_BADCREDENTIALS ? PE_MALFORMEDUSER : $req->error )
188 if $req->error;
189  
190 # Main session
191 $self->p->updateSession( $req, $req->sessionInfo );
192 $self->userLogger->notice(
193 "ContextSwitching: Update \"$realId\" session with \"$spoofId\" session data"
194 );
195  
196 $req->mustRedirect(1);
197 return $self->p->doPE( $req, $statut );
198 }
199  
200 sub _switchContext {
201 my ( $self, $req, $spoofId, $unUser ) = @_;
202 my $realSessionId = $req->userData->{_session_id};
203 my $realAuthLevel = $req->userData->{authenticationLevel};
204 my $realId = $req->userData->{ $self->conf->{whatToTrace} };
205 my $raz = 0;
206 $req->{user} = $spoofId;
207  
208 # Search user in database & create session
209 $req->steps( [
210 'getUser', 'setAuthSessionInfo',
211 'setSessionInfo', $self->p->groupsAndMacros,
212 'setPersistentSessionInfo', 'setLocalGroups',
213 'store', 'buildCookie'
214 ]
215 );
216 if ( my $error = $self->p->process($req) ) {
217 $self->userLogger->warn(
218 'ContextSwitching requested for an invalid user ('
219 . $req->{user}
220 . ")" )
221 if ( $error == PE_BADCREDENTIALS );
222 $self->logger->debug("Process returned error: $error");
223 $req->error($error);
224 $raz = 1;
225 }
226  
227 # Check identities rule if ContextSwitching required
228 $self->logger->info("\"$realId\" is an unrestricted user!") if $unUser;
229 unless ( $unUser || $self->idRule->( $req, $req->sessionInfo ) ) {
230 $self->userLogger->warn(
231 'ContextSwitching requested for an invalid user ('
232 . $req->{user}
233 . ")" );
234 $self->logger->debug('Identity NOT authorized');
235 $req->error(PE_MALFORMEDUSER); # Catch error to preserve protected Id
236 $raz = 1;
237 }
238  
239 $req->sessionInfo->{ $self->prefixedSId } = $realSessionId;
240  
241 return $self->_abortImpersonation( $req, $spoofId, $realId, 1 ) if $raz;
242  
243 $self->logger->debug(
244 "Update sessionInfo with real authenticationLevel: $realAuthLevel");
245 $req->sessionInfo->{authenticationLevel} = $realAuthLevel;
246 delete $req->sessionInfo->{groups};
247  
248 # Compute groups & macros again with real authenticationLevel
249 $req->steps(
250 [ 'setSessionInfo', $self->p->groupsAndMacros, 'setLocalGroups' ] );
251 if ( my $error = $self->p->process($req) ) {
252 $self->logger->debug(
253 "ContextSwitching: Process returned error: $error");
254 $req->error($error);
255 }
256  
257 $self->userLogger->notice(
258 "Start ContextSwitching: \"$realId\" becomes \"$spoofId\"");
259 return $req;
260 }
261  
262 sub _abortImpersonation {
263 my ( $self, $req, $spoofId, $realId, $abort ) = @_;
264 my $type = $abort ? 'sessionInfo' : 'userData';
265 my $realSessionId = $req->{$type}->{ $self->prefixedSId };
266 my $session;
267 unless ( $session = $self->p->getApacheSession($realSessionId) ) {
268 $self->userLogger->info("Session $session expired");
269 return $req->error(PE_SESSIONEXPIRED);
270 }
271  
272 if ($abort) {
273 $self->userLogger->notice(
274 "Abort ContextSwitching: \"$spoofId\" by \"$realId\"");
275 if ( my $abortSession = $self->p->getApacheSession( $req->id ) ) {
276 $abortSession->remove;
277 }
278 else {
279 $self->userLogger->info(
280 "ContextSwitching: session " . $req->id . " expired" );
281 }
282 }
283 else {
284 $self->userLogger->notice(
285 "Stop ContextSwitching for \"$realId\" with uid \"$spoofId\"");
286 $self->p->deleteSession($req);
287 }
288  
289 # Restore real session
290 $req->{$type} = { %{ $session->data } };
291 $req->{user} = $session->data->{_user};
292 $req->urldc( $req->portal );
293 $req->id($realSessionId);
294 $self->p->buildCookie($req);
295 delete $req->{$type}->{ $self->prefixedSId };
296  
297 return $req;
298 }
299  
300 sub displayLink {
301 my ( $self, $req ) = @_;
302 return 'OFF'
303 if $req->userData->{ $self->prefixedSId };
304 return 'ON' if $self->rule->( $req, $req->userData );
305 }
306  
307 1;