|
@@ -1,14 +1,108 @@
|
|
|
package api
|
|
|
|
|
|
-import "github.com/gin-gonic/gin"
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "io"
|
|
|
+ "net/http"
|
|
|
+
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+)
|
|
|
+
|
|
|
+const CASDOOEN_HOST = "https://auth.3dqueen.cloud"
|
|
|
+
|
|
|
+// forwardRequestWithJWT forwards the request to target URL with JWT token and original request data
|
|
|
+func forwardRequestWithJWT(c *gin.Context, targetURL string) (interface{}, error) {
|
|
|
+ jwtToken := c.GetHeader("Authorization")
|
|
|
+
|
|
|
+ // Create new request
|
|
|
+ var req *http.Request
|
|
|
+ var err error
|
|
|
+
|
|
|
+ // Read and reuse the original body if it's a POST request
|
|
|
+ var bodyBytes []byte
|
|
|
+ if c.Request.Body != nil {
|
|
|
+ bodyBytes, err = io.ReadAll(c.Request.Body)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("read request body error: %v", err)
|
|
|
+ }
|
|
|
+ // Restore the body for later use
|
|
|
+ c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
|
|
+ }
|
|
|
+
|
|
|
+ // Create new request with original method and body
|
|
|
+ req, err = http.NewRequest(c.Request.Method, targetURL, bytes.NewBuffer(bodyBytes))
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("create request error: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Copy original headers
|
|
|
+ for k, v := range c.Request.Header {
|
|
|
+ req.Header[k] = v
|
|
|
+ }
|
|
|
+
|
|
|
+ // Set JWT token
|
|
|
+
|
|
|
+ req.Header.Set("Authorization", "Bearer "+jwtToken)
|
|
|
+
|
|
|
+ // Set content type if it's a POST request
|
|
|
+ if c.Request.Method == "POST" {
|
|
|
+ req.Header.Set("Content-Type", "application/json")
|
|
|
+ }
|
|
|
+
|
|
|
+ // Send request
|
|
|
+ client := &http.Client{}
|
|
|
+ resp, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("send request error: %v", err)
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ // Read response
|
|
|
+ respBody, err := io.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("read response error: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Parse response JSON
|
|
|
+ var result interface{}
|
|
|
+ if err := json.Unmarshal(respBody, &result); err != nil {
|
|
|
+ return nil, fmt.Errorf("parse response error: %v", err)
|
|
|
+ }
|
|
|
+ return result, nil
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
// 转发请求到https://auth.3dqueen.cloud/api/get-account
|
|
|
// 添加jwt认证头
|
|
|
func GetAccount(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
- c.GetHeader("a")
|
|
|
+ return forwardRequestWithJWT(c, CASDOOEN_HOST+"/api/get-account")
|
|
|
+}
|
|
|
|
|
|
- c.JSON(200, "")
|
|
|
+// GetUsers forwards request to https://auth.3dqueen.cloud/api/get-users
|
|
|
+func GetUsers(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ url := fmt.Sprintf(CASDOOEN_HOST+"/api/get-users?%s", c.Request.URL.RawQuery)
|
|
|
+ return forwardRequestWithJWT(c, url)
|
|
|
+}
|
|
|
|
|
|
- return nil, nil
|
|
|
+// AddUser forwards request to https://auth.3dqueen.cloud/api/add-user
|
|
|
+func AddUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ return forwardRequestWithJWT(c, CASDOOEN_HOST+"/api/add-user")
|
|
|
+}
|
|
|
+
|
|
|
+// UpadteUser forwards request to https://auth.3dqueen.cloud/api/update-user
|
|
|
+func UpadteUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ url := fmt.Sprintf(CASDOOEN_HOST+"/api/update-user?%s", c.Request.URL.RawQuery)
|
|
|
+ return forwardRequestWithJWT(c, url)
|
|
|
+}
|
|
|
+
|
|
|
+// DeleteUser forwards request to https://auth.3dqueen.cloud/api/delete-user
|
|
|
+func DeleteUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ return forwardRequestWithJWT(c, CASDOOEN_HOST+"/api/delete-user")
|
|
|
+}
|
|
|
|
|
|
+// SetPassword forwards request to https://auth.3dqueen.cloud/api/set-password
|
|
|
+func SetPassword(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ return forwardRequestWithJWT(c, CASDOOEN_HOST+"/api/set-password")
|
|
|
}
|