package com.izouma.awesomeadmin.shiro;

import com.izouma.awesomeadmin.model.UserInfo;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;

public class WeiXinCodeRealm extends BaseRealm {

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UserInfo userInfo = null;
        WeiXinCodeToken weiXinCodeToken = (WeiXinCodeToken) token;

        userInfo = userInfoService.loginWeiXin(weiXinCodeToken.getOpenId(), weiXinCodeToken.getNickname(), weiXinCodeToken.getSex(), weiXinCodeToken.getHeadimgurl());
        if (userInfo == null) {
            throw new AuthenticationException("登录失败");
        }
        return new SimpleAuthenticationInfo(userInfo, token.getCredentials(), this.getName());


    }
}
