This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a0ebfe
commit c10ae38
Showing
46 changed files
with
1,921 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
api/src/main/java/org/openmrs/module/cfl/api/service/CaptchaService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* <p> | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
|
||
package org.openmrs.module.cfl.api.service; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** This service is used to get user's response to the reCAPTCHA challenge and validate response. */ | ||
public interface CaptchaService { | ||
|
||
/** | ||
* Retrieves and validates response | ||
* | ||
* @param request | ||
*/ | ||
void processResponse(HttpServletRequest request); | ||
} |
18 changes: 18 additions & 0 deletions
18
api/src/main/java/org/openmrs/module/cfl/api/service/CustomUserAppService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.openmrs.module.cfl.api.service; | ||
|
||
import java.util.List; | ||
import org.openmrs.Location; | ||
import org.openmrs.module.appframework.domain.Extension; | ||
|
||
public interface CustomUserAppService { | ||
|
||
/** | ||
* Sets specific app extensions based on user location. For example if registration form tile is | ||
* displayed on home page, this method checks if there is a specific registration form for current | ||
* user location (based on 'Project' location attribute) and replaces it. | ||
* | ||
* @param location user location | ||
* @param extensions current user app extensions | ||
*/ | ||
void setSpecificAppExtensionsByLocation(Location location, List<Extension> extensions); | ||
} |
10 changes: 10 additions & 0 deletions
10
api/src/main/java/org/openmrs/module/cfl/api/service/UserNotAuthorizedService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
api/src/main/java/org/openmrs/module/cfl/api/service/impl/CaptchaServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* <p> | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
|
||
package org.openmrs.module.cfl.api.service.impl; | ||
|
||
import org.openmrs.api.context.Context; | ||
import org.openmrs.module.cfl.CfldistributionGlobalParameterConstants; | ||
import org.openmrs.module.cfl.api.service.CaptchaService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.client.RestClientException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.net.URI; | ||
import java.util.regex.Pattern; | ||
|
||
public class CaptchaServiceImpl implements CaptchaService { | ||
|
||
private static final Pattern RESPONSE_PATTERN = Pattern.compile("[A-Za-z0-9_-]+"); | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(CaptchaServiceImpl.class); | ||
|
||
private static final String RECAPTCHA_URL_TEMPLATE = | ||
"https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s&remoteip=%s"; | ||
|
||
private RestTemplate restTemplate = new RestTemplate(); | ||
|
||
private ReCaptchaAttemptService reCaptchaAttemptService; | ||
|
||
public CaptchaServiceImpl() { | ||
this(new RestTemplate()); | ||
} | ||
|
||
CaptchaServiceImpl(RestTemplate restTemplate) { | ||
this.restTemplate = restTemplate; | ||
} | ||
|
||
@Override | ||
public void processResponse(HttpServletRequest request) { | ||
|
||
String response = request.getParameter("g-recaptcha-response"); | ||
|
||
securityCheck(response, request); | ||
final URI verifyUri = | ||
URI.create( | ||
String.format( | ||
RECAPTCHA_URL_TEMPLATE, getReCaptchaSecret(), response, getClientIP(request))); | ||
try { | ||
final GoogleResponse googleResponse = | ||
restTemplate.getForObject(verifyUri, GoogleResponse.class); | ||
LOGGER.debug("Google's response: {} ", googleResponse); | ||
|
||
if (!googleResponse.isSuccess()) { | ||
if (googleResponse.hasClientError()) { | ||
reCaptchaAttemptService.reCaptchaFailed(getClientIP(request)); | ||
} | ||
throw new ReCaptchaInvalidException("reCaptcha was not successfully validated"); | ||
} | ||
} catch (RestClientException rce) { | ||
throw new ReCaptchaInvalidException( | ||
"Login unavailable at this time. Please try again later.", rce); | ||
} | ||
reCaptchaAttemptService.reCaptchaSucceeded(getClientIP(request)); | ||
} | ||
|
||
private String getClientIP(HttpServletRequest request) { | ||
final String xfHeader = request.getHeader("X-Forwarded-For"); | ||
if (xfHeader == null) { | ||
return request.getRemoteAddr(); | ||
} | ||
return xfHeader.split(",")[0]; | ||
} | ||
|
||
private String getReCaptchaSecret() { | ||
return Context.getAdministrationService() | ||
.getGlobalProperty(CfldistributionGlobalParameterConstants.GOOGLE_RECAPTCHA_SECRET_KEY); | ||
} | ||
|
||
private boolean responseSanityCheck(String response) { | ||
return StringUtils.hasLength(response) && RESPONSE_PATTERN.matcher(response).matches(); | ||
} | ||
|
||
public void setReCaptchaAttemptService(ReCaptchaAttemptService reCaptchaAttemptService) { | ||
this.reCaptchaAttemptService = reCaptchaAttemptService; | ||
} | ||
|
||
private void securityCheck(String response, HttpServletRequest request) { | ||
if (!responseSanityCheck(response)) { | ||
throw new ReCaptchaInvalidException("Response contains invalid characters"); | ||
} | ||
if (reCaptchaAttemptService.isBlocked(getClientIP(request))) { | ||
throw new ReCaptchaInvalidException("Client exceeded maximum number of failed attempts"); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
api/src/main/java/org/openmrs/module/cfl/api/service/impl/CustomUserAppServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.openmrs.module.cfl.api.service.impl; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.openmrs.Location; | ||
import org.openmrs.LocationAttribute; | ||
import org.openmrs.api.context.Context; | ||
import org.openmrs.module.appframework.domain.AppDescriptor; | ||
import org.openmrs.module.appframework.domain.Extension; | ||
import org.openmrs.module.appframework.service.AppFrameworkService; | ||
import org.openmrs.module.cfl.CfldistributionConstants; | ||
import org.openmrs.module.cfl.api.service.CustomUserAppService; | ||
|
||
public class CustomUserAppServiceImpl implements CustomUserAppService { | ||
|
||
@Override | ||
public void setSpecificAppExtensionsByLocation(Location location, List<Extension> extensions) { | ||
if (location != null) { | ||
String projectName = getProjectNameByLocation(location); | ||
if (StringUtils.isNotBlank(projectName)) { | ||
replaceExtensionsIfRequired(extensions, projectName); | ||
} | ||
} | ||
} | ||
|
||
private void replaceExtensionsIfRequired(List<Extension> extensions, String projectName) { | ||
List<Extension> allAvailableProjectExtensions = getAllProjectSpecificExtensions(projectName); | ||
for (int i = 0; i < extensions.size(); i++) { | ||
String projectSpecificExtensionName = getProjectExtensionName(extensions.get(i), projectName); | ||
Optional<Extension> projectSpecificExtension = findSpecificExtension( | ||
allAvailableProjectExtensions, projectSpecificExtensionName); | ||
if (projectSpecificExtension.isPresent()) { | ||
extensions.set(i, projectSpecificExtension.get()); | ||
} | ||
} | ||
} | ||
|
||
private String getProjectNameByLocation(Location location) { | ||
String projectName = null; | ||
Optional<LocationAttribute> locationProjectAttribute = location.getActiveAttributes().stream() | ||
.filter(attribute -> StringUtils.equalsIgnoreCase(attribute.getAttributeType().getName(), | ||
CfldistributionConstants.PROJECT_LOCATION_ATTRIBUTE_TYPE_NAME)) | ||
.findFirst(); | ||
|
||
if (locationProjectAttribute.isPresent()) { | ||
projectName = locationProjectAttribute.get().getValueReference(); | ||
} | ||
|
||
return projectName; | ||
} | ||
|
||
private List<Extension> getAllProjectSpecificExtensions(String projectName) { | ||
return Context.getService(AppFrameworkService.class).getAllApps().stream() | ||
.filter(app -> StringUtils.endsWith(app.getId(), projectName)) | ||
.flatMap(app -> app.getExtensions().stream()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private String getProjectExtensionName(Extension extension, String projectName) { | ||
String extensionName = null; | ||
AppDescriptor extDescriptor = extension.getBelongsTo(); | ||
if (extDescriptor != null) { | ||
extensionName = extDescriptor.getId().concat(".").concat(projectName); | ||
} | ||
|
||
return extensionName; | ||
} | ||
|
||
private Optional<Extension> findSpecificExtension(List<Extension> list, String appId) { | ||
return list.stream() | ||
.filter(ext -> ext.getBelongsTo() != null) | ||
.filter(ext -> StringUtils.equals(ext.getBelongsTo().getId(), appId)) | ||
.findFirst(); | ||
} | ||
} |
Oops, something went wrong.