Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Aug 30, 2023
1 parent 863c473 commit 044b02d
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 262 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>org.wso2.carbon.identity.server.api</groupId>
<artifactId>org.wso2.carbon.identity.api.expired.password.identification</artifactId>
<version>1.2.69-SNAPSHOT</version>
<version>1.2.71-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ public enum ErrorMessage {

ERROR_DATE_REGEX_MISMATCH("60002",
"Invalid date format provided.",
"The value provided for %s parameter is invalid. Date format should be yyyy-mm-dd"),
"The value provided for %s parameter is invalid. Date format should be yyyy-mm-dd."),

ERROR_INVALID_DATE("60003",
"Invalid date provided.",
"The date provided for %s parameter is invalid"),
"The date provided for %s parameter is invalid."),
PASSWORD_EXPIRY_FEATURE_NOT_ENABLED("60004",
"The password expiry feature is not enabled.",
"The password expiry feature needs to be enabled to retrieve the password expired users."),

// Server errors 650xx.
ERROR_RETRIEVING_PASSWORD_EXPIRED_USERS("65001",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<groupId>org.wso2.carbon.identity.server.api</groupId>
<artifactId>org.wso2.carbon.identity.api.expired.password.identification</artifactId>
<version>1.2.69-SNAPSHOT</version>
<version>1.2.71-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class PasswordExpiredUsersApi {
@ApiResponse(code = 403, message = "Resource Forbidden", response = Void.class),
@ApiResponse(code = 500, message = "Internal Server Error", response = Error.class)
})
public Response getPasswordExpiredUsers( @Valid@ApiParam(value = "The date after which passwords will expire.") @QueryParam("expiredAfter") String expiredAfter, @Valid@ApiParam(value = "The date after which should be excluded.") @QueryParam("excludeAfter") String excludeAfter) {
public Response getPasswordExpiredUsers( @Valid @NotNull(message = "Property cannot be null.") @ApiParam(value = "The date after which passwords will expire.",required=true) @QueryParam("expiredAfter") String expiredAfter, @Valid@ApiParam(value = "The date after which should be excluded.") @QueryParam("excludeAfter") String excludeAfter) {

return delegate.getPasswordExpiredUsers(expiredAfter, excludeAfter );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import org.wso2.carbon.identity.api.expired.password.identification.v1.model.PasswordExpiredUser;
import org.wso2.carbon.identity.api.server.common.error.APIError;
import org.wso2.carbon.identity.api.server.common.error.ErrorResponse;
import org.wso2.carbon.identity.application.authentication.framework.exception.PostAuthenticationFailedException;
import org.wso2.carbon.identity.password.expiry.exceptions.ExpiredPasswordIdentificationClientException;
import org.wso2.carbon.identity.password.expiry.exceptions.ExpiredPasswordIdentificationException;
import org.wso2.carbon.identity.password.expiry.exceptions.ExpiredPasswordIdentificationServerException;
import org.wso2.carbon.identity.password.expiry.models.PasswordExpiredUserModel;
import org.wso2.carbon.identity.password.expiry.util.PasswordPolicyUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -65,6 +67,7 @@ public List<PasswordExpiredUser> getPasswordExpiredUsers(
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) {
Expand All @@ -91,13 +94,6 @@ public List<PasswordExpiredUser> getPasswordExpiredUsers(
private void validateDates(String expiredAfter, String excludeAfter) throws
ExpiredPasswordIdentificationClientException {

// Check if the required parameter 'expiredAfter' is present.
if (StringUtils.isEmpty(expiredAfter)) {
ErrorMessage error = ErrorMessage.ERROR_REQUIRED_PARAMETER_MISSING;
throw new ExpiredPasswordIdentificationClientException(error.getCode(), error.getMessage(),
String.format(error.getDescription(), DATE_EXPIRED_AFTER));
}

// Validate the date format.
validateDateFormat(expiredAfter, DATE_EXPIRED_AFTER);
if (StringUtils.isNotEmpty(excludeAfter)) {
Expand Down Expand Up @@ -191,6 +187,8 @@ private APIError handleExpiredPasswordIdentificationException(ExpiredPasswordIde
}
if (ErrorMessage.ERROR_REQUIRED_PARAMETER_MISSING.getCode().equals(exception.getErrorCode())) {
status = Response.Status.NOT_FOUND;
} else if (ErrorMessage.PASSWORD_EXPIRY_FEATURE_NOT_ENABLED.getCode().equals(exception.getErrorCode())) {
status = Response.Status.METHOD_NOT_ALLOWED;
} else {
status = Response.Status.BAD_REQUEST;
}
Expand Down Expand Up @@ -236,4 +234,24 @@ private String includeData(ErrorMessage error, String data) {
return error.getDescription();
}
}

/**
* Validate whether password expiry feature is enabled.
*
* @param tenantDomain Tenant Domain.
* @throws ExpiredPasswordIdentificationException if password expiry feature is not enabled.
*/
private void validatePasswordExpiryFeatureEnabled (String tenantDomain)
throws ExpiredPasswordIdentificationException {

try {
if (!PasswordPolicyUtils.isPasswordExpiryEnabled(tenantDomain)) {
ErrorMessage error = ErrorMessage.PASSWORD_EXPIRY_FEATURE_NOT_ENABLED;
throw new ExpiredPasswordIdentificationClientException(error.getCode(), error.getMessage(),
error.getDescription());
}
} catch (PostAuthenticationFailedException e) {
throw new ExpiredPasswordIdentificationServerException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ info:
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'

servers:
- url: 'https://{server-url}/t/{tenant-domain}/api/expired-password-identification/v1'
- url: 'https://{server-url}/t/{tenant-domain}/api/server/v1/expired-password-identification'
variables:
server-url:
default: "localhost:9443"
Expand All @@ -31,6 +31,7 @@ paths:
parameters:
- in: query
name: expiredAfter
required: true
schema:
type: string
description: The date after which passwords will expire.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<artifactId>identity-api-server</artifactId>
<groupId>org.wso2.carbon.identity.server.api</groupId>
<version>1.2.69-SNAPSHOT</version>
<version>1.2.71-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down

0 comments on commit 044b02d

Please sign in to comment.