package com.fdkankan.common.realm;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fdkankan.common.exception.JwtAuthenticationException;
import com.fdkankan.common.jwt.JwtToken;
import com.fdkankan.common.util.JwtUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserJwtRealm extends AuthorizingRealm {
    private static Logger log = LoggerFactory.getLogger("programLog");

//    @Autowired
//    private UserFeignClient userService;
    @Autowired
    private ObjectMapper mapper;

    /**
     * 必须重写此方法，不然Shiro会报错
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }
    /**
     * 只有当需要检测用户权限的时候才会调用此方法，例如checkRole,checkPermission之类的
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        String username = (String)principals.getPrimaryPrincipal();
//        List list = principals.asList();
//        Result result = userService.findByUserName(username);
//        if (result.getCode() == Result.CODE_FAILURE){
//            return authorizationInfo;
//        }
//        SSOUser dbUser = mapper.convertValue(result.getData(), SSOUser.class);
//        authorizationInfo.setRoles(dbUser.getRoleSet());
//        authorizationInfo.setStringPermissions(dbUser.getPermissionSet());
        return authorizationInfo;
    }
    /**
     * 默认使用此方法进行用户名正确与否验证，错误抛出异常即可。
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) throws AuthenticationException {
        log.info("Step 2: User进行用户名正确与否验证");
        String token = (String) auth.getCredentials();
        if (StrUtil.isEmpty(token)){
            throw new JwtAuthenticationException(3004, "无token，请重新登录");
        }
        // 解密获得username，用于和数据库进行对比
        String username = JwtUtil.getUsername(token);
        if (username == null) {
            throw new JwtAuthenticationException(3004, "访问异常！");
        }
        if (!JwtUtil.isVerify(token, username)) {
            throw new JwtAuthenticationException(3004, "非法访问！");
        }
        // TODO: 2021/12/21
//        SSOUser ssoUser = SSOLoginHelper.loginCheck(token);
//        if (ssoUser == null){
//            throw new JwtAuthenticationException(3004, "用户未登录");
//        }
//
//        // refresh
        // TODO: 2021/12/21
//        JedisUtil.expire(token, 21600);
        return new SimpleAuthenticationInfo(username, token, "jwt_realm");
    }
}
