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 PhoneCodeRealm extends BaseRealm {

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