package com.cm.common.plugin.oauth.controller.apis.auth;

import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @ClassName: AuthController
 * @Description: 授权认证
 * @Author: WangGeng
 * @Date: 2019/3/18 2:47 PM
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "认证授权")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/auth")
public class AuthController extends AbstractController {

    @Autowired
    @Qualifier("oauth2ClientContext")
    private OAuth2ClientContext oAuth2ClientContext;

    @ApiOperation(value = "获取当前access_token", notes = "获取当前access_token接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getaccesstoken")
    public SuccessResultData<String> getAccessToken() {
        OAuth2AccessToken oAuth2AccessToken = oAuth2ClientContext.getAccessToken();
        return new SuccessResultData<>(oAuth2AccessToken.getValue());
    }

    @ApiOperation(value = "获取当前refresh_token", notes = "获取当前refresh_token接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getrefreshtoken")
    public SuccessResultData<String> getRefreshToken() {
        OAuth2AccessToken oAuth2AccessToken = oAuth2ClientContext.getAccessToken();
        return new SuccessResultData<>(oAuth2AccessToken.getRefreshToken().getValue());
    }

}
