package com.m2pool.common.security.auth;

import com.m2pool.common.security.annotation.OpenApiPermissions;
import com.m2pool.common.security.annotation.RequiresPermissions;
import com.m2pool.system.api.model.OpenApiKeyInfo;

/**
 * @Description api 验证工具类
 * @Date 2024/6/13 19:17
 * @Author dy
 */
public class ApiUtil {

    /**
     * 底层的oaReq对象
     */
    public static OpenApiRequest oaReq = new OpenApiRequest();

    /**
     * 检查当前apikey是否有效
     */
    public static void checkApiKey(){
        oaReq.checkApiKey();
    }

    /**
     * 获取当前api-key信息
     * @param apiKey
     * @return
     */
    public static OpenApiKeyInfo getApiKeyInfo(String apiKey){return oaReq.getOpenApiKeyInfo(apiKey);}

    /**
     * 验证当前api-key有效期
     * @param openApiKeyInfo
     */
    public static void verifyApiKeyExpire(OpenApiKeyInfo openApiKeyInfo){
        oaReq.verifyApiKeyExpire(openApiKeyInfo);
    }


    /**
     * 判断当前账号是否含有指定权限
     *
     * @param permission 权限码
     * @return 是否含有指定权限
     */
    public static boolean hasPermission(String permission)
    {
        return oaReq.hasPermission(permission);
    }

    /**
     * 当前账号是否含有指定权限, 如果验证未通过，则抛出异常: NotPermissionException
     *
     * @param permission 权限码
     */
    public static void checkPermission(String permission)
    {
        oaReq.checkPermission(permission);
    }

    /**
     * 根据注解传入参数鉴权, 如果验证未通过，则抛出异常: NotPermissionException
     *
     * @param openApiPermissions 权限注解
     */
    public static void checkPermission(OpenApiPermissions openApiPermissions)
    {
        oaReq. checkPermission(openApiPermissions);
    }

    /**
     * 当前账号是否含有指定权限 [指定多个，必须全部验证通过]
     *
     * @param permissions 权限码数组
     */
    public static void checkPermissionAnd(String... permissions)
    {
        oaReq.checkPermissionAnd(permissions);
    }

    /**
     * 当前账号是否含有指定权限 [指定多个，只要其一验证通过即可]
     *
     * @param permissions 权限码数组
     */
    public static void checkPermissionOr(String... permissions)
    {
        oaReq.checkPermissionOr(permissions);
    }



    /**
     * 会话注销
     */
    public static void logout(){oaReq.logout();}

    /**
     * 根据token注销会话
     * @param apiKey
     */
    public static void logoutByApiKey(String apiKey){oaReq.logoutByApiKey(apiKey);}

}
