/**
 * Copyright (c) 2022 cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lutao.cloud.auth.handler;

import com.lutao.cloud.admin.api.dto.SysLogDTO;
import com.lutao.cloud.admin.api.feign.RemoteLogService;
import com.lutao.cloud.admin.api.feign.RemoteUserService;
import com.lutao.cloud.common.core.constant.SecurityConstants;
import com.lutao.cloud.common.core.util.KeyStrResolver;
import com.lutao.cloud.common.log.util.SysLogUtils;
import com.lutao.cloud.common.security.handler.AuthenticationLogoutHandler;
import com.lutao.cloud.common.security.sso.SSOAuthenticationToken;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 退出事件处理
 *
 * @date 2021/06/22
 */
@Slf4j
@Component
@AllArgsConstructor
public class CloudAuthenticationLogoutEventHandler implements AuthenticationLogoutHandler{

    private final RemoteLogService logService;

    private final KeyStrResolver tenantKeyStrResolver;

    private final RemoteUserService userService;

    /**
     * 处理登录成功方法
     * <p>
     * 获取到登录的authentication 对象
     *
     * @param authentication 登录对象
     * @param request        请求
     * @param response       返回
     * 1.获取clientId
     * 保存日志
     */
    @Async
    @Override
    public void handle(Authentication authentication, HttpServletRequest request, HttpServletResponse response){
        String username = authentication.getName();
        SysLogDTO sysLog = SysLogUtils.getSysLog(request, username);
        sysLog.setTitle(username + "用户退出");
        sysLog.setParams(username);

        // 获取clientId 信息
        if(authentication instanceof SSOAuthenticationToken) {
            sysLog.setServiceId("casst");
        } else {
            OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication;
            sysLog.setServiceId(auth2Authentication.getOAuth2Request().getClientId());
        }
        sysLog.setTenantId(Integer.parseInt(tenantKeyStrResolver.key()));
        // 保存退出的token
        String token = request.getHeader(HttpHeaders.AUTHORIZATION);
        sysLog.setParams(token);

        logService.saveLog(sysLog, SecurityConstants.FROM_IN);
        log.info("用户：{} 退出成功, token:{}  已注销", username, token);
    }

}
