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    /** The CaptchEtat helper */
037    protected CaptchEtatHelper _captchEtatHelper;
038
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        _captchEtatHelper = (CaptchEtatHelper) manager.lookup(CaptchEtatHelper.ROLE);
042    }
043
044    @Override
045    public boolean checkAndInvalidateCaptcha(String key, String value)
046    {
047        if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value))
048        {
049            return false;
050        }
051        return _captchEtatHelper.checkAndInvalidateCaptcha(key, value);
052    }
053
054    public I18nizableText getLabel()
055    {
056        return new I18nizableText("plugin.captchetat", "PLUGINS_CAPTCHETAT_CONFIG_TYPE_CAPTCHETAT");
057    }
058
059    @Override
060    public I18nizableText getLoginFailedBecauseCaptchaFailedLabel()
061    {
062        return new I18nizableText("plugin.captchetat", "PLUGINS_CAPTCHETAT_LOGIN_SCREEN_FORM_FAILED_WITH_CAPTCHA_CaptchEtat");
063    }
064
065    @Override
066    public I18nizableText getLoginFailedBecauseTooManyAttemptLabel()
067    {
068        return new I18nizableText("plugin.captchetat", "PLUGINS_CAPTCHETAT_LOGIN_SCREEN_FORM_FAILED_MANY_TIME_CaptchEtat");
069    }
070
071    @Override
072    public List<Pattern> getUsedUrlPatterns()
073    {
074        return Arrays.asList(new Pattern[] {
075                Pattern.compile("^plugins/captchetat/simple-captcha-endpoint"),
076                Pattern.compile("^plugins/captchetat/captcha.html"),
077            });
078    }
079}