2026-04-21 22:07:29 +08:00
|
|
|
package com.xuqm.im.controller;
|
|
|
|
|
|
|
|
|
|
import com.xuqm.common.model.ApiResponse;
|
2026-04-27 23:41:58 +08:00
|
|
|
import com.xuqm.common.security.AppRequestSignatureUtil;
|
2026-04-21 22:07:29 +08:00
|
|
|
import com.xuqm.im.service.ImAccountService;
|
|
|
|
|
import jakarta.validation.constraints.NotBlank;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
2026-04-27 23:41:58 +08:00
|
|
|
import org.springframework.web.bind.annotation.RequestHeader;
|
2026-04-21 22:07:29 +08:00
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/im/auth")
|
|
|
|
|
public class AuthController {
|
|
|
|
|
|
|
|
|
|
private final ImAccountService accountService;
|
|
|
|
|
|
|
|
|
|
public AuthController(ImAccountService accountService) {
|
|
|
|
|
this.accountService = accountService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/login")
|
|
|
|
|
public ResponseEntity<ApiResponse<Map<String, String>>> login(
|
|
|
|
|
@RequestParam @NotBlank String appId,
|
|
|
|
|
@RequestParam @NotBlank String userId,
|
|
|
|
|
@RequestParam(required = false) String nickname,
|
2026-04-27 23:41:58 +08:00
|
|
|
@RequestParam(required = false) String avatar,
|
|
|
|
|
@RequestHeader(value = "X-App-Timestamp", required = false) String timestamp,
|
|
|
|
|
@RequestHeader(value = "X-App-Nonce", required = false) String nonce,
|
|
|
|
|
@RequestHeader(value = "X-App-Signature", required = false) String signature) {
|
|
|
|
|
if (timestamp == null || nonce == null || signature == null) {
|
|
|
|
|
return ResponseEntity.status(401).body(ApiResponse.error(401, "Missing app signature"));
|
|
|
|
|
}
|
|
|
|
|
accountService.validateSignature(appId, userId, nickname, avatar, timestamp, nonce, signature);
|
2026-04-21 22:07:29 +08:00
|
|
|
String token = accountService.loginOrRegister(appId, userId, nickname, avatar);
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(Map.of("token", token)));
|
|
|
|
|
}
|
|
|
|
|
}
|