package com.qxgmat.util.shiro;

import com.qxgmat.data.dao.entity.User;
import com.qxgmat.service.UsersService;
import org.apache.commons.lang.time.FastDateFormat;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by GaoJie on 2017/11/3.
 */

public class OauthRealm extends AuthorizingRealm {
    private static final FastDateFormat dateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private UsersService usersService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String code = token.getUsername();
        String platform = new String(token.getPassword());

        User user = usersService.Oauth(null, code, platform);
        return new SimpleAuthenticationInfo(user, platform, getName());
    }
}
