|
|
@@ -257,50 +257,21 @@ public class ClaudeCodeController {
|
|
|
* OAuth授权端点
|
|
|
*/
|
|
|
@GetMapping("/authorize")
|
|
|
- public void authorize(@RequestParam(name = "client_id") String clientId,
|
|
|
- @RequestParam(name = "redirect_uri") String redirectUri,
|
|
|
- @RequestParam String state,
|
|
|
- HttpServletResponse response) throws IOException {
|
|
|
-
|
|
|
- try {
|
|
|
- // 验证参数
|
|
|
- if (clientId == null || clientId.isEmpty() ||
|
|
|
- redirectUri == null || redirectUri.isEmpty() ||
|
|
|
- state == null || state.isEmpty()) {
|
|
|
- response.sendError(400, "Missing required parameters");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 验证客户端ID
|
|
|
- if (!isValidClientId(clientId)) {
|
|
|
- response.sendError(400, "Invalid client_id");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 检查用户登录状态
|
|
|
- if (!StpUserUtil.isLogin()) {
|
|
|
- // 重定向到登录页面
|
|
|
- String loginUrl = "/login?client_id=" + clientId +
|
|
|
- "&redirect_uri=" + URLEncoder.encode(redirectUri, StandardCharsets.UTF_8) +
|
|
|
- "&state=" + state;
|
|
|
- response.sendRedirect(loginUrl);
|
|
|
- return;
|
|
|
- }
|
|
|
- // 用户已登录,生成token
|
|
|
+ public Result<String> authorize(@RequestParam(name = "client_id") String clientId,
|
|
|
+ @RequestParam String state) throws Exception {
|
|
|
+ // 验证参数
|
|
|
+ if (clientId == null || clientId.isEmpty() ||
|
|
|
+ state == null || state.isEmpty()) {
|
|
|
+ throw BusinessRuntimeException.getInstance("缺少必填参数");
|
|
|
+ }
|
|
|
+ if (!StpUserUtil.isLogin()) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请先登录");
|
|
|
+ } else {
|
|
|
Long userId = StpUserUtil.getLoginIdAsLong();
|
|
|
String token = oAuthClaudeCodeService.generateAccessToken(userId, clientId);
|
|
|
- if (token == null) {
|
|
|
- throw BusinessRuntimeException.getInstance("您还没有claudecode账号");
|
|
|
- }
|
|
|
- // 重定向回客户端
|
|
|
- String callbackUrl = redirectUri + "?token=" + token + "&state=" + state;
|
|
|
- response.sendRedirect(callbackUrl);
|
|
|
-
|
|
|
log.info("OAuth授权成功: userId={}, clientId={}, token={}", userId, clientId, token.substring(0, 8) + "...");
|
|
|
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("OAuth授权失败", e);
|
|
|
- response.sendError(500, "Authorization failed");
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(token);
|
|
|
}
|
|
|
}
|
|
|
|