-
Notifications
You must be signed in to change notification settings - Fork 67
/
AbstractAuthenticationDataPublisher.java
660 lines (601 loc) · 32.4 KB
/
AbstractAuthenticationDataPublisher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.identity.data.publisher.application.authentication;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.application.authentication.framework.AuthenticationDataPublisher;
import org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig;
import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.context.SessionContext;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedIdPData;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.core.bean.context.MessageContext;
import org.wso2.carbon.identity.core.handler.AbstractIdentityMessageHandler;
import org.wso2.carbon.identity.core.model.IdentityEventListenerConfig;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.data.publisher.application.authentication.model.AuthenticationData;
import org.wso2.carbon.identity.data.publisher.application.authentication.model.SessionData;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
/**
* The data publishers which implement {@link AbstractAuthenticationDataPublisher} changed to implement
* AbstractEventHandler with this release.
* So its recommended to use implementation of AbstractEventHandlers for new data publishers.
*
* @since 5.1.9
*
* @deprecated to use {@link org.wso2.carbon.identity.event.handler.AbstractEventHandler} \}
*/
@Deprecated
public abstract class AbstractAuthenticationDataPublisher extends AbstractIdentityMessageHandler implements
AuthenticationDataPublisher {
private static final Log log = LogFactory.getLog(AbstractAuthenticationDataPublisher.class);
/**
* Publish authentication success
*
* @param request Request which comes to the framework for authentication
* @param context Authentication context
* @param params Other parameters which are need to be passed
*/
public void publishAuthenticationStepSuccess(HttpServletRequest request, AuthenticationContext context,
Map<String, Object> params) {
if (log.isDebugEnabled()) {
log.debug("Publishing authentication step success");
}
AuthenticationData authenticationData = new AuthenticationData();
int step = context.getCurrentStep();
if (context.getExternalIdP() == null) {
authenticationData.setIdentityProvider(FrameworkConstants.LOCAL_IDP_NAME);
} else {
authenticationData.setIdentityProvider(context.getExternalIdP().getIdPName());
}
Object userObj = params.get(FrameworkConstants.AnalyticsAttributes.USER);
if (userObj != null && userObj instanceof AuthenticatedUser) {
AuthenticatedUser user = (AuthenticatedUser) userObj;
authenticationData.setTenantDomain(user.getTenantDomain());
authenticationData.setUserStoreDomain(user.getUserStoreDomain());
if (StringUtils.isNotBlank(user.getUserName())) {
authenticationData.setUsername(user.getUserName());
} else {
authenticationData.setUsername(user.getAuthenticatedSubjectIdentifier());
}
}
Object isFederatedObj = params.get(FrameworkConstants.AnalyticsAttributes.IS_FEDERATED);
if (isFederatedObj != null) {
boolean isFederated = (Boolean) isFederatedObj;
if (isFederated) {
authenticationData.setIdentityProviderType(FrameworkConstants.FEDERATED_IDP_NAME);
} else {
authenticationData.setIdentityProviderType(FrameworkConstants.LOCAL_IDP_NAME);
authenticationData.setLocalUsername(authenticationData.getUsername());
}
}
authenticationData.setContextId(context.getContextIdentifier());
authenticationData.setEventId(UUID.randomUUID().toString());
authenticationData.setEventType(AuthPublisherConstants.STEP_EVENT);
authenticationData.setAuthnSuccess(false);
if (request != null) {
authenticationData.setRemoteIp(IdentityUtil.getClientIpAddress(request));
} else {
authenticationData.setRemoteIp((String) params.get(AuthPublisherConstants.REMOTE_IP_ADDRESS));
}
authenticationData.setServiceProvider(context.getServiceProviderName());
authenticationData.setInboundProtocol(context.getRequestType());
authenticationData.setRememberMe(context.isRememberMe());
authenticationData.setForcedAuthn(context.isForceAuthenticate());
authenticationData.setPassive(context.isPassiveAuthenticate());
authenticationData.setInitialLogin(false);
String authenticator = AuthnDataPublisherUtils.replaceIfNotAvailable(AuthPublisherConstants.CONFIG_PREFIX +
AuthPublisherConstants.AUTHENTICATOR_NAME, (String) params.get(FrameworkConstants.AUTHENTICATOR));
authenticationData.setAuthenticator(authenticator);
authenticationData.setSuccess(true);
authenticationData.setStepNo(step);
authenticationData.addParameter(AuthPublisherConstants.TENANT_ID, AuthnDataPublisherUtils
.getTenantDomains(context.getTenantDomain(), authenticationData.getTenantDomain()));
authenticationData.addParameter(AuthPublisherConstants.RELYING_PARTY, context.getRelyingParty());
doPublishAuthenticationStepSuccess(authenticationData);
}
/**
* Published authentication step failure
*
* @param request Incoming Http request to framework for authentication
* @param context Authentication Context
* @param params Other relevant parameters which needs to be published
*/
public void publishAuthenticationStepFailure(HttpServletRequest request, AuthenticationContext context,
Map<String, Object> params) {
if (log.isDebugEnabled()) {
log.debug("Publishing authentication step failure");
}
AuthenticationData authenticationData = new AuthenticationData();
int step = context.getCurrentStep();
if (context.getExternalIdP() == null) {
authenticationData.setIdentityProvider(FrameworkConstants.LOCAL_IDP_NAME);
} else {
authenticationData.setIdentityProvider(context.getExternalIdP().getIdPName());
}
Object userObj = params.get(FrameworkConstants.AnalyticsAttributes.USER);
if (userObj != null && userObj instanceof User) {
User user = (User) userObj;
authenticationData.setTenantDomain(user.getTenantDomain());
authenticationData.setUserStoreDomain(user.getUserStoreDomain());
authenticationData.setUsername(user.getUserName());
} else if (userObj != null && userObj instanceof AuthenticatedUser) {
AuthenticatedUser user = (AuthenticatedUser) userObj;
authenticationData.setTenantDomain(user.getTenantDomain());
authenticationData.setUserStoreDomain(user.getUserStoreDomain());
if (StringUtils.isNotEmpty(user.getUserName())) {
authenticationData.setUsername(user.getUserName());
} else {
authenticationData.setUsername(user.getAuthenticatedSubjectIdentifier());
}
}
Object isFederatedObj = params.get(FrameworkConstants.AnalyticsAttributes.IS_FEDERATED);
if (isFederatedObj != null) {
boolean isFederated = (Boolean) isFederatedObj;
if (isFederated) {
authenticationData.setIdentityProviderType(FrameworkConstants.FEDERATED_IDP_NAME);
} else {
authenticationData.setIdentityProviderType(FrameworkConstants.LOCAL_IDP_NAME);
authenticationData.setLocalUsername(authenticationData.getUsername());
}
}
authenticationData.setContextId(context.getContextIdentifier());
authenticationData.setEventId(UUID.randomUUID().toString());
authenticationData.setEventType(AuthPublisherConstants.STEP_EVENT);
authenticationData.setAuthnSuccess(false);
if (request != null) {
authenticationData.setRemoteIp(IdentityUtil.getClientIpAddress(request));
} else {
authenticationData.setRemoteIp((String) params.get(AuthPublisherConstants.REMOTE_IP_ADDRESS));
}
authenticationData.setServiceProvider(context.getServiceProviderName());
authenticationData.setInboundProtocol(context.getRequestType());
authenticationData.setRememberMe(context.isRememberMe());
authenticationData.setForcedAuthn(context.isForceAuthenticate());
authenticationData.setPassive(context.isPassiveAuthenticate());
authenticationData.setInitialLogin(false);
String authenticator = AuthnDataPublisherUtils.replaceIfNotAvailable(AuthPublisherConstants.CONFIG_PREFIX +
AuthPublisherConstants.AUTHENTICATOR_NAME, (String) params.get(FrameworkConstants.AUTHENTICATOR));
authenticationData.setAuthenticator(authenticator);
authenticationData.setSuccess(false);
authenticationData.setStepNo(step);
// Should publish the event to both SP tenant domain and the tenant domain of the user who did the login
// attempt
if (context.getSequenceConfig() != null && context.getSequenceConfig().getApplicationConfig() != null && context
.getSequenceConfig().getApplicationConfig().isSaaSApp()) {
authenticationData.addParameter(AuthPublisherConstants.TENANT_ID, AuthnDataPublisherUtils
.getTenantDomains(context.getTenantDomain(), authenticationData.getTenantDomain()));
} else {
authenticationData.addParameter(AuthPublisherConstants.TENANT_ID, AuthnDataPublisherUtils
.getTenantDomains(context.getTenantDomain(), null));
}
authenticationData.addParameter(AuthPublisherConstants.RELYING_PARTY, context.getRelyingParty());
doPublishAuthenticationStepFailure(authenticationData);
}
/**
* Publishes authentication success
*
* @param request Incoming request for authentication
* @param context Authentication context
* @param params Other relevant parameters which needs to be published
*/
public void publishAuthenticationSuccess(HttpServletRequest request, AuthenticationContext context,
Map<String, Object> params) {
if (log.isDebugEnabled()) {
log.debug("Publishing authentication success");
}
AuthenticationData authenticationData = new AuthenticationData();
Object userObj = params.get(FrameworkConstants.AnalyticsAttributes.USER);
if (userObj != null && userObj instanceof AuthenticatedUser) {
AuthenticatedUser user = (AuthenticatedUser) userObj;
authenticationData.setUsername(user.getUserName());
}
Object hasFederatedStepObj = context.getProperty(FrameworkConstants.AnalyticsAttributes.HAS_FEDERATED_STEP);
Object hasLocalStepObj = context.getProperty(FrameworkConstants.AnalyticsAttributes.HAS_LOCAL_STEP);
Object isInitialLoginObj = context.getProperty(FrameworkConstants.AnalyticsAttributes.IS_INITIAL_LOGIN);
boolean hasFederated = false;
boolean hasLocal = false;
boolean isInitialLogin = false;
boolean hasPreviousLocalStep = hasPreviousLocalEvent(context);
if (hasFederatedStepObj != null) {
hasFederated = (Boolean) hasFederatedStepObj;
}
if (isInitialLoginObj != null) {
isInitialLogin = (Boolean) isInitialLoginObj;
}
if (hasLocalStepObj != null) {
hasLocal = (Boolean) hasLocalStepObj;
}
// If a previous successful local step is there, we need to skip this event from local events
if (!hasPreviousLocalStep && hasFederated && hasLocal) {
authenticationData.setIdentityProviderType(FrameworkConstants.FEDERATED_IDP_NAME + "," +
FrameworkConstants.LOCAL_IDP_NAME);
authenticationData.setStepNo(getLocalStepNo(context));
} else if (!hasPreviousLocalStep && hasLocal) {
authenticationData.setIdentityProviderType(FrameworkConstants.LOCAL_IDP_NAME);
authenticationData.setStepNo(getLocalStepNo(context));
} else if (hasFederated) {
authenticationData.setIdentityProviderType(FrameworkConstants.FEDERATED_IDP_NAME);
}
authenticationData.setIdentityProvider(AuthnDataPublisherUtils.getSubjectStepIDP(context));
authenticationData.setEventType(AuthPublisherConstants.OVERALL_EVENT);
authenticationData.setSuccess(true);
authenticationData.setContextId(context.getContextIdentifier());
authenticationData.setEventId(UUID.randomUUID().toString());
authenticationData.setAuthnSuccess(true);
if (request != null) {
authenticationData.setRemoteIp(IdentityUtil.getClientIpAddress(request));
} else {
authenticationData.setRemoteIp((String) params.get(AuthPublisherConstants.REMOTE_IP_ADDRESS));
}
authenticationData.setServiceProvider(context.getServiceProviderName());
authenticationData.setInboundProtocol(context.getRequestType());
authenticationData.setRememberMe(context.isRememberMe());
authenticationData.setForcedAuthn(context.isForceAuthenticate());
authenticationData.setPassive(context.isPassiveAuthenticate());
authenticationData.setInitialLogin(isInitialLogin);
authenticationData = fillLocalEvent(authenticationData, context);
authenticationData.addParameter(AuthPublisherConstants.TENANT_ID, AuthnDataPublisherUtils
.getTenantDomains(context.getTenantDomain(), authenticationData.getTenantDomain()));
authenticationData.addParameter(AuthPublisherConstants.SUBJECT_IDENTIFIER, context.getSequenceConfig()
.getAuthenticatedUser().getAuthenticatedSubjectIdentifier());
authenticationData.addParameter(AuthPublisherConstants.RELYING_PARTY, context.getRelyingParty());
authenticationData.addParameter(AuthPublisherConstants.AUTHENTICATED_IDPS, context.getSequenceConfig()
.getAuthenticatedIdPs());
doPublishAuthenticationSuccess(authenticationData);
}
/**
* Publishes authentication failure
*
* @param request Incoming authentication request
* @param context Authentication context
* @param params Other relevant parameters which needs to be published
*/
public void publishAuthenticationFailure(HttpServletRequest request, AuthenticationContext context,
Map<String, Object> params) {
if (log.isDebugEnabled()) {
log.debug("Publishing authentication failure");
}
AuthenticationData authenticationData = new AuthenticationData();
Object userObj = params.get(FrameworkConstants.AnalyticsAttributes.USER);
if (userObj != null && userObj instanceof AuthenticatedUser) {
AuthenticatedUser user = (AuthenticatedUser) userObj;
authenticationData.setTenantDomain(user.getTenantDomain());
authenticationData.setUserStoreDomain(user.getUserStoreDomain());
authenticationData.setUsername(user.getUserName());
}
authenticationData.setContextId(context.getContextIdentifier());
authenticationData.setEventId(UUID.randomUUID().toString());
authenticationData.setEventType(AuthPublisherConstants.OVERALL_EVENT);
authenticationData.setAuthnSuccess(false);
if (request != null) {
authenticationData.setRemoteIp(IdentityUtil.getClientIpAddress(request));
} else {
authenticationData.setRemoteIp((String) params.get(AuthPublisherConstants.REMOTE_IP_ADDRESS));
}
authenticationData.setServiceProvider(context.getServiceProviderName());
authenticationData.setInboundProtocol(context.getRequestType());
authenticationData.setRememberMe(context.isRememberMe());
authenticationData.setForcedAuthn(context.isForceAuthenticate());
authenticationData.setPassive(context.isPassiveAuthenticate());
authenticationData.setInitialLogin(false);
// Should publish the event to both SP tenant domain and the tenant domain of the user who did the login
// attempt
if (context.getSequenceConfig() != null && context.getSequenceConfig().getApplicationConfig
() != null && context.getSequenceConfig().getApplicationConfig().isSaaSApp()) {
authenticationData.addParameter(AuthPublisherConstants.TENANT_ID, AuthnDataPublisherUtils
.getTenantDomains(context.getTenantDomain(), authenticationData.getTenantDomain()));
} else {
authenticationData.addParameter(AuthPublisherConstants.TENANT_ID, AuthnDataPublisherUtils
.getTenantDomains(context.getTenantDomain(), null));
}
authenticationData.addParameter(AuthPublisherConstants.RELYING_PARTY, context.getRelyingParty());
doPublishAuthenticationFailure(authenticationData);
}
/**
* Publishes session creation information
*
* @param request Incoming request for authentication
* @param context Authentication Context
* @param sessionContext Session context
* @param params Other relevant parameters which needs to be published
*/
public void publishSessionCreation(HttpServletRequest request, AuthenticationContext context, SessionContext
sessionContext, Map<String, Object> params) {
if (log.isDebugEnabled()) {
log.debug("Publishing session creation");
}
SessionData sessionData = new SessionData();
Object userObj = params.get(FrameworkConstants.AnalyticsAttributes.USER);
String sessionId = (String) params.get(FrameworkConstants.AnalyticsAttributes.SESSION_ID);
String userName = null;
String userStoreDomain = null;
String tenantDomain = null;
Long terminationTime = null;
Long createdTime = null;
if (userObj != null && userObj instanceof AuthenticatedUser) {
AuthenticatedUser user = (AuthenticatedUser) userObj;
userName = user.getUserName();
userStoreDomain = user.getUserStoreDomain();
tenantDomain = user.getTenantDomain();
}
if (sessionContext != null) {
Object createdTimeObj = sessionContext.getProperty(FrameworkConstants.CREATED_TIMESTAMP);
createdTime = (Long) createdTimeObj;
terminationTime = AuthnDataPublisherUtils.getSessionExpirationTime(createdTime, createdTime,
context.getTenantDomain(), sessionContext.isRememberMe());
sessionData.setIsRememberMe(sessionContext.isRememberMe());
}
sessionData.setUser(userName);
sessionData.setUserStoreDomain(userStoreDomain);
sessionData.setTenantDomain(tenantDomain);
sessionData.setSessionId(sessionId);
sessionData.setCreatedTimestamp(createdTime);
sessionData.setUpdatedTimestamp(createdTime);
sessionData.setTerminationTimestamp(terminationTime);
sessionData.setIdentityProviders(getCommaSeparatedIDPs(sessionContext));
sessionData.setServiceProvider(context.getServiceProviderName());
sessionData.setRemoteIP(IdentityUtil.getClientIpAddress(request));
sessionData.setUserAgent(request.getHeader(AuthPublisherConstants.USER_AGENT));
if (context.getSequenceConfig().getApplicationConfig().isSaaSApp()) {
sessionData.addParameter(AuthPublisherConstants.TENANT_ID, AuthnDataPublisherUtils
.getTenantDomains(context.getTenantDomain(), sessionData.getTenantDomain()));
} else {
sessionData.addParameter(AuthPublisherConstants.TENANT_ID, new String[]{sessionData.getTenantDomain()});
}
if (params.containsKey(FrameworkConstants.AnalyticsAttributes.ACTIVE_SESSION_COUNT)){
int activeSessionCount = (int) params.get(FrameworkConstants.AnalyticsAttributes.ACTIVE_SESSION_COUNT);
sessionData.setActiveSessionCount(activeSessionCount);
}
doPublishSessionCreation(sessionData);
}
/**
* Publishes session update
*
* @param request Incoming request for authentication
* @param context Authentication context
* @param sessionContext Session context
* @param params Other relevant parameters which needs to be published
*/
public void publishSessionUpdate(HttpServletRequest request, AuthenticationContext context, SessionContext
sessionContext, Map<String, Object> params) {
if (log.isDebugEnabled()) {
log.debug("Publishing session update");
}
SessionData sessionData = new SessionData();
Object userObj = params.get(FrameworkConstants.AnalyticsAttributes.USER);
String sessionId = (String) params.get(FrameworkConstants.AnalyticsAttributes.SESSION_ID);
String userName = null;
String userStoreDomain = null;
String tenantDomain = null;
Long terminationTime = null;
Long createdTime = null;
Long currentTime = System.currentTimeMillis();
if (userObj != null && userObj instanceof AuthenticatedUser) {
AuthenticatedUser user = (AuthenticatedUser) userObj;
userName = user.getUserName();
userStoreDomain = user.getUserStoreDomain();
tenantDomain = user.getTenantDomain();
}
if (sessionContext != null) {
Object createdTimeObj = sessionContext.getProperty(FrameworkConstants.CREATED_TIMESTAMP);
createdTime = (Long) createdTimeObj;
terminationTime = AuthnDataPublisherUtils.getSessionExpirationTime(createdTime, currentTime,
context.getTenantDomain(), sessionContext.isRememberMe());
sessionData.setIsRememberMe(sessionContext.isRememberMe());
}
sessionData.setUser(userName);
sessionData.setIdentityProviders(getCommaSeparatedIDPs(sessionContext));
sessionData.setServiceProvider(context.getServiceProviderName());
sessionData.setUserStoreDomain(userStoreDomain);
sessionData.setTenantDomain(tenantDomain);
sessionData.setSessionId(sessionId);
sessionData.setCreatedTimestamp(createdTime);
sessionData.setUpdatedTimestamp(currentTime);
sessionData.setTerminationTimestamp(terminationTime);
sessionData.setRemoteIP(IdentityUtil.getClientIpAddress(request));
if (context.getSequenceConfig().getApplicationConfig().isSaaSApp()) {
sessionData.addParameter(AuthPublisherConstants.TENANT_ID, AuthnDataPublisherUtils
.getTenantDomains(context.getTenantDomain(), sessionData.getTenantDomain()));
} else {
sessionData.addParameter(AuthPublisherConstants.TENANT_ID, new String[]{sessionData.getTenantDomain()});
}
if (params.containsKey(FrameworkConstants.AnalyticsAttributes.ACTIVE_SESSION_COUNT)){
int activeSessionCount = (int) params.get(FrameworkConstants.AnalyticsAttributes.ACTIVE_SESSION_COUNT);
sessionData.setActiveSessionCount(activeSessionCount);
}
doPublishSessionUpdate(sessionData);
}
/**
* Publishes session termination
*
* @param request Incoming request for authentication
* @param context Authentication context
* @param sessionContext Session context
* @param params Other relevant parameters which needs to be published
*/
public void publishSessionTermination(HttpServletRequest request, AuthenticationContext context,
SessionContext sessionContext, Map<String, Object> params) {
if (log.isDebugEnabled()) {
log.debug("Publishing session termination");
}
SessionData sessionData = new SessionData();
Object userObj = params.get(FrameworkConstants.AnalyticsAttributes.USER);
String sessionId = (String) params.get(FrameworkConstants.AnalyticsAttributes.SESSION_ID);
String userName = null;
String userStoreDomain = null;
String tenantDomain = null;
Long createdTime = null;
Long currentTime = System.currentTimeMillis();
if (userObj != null && userObj instanceof AuthenticatedUser) {
AuthenticatedUser user = (AuthenticatedUser) userObj;
userName = user.getUserName();
userStoreDomain = user.getUserStoreDomain();
tenantDomain = user.getTenantDomain();
}
if (sessionContext != null) {
Object createdTimeObj = sessionContext.getProperty(FrameworkConstants.CREATED_TIMESTAMP);
createdTime = (Long) createdTimeObj;
sessionData.setIsRememberMe(sessionContext.isRememberMe());
}
sessionData.setUser(userName);
sessionData.setUserStoreDomain(userStoreDomain);
sessionData.setTenantDomain(tenantDomain);
sessionData.setSessionId(sessionId);
sessionData.setCreatedTimestamp(createdTime);
sessionData.setUpdatedTimestamp(currentTime);
sessionData.setIdentityProviders(getCommaSeparatedIDPs(sessionContext));
sessionData.setTerminationTimestamp(currentTime);
if (context != null) {
sessionData.setServiceProvider(context.getServiceProviderName());
sessionData.addParameter(AuthPublisherConstants.TENANT_ID,
AuthnDataPublisherUtils.getTenantDomains(context.getTenantDomain(), sessionData.getTenantDomain()));
} else {
sessionData.addParameter(AuthPublisherConstants.TENANT_ID, new String[]{sessionData.getTenantDomain()});
}
if (request != null) {
sessionData.setRemoteIP(IdentityUtil.getClientIpAddress(request));
}
if (params.containsKey(FrameworkConstants.AnalyticsAttributes.ACTIVE_SESSION_COUNT)){
int activeSessionCount = (int) params.get(FrameworkConstants.AnalyticsAttributes.ACTIVE_SESSION_COUNT);
sessionData.setActiveSessionCount(activeSessionCount);
}
doPublishSessionTermination(sessionData);
}
protected String getCommaSeparatedIDPs(SessionContext sessionContext) {
if (log.isDebugEnabled()) {
log.debug("Retrieving current IDPw for user ");
}
if (sessionContext == null || sessionContext.getAuthenticatedIdPs() == null || sessionContext
.getAuthenticatedIdPs().isEmpty()) {
return StringUtils.EMPTY;
}
Iterator iterator = sessionContext.getAuthenticatedIdPs().entrySet().iterator();
StringBuilder sb = new StringBuilder();
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry) iterator.next();
sb.append(",").append(pair.getKey());
}
if (sb.length() > 0) {
if (log.isDebugEnabled()) {
log.debug("Returning roles, " + sb.substring(1));
}
return sb.substring(1); //remove the first comma
}
return StringUtils.EMPTY;
}
protected AuthenticationData fillLocalEvent(AuthenticationData authenticationData, AuthenticationContext context) {
AuthenticatedIdPData localIDPData = null;
Map<String, AuthenticatedIdPData> previousAuthenticatedIDPs = context.getPreviousAuthenticatedIdPs();
Map<String, AuthenticatedIdPData> currentAuthenticatedIDPs = context.getCurrentAuthenticatedIdPs();
if (currentAuthenticatedIDPs != null && currentAuthenticatedIDPs.size() > 0) {
localIDPData = currentAuthenticatedIDPs.get(FrameworkConstants.LOCAL_IDP_NAME);
}
if (localIDPData == null && previousAuthenticatedIDPs != null && previousAuthenticatedIDPs.size() > 0) {
localIDPData = previousAuthenticatedIDPs.get(FrameworkConstants.LOCAL_IDP_NAME);
}
if (localIDPData != null) {
authenticationData.setLocalUsername(localIDPData.getUser().getAuthenticatedSubjectIdentifier());
authenticationData.setUserStoreDomain(localIDPData.getUser().getUserStoreDomain());
authenticationData.setTenantDomain(localIDPData.getUser().getTenantDomain());
authenticationData.setAuthenticator(localIDPData.getAuthenticator().getName());
}
return authenticationData;
}
protected int getLocalStepNo(AuthenticationContext context) {
int stepNo = 0;
Map<Integer, StepConfig> map = context.getSequenceConfig().getStepMap();
for (Map.Entry<Integer, StepConfig> entry : map.entrySet()) {
StepConfig stepConfig = entry.getValue();
if (stepConfig != null && FrameworkConstants.LOCAL_IDP_NAME.equalsIgnoreCase(stepConfig
.getAuthenticatedIdP())) {
stepNo = entry.getKey();
return stepNo;
}
}
return stepNo;
}
public boolean hasPreviousLocalEvent(AuthenticationContext context) {
Map<String, AuthenticatedIdPData> previousAuthenticatedIDPs = context.getPreviousAuthenticatedIdPs();
if (previousAuthenticatedIDPs.get(FrameworkConstants.LOCAL_IDP_NAME) != null) {
return true;
}
return false;
}
/**
* Does the publishing part of authentication step success
*
* @param authenticationData Bean with authentication information
*/
public abstract void doPublishAuthenticationStepSuccess(AuthenticationData authenticationData);
/**
* Does the publishing part of authentication step failure
*
* @param authenticationData Bean with authentication information
*/
public abstract void doPublishAuthenticationStepFailure(AuthenticationData authenticationData);
/**
* Does the publishing part of authentication success
*
* @param authenticationData Bean with authentication information
*/
public abstract void doPublishAuthenticationSuccess(AuthenticationData authenticationData);
/**
* Does the publishing part of authentication step failure
*
* @param authenticationData Bean with authentication information
*/
public abstract void doPublishAuthenticationFailure(AuthenticationData authenticationData);
/**
* Does the publishing part of session creation
*
* @param sessionData Bean with session information
*/
public abstract void doPublishSessionCreation(SessionData sessionData);
/**
* Does the publishing part of session update
*
* @param sessionData Bean with session information
*/
public abstract void doPublishSessionUpdate(SessionData sessionData);
/**
* Does the publishing part of session termination
*
* @param sessionData Bean with session information
*/
public abstract void doPublishSessionTermination(SessionData sessionData);
@Override
public boolean canHandle(MessageContext messageContext) {
return true;
}
@Override
public boolean isEnabled(MessageContext messageContext) {
IdentityEventListenerConfig identityEventListenerConfig = IdentityUtil.readEventListenerProperty
(AbstractIdentityMessageHandler.class.getName(), this.getClass().getName());
if (identityEventListenerConfig == null) {
return false;
}
return Boolean.parseBoolean(identityEventListenerConfig.getEnable());
}
}