001/*
002 *  Copyright 2023 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016
017package org.ametys.plugins.captchetat.captcha;
018
019import java.util.Arrays;
020import java.util.List;
021import java.util.regex.Pattern;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.commons.lang3.StringUtils;
027
028import org.ametys.core.captcha.AbstractCaptcha;
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * Captcha implementation with images
033 */
034public class CaptchEtat extends AbstractCaptcha implements Serviceable
035{
036    private CaptchEtatHelper _captchEtatHelper;
037
038    public void service(ServiceManager manager) throws ServiceException
039    {
040        _captchEtatHelper = (CaptchEtatHelper) manager.lookup(CaptchEtatHelper.ROLE);
041    }
042
043    @Override
044    public boolean checkAndInvalidateCaptcha(String key, String value)
045    {
046        if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value))
047        {
048            return false;
049        }
050        return _captchEtatHelper.checkAndInvalidateCaptcha(key, value);
051    }
052
053    public I18nizableText getLabel()
054    {
055        return new I18nizableText("plugin.captchetat", "PLUGINS_CAPTCHETAT_CONFIG_TYPE_CAPTCHETAT");
056    }
057
058    @Override
059    public I18nizableText getLoginFailedBecauseCaptchaFailedLabel()
060    {
061        return new I18nizableText("plugin.captchetat", "PLUGINS_CAPTCHETAT_LOGIN_SCREEN_FORM_FAILED_WITH_CAPTCHA_CaptchEtat");
062    }
063
064    @Override
065    public I18nizableText getLoginFailedBecauseTooManyAttemptLabel()
066    {
067        return new I18nizableText("plugin.captchetat", "PLUGINS_CAPTCHETAT_LOGIN_SCREEN_FORM_FAILED_MANY_TIME_CaptchEtat");
068    }
069
070    @Override
071    public List<Pattern> getUsedUrlPatterns()
072    {
073        return Arrays.asList(new Pattern[] {Pattern.compile("^plugins/captchetat/simple-captcha-endpoint")});
074    }
075}