package com.inkasso.auth.service.impl;

import com.inkasso.adapter.bean.AbstractUser;
import com.inkasso.adapter.constance.AuthConstance;
import com.inkasso.adapter.util.TokenAndSessionCacheUtil;
import com.inkasso.auth.annotation.AuthenticationType;
import com.inkasso.auth.service.AbstractAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 张超
 * @date 2021/11/3
 */
@AuthenticationType(type="token")
public class TokenCheckAuthentication implements AbstractAuthentication {

  @Autowired
  private TokenAndSessionCacheUtil tokenAndSessionCacheUtil;

  @Override
  public AbstractUser auth(HttpServletRequest request) {
    //获取request 中的 token
    String access = request.getHeader(AuthConstance.X_Token);
    if(StringUtils.hasLength(access)){
      AbstractUser loginUserByToken = tokenAndSessionCacheUtil.getLoginUserByToken(access);
      return loginUserByToken;
    }
    return null;
  }
}
