package com.izouma.awesomeadmin.shiro;

import com.izouma.awesomeadmin.model.UserInfo;
import com.izouma.awesomeadmin.service.impl.UserInfoServiceImpl;
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 WechatRealm extends BaseRealm {

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UserInfo userInfo = null;
        WechatToken wechatToken = (WechatToken) token;
        try {
            userInfo = userInfoService.loginWechat(wechatToken.getCode());
            return new SimpleAuthenticationInfo(userInfo, token.getCredentials(), this.getName());
        } catch (UserInfoServiceImpl.LoginException e) {
            throw new AuthenticationException(e.getMessage());
        }
    }
}
