Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FORMS-14468 | Updating Captcha interface and it's implementation #1281

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
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 @@ -71,12 +71,18 @@ private FormConstants() {
/** The resource type for check box group v1 */
public static final String RT_FD_FORM_CHECKBOX_GROUP_V1 = RT_FD_FORM_PREFIX + "checkboxgroup/v1/checkboxgroup";

/** The resource type for reCaptcha v1 */
/** The resource type for hCaptcha v1 */
public static final String RT_FD_FORM_HCAPTCHA_V1 = RT_FD_FORM_PREFIX + "hcaptcha/v1/hcaptcha";

/** The resource type for hCaptcha v2 */
public static final String RT_FD_FORM_HCAPTCHA_V2 = RT_FD_FORM_PREFIX + "hcaptcha/v2/hcaptcha";

/** The resource type for reCaptcha v1 */
public static final String RT_FD_FORM_RECAPTCHA_V1 = RT_FD_FORM_PREFIX + "recaptcha/v1/recaptcha";

/** The resource type for reCaptcha v2 */
public static final String RT_FD_FORM_RECAPTCHA_V2 = RT_FD_FORM_PREFIX + "recaptcha/v2/recaptcha";

/** The resource type for radio button v1 */
public static final String RT_FD_FORM_RADIO_BUTTON_V1 = RT_FD_FORM_PREFIX + "radiobutton/v1/radiobutton";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,20 @@
public class HCaptchaImpl extends AbstractCaptchaImpl implements HCaptcha {

private static final Logger LOGGER = LoggerFactory.getLogger(HCaptchaImpl.class);
protected static final String HCAPTCHA_CONFIG_FETCH_ERROR = "[AF] [CAPTCHA] [HCAPTCHA] Error while fetching cloud configuration, upgrade to latest release to use hCaptcha.";

@Inject
private ResourceResolver resourceResolver;
protected ResourceResolver resourceResolver;

private Resource resource;
protected Resource resource;

@Reference
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private HCaptchaConfiguration hCaptchaConfiguration;
protected HCaptchaConfiguration hCaptchaConfiguration;

@OSGiService
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private CloudConfigurationProvider cloudConfigurationProvider;
protected CloudConfigurationProvider cloudConfigurationProvider;

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
@JsonIgnore
Expand All @@ -76,18 +77,13 @@ public class HCaptchaImpl extends AbstractCaptchaImpl implements HCaptcha {
@Named("size")
protected String size;

private static final String SITE_KEY = "siteKey";
private static final String URI = "uri";
private static final String SIZE = "size";
private static final String THEME = "theme";
private static final String TYPE = "type";

@Override
public String getCloudServicePath() {
return cloudServicePath;
}

@Override
@JsonIgnore
public String getProvider() {
return "hcaptcha";
}
Expand All @@ -99,7 +95,7 @@ public Map<String, Object> getCaptchaProperties() throws GuideException {
String siteKey = null, uri = null;
resource = resourceResolver.getResource(this.getPath());
if (cloudConfigurationProvider == null) {
LOGGER.info("[AF] [Captcha] [HCAPTCHA] Error while fetching cloud configuration, upgrade to latest release to use hCaptcha.");
LOGGER.info(HCAPTCHA_CONFIG_FETCH_ERROR);
}
try {
if (resource != null && cloudConfigurationProvider != null) {
Expand All @@ -110,13 +106,13 @@ public Map<String, Object> getCaptchaProperties() throws GuideException {
}
}
} catch (GuideException e) {
LOGGER.error("[AF] [Captcha] [HCAPTCHA] Error while fetching cloud configuration, upgrade to latest release to use hCaptcha.");
LOGGER.error(HCAPTCHA_CONFIG_FETCH_ERROR, e);
}
customCaptchaProperties.put(SITE_KEY, siteKey);
customCaptchaProperties.put(URI, uri);
customCaptchaProperties.put(SIZE, this.size);
customCaptchaProperties.put(THEME, "light");
customCaptchaProperties.put(TYPE, "image");
customCaptchaProperties.put(CAPTCHA_SITE_KEY, siteKey);
customCaptchaProperties.put(CAPTCHA_URI, uri);
customCaptchaProperties.put(CAPTCHA_SIZE, this.size);
customCaptchaProperties.put(CAPTCHA_THEME, CAPTCHA_THEME_LIGHT);
customCaptchaProperties.put(CAPTCHA_TYPE, CAPTCHA_TYPE_IMAGE);

return customCaptchaProperties;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.adobe.aemds.guide.model.ReCaptchaConfigurationModel;
import com.adobe.aemds.guide.service.CloudConfigurationProvider;
import com.adobe.aemds.guide.service.GuideException;
import com.adobe.aemds.guide.utils.GuideConstants;
import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.adobe.cq.forms.core.components.internal.form.FormConstants;
Expand All @@ -51,17 +52,17 @@
public class RecaptchaImpl extends AbstractCaptchaImpl implements Captcha {

@Inject
private ResourceResolver resourceResolver;
protected ResourceResolver resourceResolver;

private Resource resource;
protected Resource resource;

@Reference
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private ReCaptchaConfigurationModel reCaptchaConfiguration;
protected ReCaptchaConfigurationModel reCaptchaConfiguration;

@OSGiService
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private CloudConfigurationProvider cloudConfigurationProvider;
protected CloudConfigurationProvider cloudConfigurationProvider;

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
@JsonIgnore
Expand All @@ -77,13 +78,8 @@ public class RecaptchaImpl extends AbstractCaptchaImpl implements Captcha {
public static final String RECAPTCHA_DEFAULT_URL = RECAPTCHA_DEFAULT_DOMAIN + "recaptcha/api.js";
public static final String RECAPTCHA_ENTERPRISE_DEFAULT_URL = RECAPTCHA_DEFAULT_DOMAIN + "recaptcha/enterprise.js";

private static final String RECAPTCHA_SITE_KEY = "siteKey";
private static final String RECAPTCHA_URI = "uri";
private static final String RECAPTCHA_SIZE = "size";
private static final String RECAPTCHA_THEME = "theme";
private static final String RECAPTCHA_TYPE = "type";
private static final String RECAPTCHA_VERSION = "version";
private static final String RECAPTCHA_KEYTYPE = "keyType";
public static final String RECAPTCHA_VERSION = "version";
public static final String RECAPTCHA_KEYTYPE = "keyType";

@Override
@JsonIgnore
Expand All @@ -97,6 +93,7 @@ public String getSize() {
}

@Override
@JsonIgnore
public String getProvider() {
return "recaptcha";
}
Expand All @@ -118,15 +115,15 @@ public Map<String, Object> getCaptchaProperties() throws GuideException {
keyType = reCaptchaConfiguration.keyType();
}
}
customCaptchaProperties.put(RECAPTCHA_SITE_KEY, siteKey);
if (StringUtils.isNotEmpty(version) && version.equals("enterprise")) {
customCaptchaProperties.put(RECAPTCHA_URI, RECAPTCHA_ENTERPRISE_DEFAULT_URL);
customCaptchaProperties.put(CAPTCHA_SITE_KEY, siteKey);
if (StringUtils.isNotEmpty(version) && version.equals(GuideConstants.RECAPTCHA_ENTERPRISE_VERSION)) {
customCaptchaProperties.put(CAPTCHA_URI, RECAPTCHA_ENTERPRISE_DEFAULT_URL);
} else {
customCaptchaProperties.put(RECAPTCHA_URI, RECAPTCHA_DEFAULT_URL);
customCaptchaProperties.put(CAPTCHA_URI, RECAPTCHA_DEFAULT_URL);
}
customCaptchaProperties.put(RECAPTCHA_SIZE, getSize());
customCaptchaProperties.put(RECAPTCHA_THEME, "light");
customCaptchaProperties.put(RECAPTCHA_TYPE, "image");
customCaptchaProperties.put(CAPTCHA_SIZE, getSize());
customCaptchaProperties.put(CAPTCHA_THEME, CAPTCHA_THEME_LIGHT);
customCaptchaProperties.put(CAPTCHA_TYPE, CAPTCHA_TYPE_IMAGE);
customCaptchaProperties.put(RECAPTCHA_VERSION, version);
customCaptchaProperties.put(RECAPTCHA_KEYTYPE, keyType);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2024 Adobe
~
~ Licensed 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 com.adobe.cq.forms.core.components.internal.models.v2.form;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.aemds.guide.service.GuideException;
import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.adobe.cq.forms.core.components.internal.form.FormConstants;
import com.adobe.cq.forms.core.components.internal.models.v1.form.HCaptchaImpl;
import com.adobe.cq.forms.core.components.models.form.HCaptcha;
import com.fasterxml.jackson.annotation.JsonIgnore;

@Model(
adaptables = { SlingHttpServletRequest.class, Resource.class },
adapters = { HCaptcha.class,
ComponentExporter.class },
resourceType = { FormConstants.RT_FD_FORM_HCAPTCHA_V2 })
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class HCaptchaImplV2 extends HCaptchaImpl {
private static final Logger LOGGER = LoggerFactory.getLogger(HCaptchaImplV2.class);

private String captchaSiteKey;

@Override
@JsonIgnore(false)
public String getProvider() {
return super.getProvider();
}

/**
* Set the hCaptchaConfiguration, by fetching it from the cloud configurations.
* Also sets the captchaSiteKey.
*/
private void setHCaptchaConfiguration() {
if (cloudConfigurationProvider != null) {
try {
resource = resourceResolver.getResource(this.getPath());
hCaptchaConfiguration = cloudConfigurationProvider.getHCaptchaCloudConfiguration(resource);
if (hCaptchaConfiguration != null) {
captchaSiteKey = hCaptchaConfiguration.getSiteKey();
}
} catch (GuideException e) {
LOGGER.error(HCAPTCHA_CONFIG_FETCH_ERROR, e);
}
} else {
LOGGER.error(HCAPTCHA_CONFIG_FETCH_ERROR);
}
}

@PostConstruct
@Override
public Map<String, Object> getCaptchaProperties() throws GuideException {

Map<String, Object> customCaptchaProperties = new LinkedHashMap<>();
String uri = null;
if (hCaptchaConfiguration == null) {
setHCaptchaConfiguration();
}
if (hCaptchaConfiguration != null) {
uri = hCaptchaConfiguration.getClientSideJsUrl();
}
customCaptchaProperties.put(CAPTCHA_URI, uri);
customCaptchaProperties.put(CAPTCHA_SIZE, this.size);
customCaptchaProperties.put(CAPTCHA_THEME, CAPTCHA_THEME_LIGHT);
customCaptchaProperties.put(CAPTCHA_TYPE, CAPTCHA_TYPE_IMAGE);
return customCaptchaProperties;
}

@PostConstruct
@Override
public String getCaptchaDisplayMode() {
CaptchaDisplayMode captchaDisplayMode = CaptchaDisplayMode.VISIBLE;
if (CaptchaDisplayMode.INVISIBLE.getValue().equals(this.size)) {
captchaDisplayMode = CaptchaDisplayMode.INVISIBLE;
}
return captchaDisplayMode.getValue();
}

@PostConstruct
@Override
public String getCaptchaSiteKey() {
if (hCaptchaConfiguration == null) {
setHCaptchaConfiguration();
}
return this.captchaSiteKey;
}
}
Loading