package com.m2pool.common.security.utils;


import com.m2pool.common.core.constant.OpenApiKeyConstants;
import com.m2pool.common.core.constant.TokenConstants;
import com.m2pool.common.core.context.OpenApiContextHolder;
import com.m2pool.common.core.utils.ServletUtils;
import com.m2pool.common.core.utils.StringUtils;
import com.m2pool.system.api.model.OpenApiKeyInfo;


import javax.servlet.http.HttpServletRequest;

/**
 * @Description 权限获取工具类
 * @Date 2024/6/11 16:58
 * @Author dy
 */
public class OpenApiUtils {


    /**
     * 获取api用户
     */
    public static String getApiUser()
    {
        return OpenApiContextHolder.getApiUser();
    }

    /**
     * 获取api绑定的ip
     */
    public static String getApiIp()
    {
        return OpenApiContextHolder.getApiIp();
    }

    /**
     * 获取api key
     */
    public static String getKey()
    {
        return OpenApiContextHolder.getApiKey();
    }


    /**
     * 获取apiKey对应的apiKey详细信息
     */
    public static OpenApiKeyInfo getOpenApiKeyInfo()
    {
        OpenApiKeyInfo info = OpenApiContextHolder.get(OpenApiKeyConstants.API_KEY_INFO, OpenApiKeyInfo.class);
        return info;
    }
    /**
     * 获取请求token
     */
    public static String getApiKey()
    {
        return getApiKey(ServletUtils.getRequest());
    }

    /**
     * 根据request获取请求api-key
     */
    public static String getApiKey(HttpServletRequest request)
    {
        // 从header获取token标识
        String token = request.getHeader(TokenConstants.API_KEY);
        return token;
    }

    /**
     * 是否为管理员
     *
     * @param user 用户邮箱
     * @return 结果
     */
    public static boolean isAdmin(String user)
    {
        return StringUtils.isNotBlank(user) && "".equals(user);
    }



}
