add user routes
This commit is contained in:
70
routes/api/user/param_userid/auth.go
Normal file
70
routes/api/user/param_userid/auth.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package param_userid
|
||||
|
||||
import (
|
||||
"bdgrp_go/db"
|
||||
"bdgrp_go/pbutil"
|
||||
"bdgrp_go/routes"
|
||||
"bdgrp_go/utils"
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
path := routes.PathOf()
|
||||
routes.Register(func(app *fiber.App) {
|
||||
app.Put(path, Auth)
|
||||
fmt.Println("Loaded:", path)
|
||||
})
|
||||
}
|
||||
|
||||
func Auth(c *fiber.Ctx) error {
|
||||
server := os.Getenv("SERVER")
|
||||
userid := c.Params("userid")
|
||||
|
||||
decrypted, err := utils.Decrypt(c.Body())
|
||||
if err != nil {
|
||||
return c.Status(500).SendString("")
|
||||
}
|
||||
|
||||
decoded, err := pbutil.Decode("UserAuthRequest", decrypted)
|
||||
if err != nil {
|
||||
return c.Status(500).SendString("")
|
||||
}
|
||||
|
||||
hash := fmt.Sprintf("%x", sha1.Sum([]byte(userid+":")))
|
||||
|
||||
errData := map[string]any{
|
||||
"httpStatus": 403,
|
||||
"errorCode": "",
|
||||
"errorMessage": "signature invalid.",
|
||||
}
|
||||
|
||||
sendErr := func() error {
|
||||
encoded, _ := pbutil.Encode("ClientErrorResponse", errData)
|
||||
encrypted, _ := utils.Encrypt(encoded)
|
||||
return c.Status(403).Send(encrypted)
|
||||
}
|
||||
|
||||
user := db.FindUser(userid)
|
||||
if hash != decoded["hash1"] || user == nil {
|
||||
return sendErr()
|
||||
}
|
||||
|
||||
if c.Get("x-signature") != user["hash"] {
|
||||
return sendErr()
|
||||
}
|
||||
|
||||
// 更新 clientVersion
|
||||
for i, u := range db.DB.Users[server] {
|
||||
if fmt.Sprintf("%v", u["userId"]) == userid {
|
||||
db.DB.Users[server][i]["clientVersion"] = c.Get("x-clientversion")
|
||||
break
|
||||
}
|
||||
}
|
||||
db.Save()
|
||||
|
||||
return c.Send(nil)
|
||||
}
|
||||
Reference in New Issue
Block a user