package com.cesgroup.cesmonitor.web;

import java.util.concurrent.atomic.AtomicInteger;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;

/**
 * shiro 验证 密码的 策略 <br>
 * 包括加密方式，重试次数等
 *
 * @author zhang.erwei
 * @version 1.0.0 2017年5月24日
 */
public class RetryLimitHashedCredentialsMatcher extends HashedCredentialsMatcher {

    private Integer maxFailedTimes;

    private Long sessionInvalidtime;

    private Boolean loginFailedAutofreeze;

    // 集群中可能会导致出现验证多过5次的现象，因为AtomicInteger只能保证单节点并发
    private Cache<String, AtomicInteger> passwordRetryCache;

    // 登陆失败缓存信息
    private Cache<String, String> loginFailMessage;

    public RetryLimitHashedCredentialsMatcher(CacheManager cacheManager) {
        passwordRetryCache = cacheManager.getCache("passwordRetryCache");
        loginFailMessage = cacheManager.getCache("loginFailMessage");
    }

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        //        String username = (String) token.getPrincipal();
        //        String userId = "";
        // if(!"superadmin".equals(username)){
        // User user = identityService.login(username, password);
        // userId = user.getId();
        // if (loginFailedAutofreeze)
        // {
        // loginFailMessage.put("maxFailedTimes",
        // String.valueOf(maxFailedTimes));
        // // retry count + 1
        // AtomicInteger retryCount = passwordRetryCache.get(userId);
        // if (null == retryCount)
        // {
        // retryCount = new AtomicInteger(0);
        // passwordRetryCache.put(userId, retryCount);
        // }
        // if (retryCount.incrementAndGet() >= maxFailedTimes)
        // {
        // loginFailMessage.put(username,
        // "登陆重试次数过多，账号被冻结"+sessionInvalidtime/60000+"分钟");
        // throw new ExcessiveAttemptsException("登陆重试次数过多，账号被冻结30分钟");
        // }else{
        // int count = maxFailedTimes-retryCount.intValue();
        // loginFailMessage.put(username, "用户名与密码不匹配,剩余尝试次数"+count);
        // }
        //
        // }else{
        // loginFailMessage.put(username, "用户名与密码不匹配");
        // }
        // }
        //        boolean matches = super.doCredentialsMatch(token, info);
        //        if (matches) {
        //            // clear retry data
        //            AtomicInteger retryCount = passwordRetryCache.get(userId);
        //            if (!"superadmin".equals(username) && retryCount.incrementAndGet() < maxFailedTimes) {
        //                passwordRetryCache.remove(userId);
        //            }
        //        }
        //        return matches;
        return true;
    }

    public Integer getMaxFailedTimes() {
        return maxFailedTimes;
    }

    public void setMaxFailedTimes(Integer maxFailedTimes) {
        this.maxFailedTimes = maxFailedTimes;
    }

    public Boolean getLoginFailedAutofreeze() {
        return loginFailedAutofreeze;
    }

    public void setLoginFailedAutofreeze(Boolean loginFailedAutofreeze) {
        this.loginFailedAutofreeze = loginFailedAutofreeze;
    }

    public Long getSessionInvalidtime() {
        return sessionInvalidtime;
    }

    public void setSessionInvalidtime(Long sessionInvalidtime) {
        this.sessionInvalidtime = sessionInvalidtime;
    }

}
