package com.tashow.cloud.sso.controller;
import cn.dev33.satoken.sso.processor.SaSsoServerProcessor;
import cn.dev33.satoken.sso.template.SaSsoUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.tashow.cloud.sso.model.SystemUser;
import com.tashow.cloud.sso.service.SystemUserService;
import jakarta.annotation.security.PermitAll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * Sa-Token-SSO Server端 Controller
 */
@RestController
public class SsoServerController {
    @Autowired
    private SystemUserService systemUserService;
    /**
     * SSO-Server端：处理所有SSO相关请求 (下面的章节我们会详细列出开放的接口)
     */
    @RequestMapping("sso/*")
    @PermitAll
    public Object ssoRequest() {
        return SaSsoServerProcessor.instance.dister();
    }

    // 测试登录，浏览器访问： http://localhost:48083/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    @PermitAll
    public String doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            return "登录成功";
        }
        return "登录失败";
    }
    // 返回SSO认证中心登录地址
    @PermitAll
    @RequestMapping("sso/getSsoAuthUrl")
    public SaResult getSsoAuthUrl(String clientLoginUrl) {
        String serverAuthUrl = SaSsoUtil.buildServerAuthUrl(clientLoginUrl, "");
        return SaResult.data(serverAuthUrl);
    }


    // 查询登录状态，浏览器访问： http://localhost:48083/isLogin
    @RequestMapping("isLogin")
    @PermitAll
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }
}
