Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.wso2.carbon.identity.api.expired.password.identification.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.identity.core.util.IdentityCoreConstants;
import org.wso2.carbon.identity.core.util.IdentityUtil;
Expand All @@ -27,17 +29,30 @@
*/
public class ContextLoader {

private static final Log log = LogFactory.getLog(ContextLoader.class);

/**
* Retrieves loaded tenant domain from carbon context.
*
* @return tenant domain of the request is being served.
*/
public static String getTenantDomainFromContext() {

if (log.isDebugEnabled()) {
log.debug("Retrieving tenant domain from carbon context.");
}
String tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
if (IdentityUtil.threadLocalProperties.get().get(IdentityCoreConstants.TENANT_NAME_FROM_CONTEXT) != null) {
if (IdentityUtil.threadLocalProperties.get() != null &&
IdentityUtil.threadLocalProperties.get().get(IdentityCoreConstants.TENANT_NAME_FROM_CONTEXT) != null) {
tenantDomain = (String) IdentityUtil.threadLocalProperties.get()
.get(IdentityCoreConstants.TENANT_NAME_FROM_CONTEXT);
if (log.isDebugEnabled()) {
log.debug("Retrieved tenant domain from context: " + tenantDomain);
}
} else {
if (log.isDebugEnabled()) {
log.debug("Using default super tenant domain: " + tenantDomain);
}
}
return tenantDomain;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.wso2.carbon.identity.api.expired.password.identification.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.password.expiry.services.ExpiredPasswordIdentificationService;

Expand All @@ -26,6 +28,8 @@
*/
public class PasswordExpiryServiceHolder {

private static final Log log = LogFactory.getLog(PasswordExpiryServiceHolder.class);

private PasswordExpiryServiceHolder() {}

private static class ExpiredPasswordIdentificationServiceHolder {
Expand All @@ -40,6 +44,14 @@ private static class ExpiredPasswordIdentificationServiceHolder {
* @return ExpiredPassword identification Service.
*/
public static ExpiredPasswordIdentificationService getExpiredPasswordIdentificationService() {
return ExpiredPasswordIdentificationServiceHolder.SERVICE;

if (log.isDebugEnabled()) {
log.debug("Retrieving ExpiredPasswordIdentificationService from OSGi context.");
}
ExpiredPasswordIdentificationService service = ExpiredPasswordIdentificationServiceHolder.SERVICE;
if (service == null) {
log.warn("ExpiredPasswordIdentificationService is not available in the OSGi context.");
}
return service;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,33 @@ public PasswordExpiredUsersManagementApiService(
public List<PasswordExpiredUser> getPasswordExpiredUsers(
String expiredAfter, String excludeAfter, String tenantDomain) {

if (LOG.isDebugEnabled()) {
LOG.debug("Processing password expired users request for tenant: " + tenantDomain +
", expiredAfter: " + expiredAfter + ", excludeAfter: " + excludeAfter);
}
List<PasswordExpiredUserModel> passwordExpiredUsers = null;
try {
validateDates(expiredAfter, excludeAfter);
validatePasswordExpiryFeatureEnabled(tenantDomain);
LocalDateTime expiredAfterDate = convertToDateObject(expiredAfter, DATE_EXPIRED_AFTER);
LocalDateTime excludeAfterDate = convertToDateObject(excludeAfter, DATE_EXCLUDE_AFTER);
if (excludeAfterDate == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Retrieving password expired users from specific date for tenant: " + tenantDomain);
}
passwordExpiredUsers = expiredPasswordIdentificationService
.getPasswordExpiredUsersFromSpecificDate(expiredAfterDate, tenantDomain);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Retrieving password expired users between specific dates for tenant: " + tenantDomain);
}
passwordExpiredUsers = expiredPasswordIdentificationService
.getPasswordExpiredUsersBetweenSpecificDates(expiredAfterDate, excludeAfterDate, tenantDomain);
}
return buildResponse(passwordExpiredUsers);
List<PasswordExpiredUser> result = buildResponse(passwordExpiredUsers);
LOG.info("Successfully retrieved " + result.size() +
" password expired users for tenant: " + tenantDomain);
return result;
} catch (ExpiredPasswordIdentificationException e) {
throw handleExpiredPasswordIdentificationException(e,
ErrorMessage.ERROR_RETRIEVING_PASSWORD_EXPIRED_USERS, tenantDomain);
Expand Down Expand Up @@ -251,13 +264,21 @@ private String includeData(ErrorMessage error, String data) {
private void validatePasswordExpiryFeatureEnabled (String tenantDomain)
throws ExpiredPasswordIdentificationException {

if (LOG.isDebugEnabled()) {
LOG.debug("Validating password expiry feature for tenant: " + tenantDomain);
}
try {
if (!PasswordPolicyUtils.isPasswordExpiryEnabled(tenantDomain)) {
LOG.warn("Password expiry feature is not enabled for tenant: " + tenantDomain);
ErrorMessage error = ErrorMessage.PASSWORD_EXPIRY_FEATURE_NOT_ENABLED;
throw new ExpiredPasswordIdentificationClientException(error.getCode(), error.getMessage(),
error.getDescription());
}
if (LOG.isDebugEnabled()) {
LOG.debug("Password expiry feature validation successful for tenant: " + tenantDomain);
}
} catch (PostAuthenticationFailedException e) {
LOG.error("Error occurred while validating password expiry feature for tenant: " + tenantDomain, e);
throw new ExpiredPasswordIdentificationServerException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.wso2.carbon.identity.api.expired.password.identification.v1.factories;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.api.expired.password.identification.common.PasswordExpiryServiceHolder;
import org.wso2.carbon.identity.api.expired.password.identification.v1.core.PasswordExpiredUsersManagementApiService;
import org.wso2.carbon.identity.password.expiry.services.ExpiredPasswordIdentificationService;
Expand All @@ -27,17 +29,23 @@
*/
public class PasswordExpiredUsersManagementApiServiceFactory {

private static final Log LOG = LogFactory.getLog(PasswordExpiredUsersManagementApiServiceFactory.class);
private static final PasswordExpiredUsersManagementApiService SERVICE;

static {
if (LOG.isDebugEnabled()) {
LOG.debug("Initializing PasswordExpiredUsersManagementApiServiceFactory.");
}
ExpiredPasswordIdentificationService expiredPasswordIdentificationService =
PasswordExpiryServiceHolder.getExpiredPasswordIdentificationService();

if (expiredPasswordIdentificationService == null) {
throw new IllegalStateException("RolePermissionManagementService is not available from OSGi context.");
LOG.error("ExpiredPasswordIdentificationService is not available from OSGi context.");
throw new IllegalStateException("ExpiredPasswordIdentificationService is not available from OSGi context.");
}

SERVICE = new PasswordExpiredUsersManagementApiService(expiredPasswordIdentificationService);
LOG.info("PasswordExpiredUsersManagementApiService initialized successfully.");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.wso2.carbon.identity.api.expired.password.identification.v1.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.api.expired.password.identification.common.ContextLoader;
import org.wso2.carbon.identity.api.expired.password.identification.v1.PasswordExpiredUsersApiService;
import org.wso2.carbon.identity.api.expired.password.identification.v1.core.PasswordExpiredUsersManagementApiService;
Expand All @@ -30,14 +32,19 @@
*/
public class PasswordExpiredUsersApiServiceImpl implements PasswordExpiredUsersApiService {

private static final Log LOG = LogFactory.getLog(PasswordExpiredUsersApiServiceImpl.class);
private final PasswordExpiredUsersManagementApiService passwordExpiredUsersManagementApiService;

public PasswordExpiredUsersApiServiceImpl() {

try {
this.passwordExpiredUsersManagementApiService = PasswordExpiredUsersManagementApiServiceFactory
.getExpiredPasswordIdentificationService();
if (LOG.isDebugEnabled()) {
LOG.debug("PasswordExpiredUsersApiServiceImpl initialized successfully.");
}
} catch (IllegalStateException e) {
LOG.error("Error occurred while initiating password expired users management service.", e);
throw new RuntimeException("Error occurred while initiating password expired users management service.", e);
}
}
Expand All @@ -46,6 +53,10 @@ public PasswordExpiredUsersApiServiceImpl() {
public Response getPasswordExpiredUsers(String expiredAfter, String excludeAfter) {

String tenantDomain = ContextLoader.getTenantDomainFromContext();
if (LOG.isDebugEnabled()) {
LOG.debug("Retrieving password expired users for tenant: " + tenantDomain +
", expiredAfter: " + expiredAfter + ", excludeAfter: " + excludeAfter);
}
return Response.ok().entity(passwordExpiredUsersManagementApiService.getPasswordExpiredUsers(
expiredAfter, excludeAfter, tenantDomain)).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,21 @@ public class ContextLoader {
public static String getTenantDomainFromContext() {

String tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
if (IdentityUtil.threadLocalProperties.get().get(IdentityCoreConstants.TENANT_NAME_FROM_CONTEXT) != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Retrieving tenant domain from carbon context. Default tenant domain: " + tenantDomain);
}

if (IdentityUtil.threadLocalProperties.get() != null &&
IdentityUtil.threadLocalProperties.get().get(IdentityCoreConstants.TENANT_NAME_FROM_CONTEXT) != null) {
tenantDomain = (String) IdentityUtil.threadLocalProperties.get()
.get(IdentityCoreConstants.TENANT_NAME_FROM_CONTEXT);
if (LOG.isDebugEnabled()) {
LOG.debug("Retrieved tenant domain from context: " + tenantDomain);
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("No tenant domain found in context. Using default tenant domain: " + tenantDomain);
}
}
return tenantDomain;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.wso2.carbon.identity.api.idle.account.identification.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.idle.account.identification.services.IdleAccountIdentificationService;

Expand All @@ -26,6 +28,8 @@
*/
public class IdleAccountIdentificationServiceHolder {

private static final Log LOG = LogFactory.getLog(IdleAccountIdentificationServiceHolder.class);

private IdleAccountIdentificationServiceHolder() {}

private static class IdleAccountServiceHolder {
Expand All @@ -42,6 +46,13 @@ private static class IdleAccountServiceHolder {
*/
public static IdleAccountIdentificationService getIdleAccountIdentificationService() {

return IdleAccountServiceHolder.SERVICE;
if (LOG.isDebugEnabled()) {
LOG.debug("Retrieving IdleAccountIdentificationService from OSGi service registry.");
}
IdleAccountIdentificationService service = IdleAccountServiceHolder.SERVICE;
if (service == null) {
LOG.warn("IdleAccountIdentificationService is not available in the OSGi service registry.");
}
return service;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public List<InactiveUser> getInactiveUsers(String inactiveAfter, String excludeB
public List<InactiveUser> getInactiveUsers(String inactiveAfter, String excludeBefore, String tenantDomain,
String filter) throws IdleAccountIdentificationClientException {

if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Getting inactive users for tenant: %s with parameters - inactiveAfter: %s, " +
"excludeBefore: %s, filter: %s", tenantDomain, inactiveAfter, excludeBefore, filter));
}
List<InactiveUserModel> inactiveUsers = null;
try {
validateDates(inactiveAfter, excludeBefore);
Expand All @@ -113,18 +117,30 @@ public List<InactiveUser> getInactiveUsers(String inactiveAfter, String excludeB
idleAccountIdentificationService.getLimitedInactiveUsersFromSpecificDate(inactiveAfterDate,
excludeBeforeDate, tenantDomain);
}
return buildResponse(inactiveUsers);
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Retrieved %d inactive users for tenant: %s",
inactiveUsers != null ? inactiveUsers.size() : 0, tenantDomain));
}
return buildResponse(inactiveUsers != null ? inactiveUsers : new ArrayList<>());
}

List<ExpressionNode> expressionNodes = getExpressionNodes(filter);
if (validateExpressionNodes(expressionNodes)) {
boolean isDisabled = Boolean.parseBoolean(expressionNodes.get(0).getValue());
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Filtering inactive users by disabled status: %s for tenant: %s",
isDisabled, tenantDomain));
}

inactiveUsers = IdleAccountIdentificationServiceHolder.getIdleAccountIdentificationService()
.filterInactiveUsersIfDisabled(inactiveAfterDate, excludeBeforeDate, tenantDomain,
isDisabled);

return buildResponse(inactiveUsers);
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Retrieved %d filtered inactive users for tenant: %s",
inactiveUsers != null ? inactiveUsers.size() : 0, tenantDomain));
}
return buildResponse(inactiveUsers != null ? inactiveUsers : new ArrayList<>());
}
return getInactiveUsers(inactiveAfter, excludeBefore, tenantDomain);

Expand All @@ -143,8 +159,14 @@ public List<InactiveUser> getInactiveUsers(String inactiveAfter, String excludeB
private void validateDates(String inactiveAfter, String excludeBefore) throws
IdleAccountIdentificationClientException {

if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Validating dates - inactiveAfter: %s, excludeBefore: %s",
inactiveAfter, excludeBefore));
}

// Check if the required parameter 'inactiveAfter' is present.
if (StringUtils.isEmpty(inactiveAfter)) {
LOG.warn("Required parameter 'inactiveAfter' is missing.");
ErrorMessage error = ErrorMessage.ERROR_REQUIRED_PARAMETER_MISSING;
throw new IdleAccountIdentificationClientException(error.getCode(), error.getMessage(),
String.format(error.getDescription(), DATE_INACTIVE_AFTER));
Expand All @@ -168,8 +190,12 @@ private void validateDateFormat(String dateString, String dateType) throws
IdleAccountIdentificationClientException {

if (Pattern.matches(DATE_FORMAT_REGEX, dateString)) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Date format validation successful for %s: %s", dateType, dateString));
}
return;
}
LOG.warn(String.format("Invalid date format for %s: %s", dateType, dateString));
ErrorMessage error = ErrorMessage.ERROR_DATE_REGEX_MISMATCH;
throw new IdleAccountIdentificationClientException(error.getCode(), error.getMessage(),
String.format(error.getDescription(), dateType));
Expand Down Expand Up @@ -302,6 +328,8 @@ private void validateDatesCombination(LocalDateTime inactiveAfterDate, LocalDate
if (inactiveAfterDate != null && excludeBeforeDate != null
&& inactiveAfterDate.isBefore(excludeBeforeDate)) {

LOG.warn(String.format("Invalid date combination: inactiveAfter (%s) is before excludeBefore (%s)",
inactiveAfterDate, excludeBeforeDate));
ErrorMessage error = ErrorMessage.ERROR_INVALID_DATE_COMBINATION;
throw new IdleAccountIdentificationClientException(error.getCode(), error.getMessage(),
String.format(error.getDescription()));
Expand All @@ -318,6 +346,9 @@ private void validateDatesCombination(LocalDateTime inactiveAfterDate, LocalDate
private List<ExpressionNode> getExpressionNodes(String filter) throws IdleAccountIdentificationClientException {

// Filter example : isDisabled eq true.
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Processing filter expression: %s", filter));
}
List<ExpressionNode> expressionNodes = new ArrayList<>();
FilterTreeBuilder filterTreeBuilder;
if (StringUtils.isNotBlank(filter)) {
Expand All @@ -326,6 +357,7 @@ private List<ExpressionNode> getExpressionNodes(String filter) throws IdleAccoun
Node rootNode = filterTreeBuilder.buildTree();
setExpressionNodeList(rootNode, expressionNodes);
} catch (IOException | IdentityException e) {
LOG.warn(String.format("Invalid filter expression: %s", filter), e);
ErrorMessage error = ErrorMessage.ERROR_INVALID_FILTER;
throw new IdleAccountIdentificationClientException(error.getCode(), error.getMessage(),
String.format(error.getDescription()));
Expand Down
Loading