package com.jeesite.modules.sso.web;


import com.alibaba.fastjson.JSON;
import com.jeesite.common.config.Global;
import com.jeesite.common.web.BaseController;
import com.jeesite.common.web.http.HttpClientUtils;
import com.jeesite.modules.sso.entity.SsoApp;
import com.jeesite.modules.sso.service.SsoOauthService;
import com.jeesite.modules.sys.entity.User;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.Folder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.MalformedURLException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/sso/oauth2")
@RestController
@Slf4j
public class SsoOauthController extends BaseController {


    @Autowired
    private SsoOauthService ssoOauthService;
   /* *//***
     * 验证appkey和秘钥
     * @param appKey
     * @param appSecretkey
     * @param responseType  固定值填写code
     * @return
     *//*
    @RequestMapping("/authorize")
    public String authorize(String appKey, String appSecretkey, String responseType, String redirectUri){

        return ssoOauthService.authorize(appKey,appSecretkey,responseType,redirectUri);
    }*/

    /***
     * 生成token
     * @param appKey
     * @param appSecretkey
     * @param grantType  固定值authorization_code
     * @param code  验证码需要去redis中验证
     * @return
     */
    @RequestMapping("/token")
    public String token(String appKey, String appSecretkey, String grantType, String code,String redirectUri) throws MalformedURLException {
        return ssoOauthService.getToken(appKey,appSecretkey,grantType,code,redirectUri);
    }

    /***
     *  获取登录id
     * @return
     */
    @RequestMapping("/loginid")
    public String getUserIdByToken(HttpServletRequest request,HttpServletResponse response){
        return ssoOauthService.getUserIdByToken(request,response);
    }

    /**
     * 刷新token
     * @param appKey
     * @param appSecretkey
     * @return
     */
    @RequestMapping("/refreshToken")
    public String refreshToken(String appKey,String appSecretkey,HttpServletRequest request,HttpServletResponse response){
        return ssoOauthService.refreshToken(appKey,appSecretkey,request,response);
    }

    /***
     *  获取登录id
     * @return
     */
    @RequestMapping("/getUserInfoMsg")
    public String getUserInfo(HttpServletRequest request,HttpServletResponse response){
        return ssoOauthService.getUserInfoMsg(request,response);
    }



}
