From b7c156d1e0744ccce9523714a5912652795159dc Mon Sep 17 00:00:00 2001 From: ashley zomo Date: Fri, 9 Dec 2022 14:19:26 -0600 Subject: [PATCH] updated token storage --- discord/structs.go | 2 +- endpoints/endpoints.go | 2 +- endpoints/login.go | 1 + storage/bots.go | 27 +++++++++++++++++++++++++++ storage/{api.go => login.go} | 24 ++++++++++++++++-------- 5 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 storage/bots.go rename storage/{api.go => login.go} (85%) diff --git a/discord/structs.go b/discord/structs.go index 4515ea0..a30736d 100644 --- a/discord/structs.go +++ b/discord/structs.go @@ -1,7 +1,7 @@ package discord type User struct { - Id string `json:"id"` + ID string `json:"id"` Username string `json:"username"` Discriminator string `json:"discriminator"` Avatar string `json:"avatar"` diff --git a/endpoints/endpoints.go b/endpoints/endpoints.go index ae3c692..6c9badb 100644 --- a/endpoints/endpoints.go +++ b/endpoints/endpoints.go @@ -15,7 +15,7 @@ func Run() { private := r.Group("/") private.Use(userIsAuthorized) - private.POST("/user", user) //change username/password (required before adding bots) + private.PATCH("/login", user) //change username/password (required before adding bots) private.GET("/bots", func(c *gin.Context) {}) //generalized list of bots private.GET("/bot/:bot", func(c *gin.Context) {}) //specific bot diff --git a/endpoints/login.go b/endpoints/login.go index c9cd7b2..cc14a38 100644 --- a/endpoints/login.go +++ b/endpoints/login.go @@ -13,6 +13,7 @@ type LoginBody struct { } func login(c *gin.Context) { + var loginBody LoginBody if err := c.BindJSON(&loginBody); err != nil { fmt.Println(err) diff --git a/storage/bots.go b/storage/bots.go new file mode 100644 index 0000000..1791d8e --- /dev/null +++ b/storage/bots.go @@ -0,0 +1,27 @@ +package storage + +type SimpleDiscordUser struct { + Username string `json:"username"` + Discriminator string `json:"discriminator"` + Avatar string `json:"avatar"` + Banner string `json:"banner"` + Accent_color int `json:"accent_color"` + Verified bool `json:"verified"` +} + +type BotData struct { + Token string + ID string + Loaded SimpleDiscordUser +} + +// one array of just bot ids +// `bot:(id):token` +// `bot:(id):data` +// SimpleDiscordUser + +func GetBots() { + + + +} \ No newline at end of file diff --git a/storage/api.go b/storage/login.go similarity index 85% rename from storage/api.go rename to storage/login.go index fc32919..1db067a 100644 --- a/storage/api.go +++ b/storage/login.go @@ -1,6 +1,7 @@ package storage import ( + "encoding/json" "fmt" "time" @@ -40,6 +41,8 @@ func CheckLogin(username string, password string, ip string) (bool, string) { panic(err) } + fmt.Println(user, username) + if user != username { return false, "" } @@ -52,10 +55,10 @@ func CheckLogin(username string, password string, ip string) (bool, string) { return true, createLoginToken(ip) } -type loginToken struct { - Token []byte `json:"token"` - IP string `jsong:"ip"` - End string `json:"end"` +type LoginToken struct { + Token string `json:"token"` + IP string `json:"ip"` + End string `json:"end"` } func createLoginToken(ip string) string { @@ -66,15 +69,20 @@ func createLoginToken(ip string) string { panic(err) } - tokenData := loginToken{ - Token: tokenHash, + tokenData := LoginToken{ + Token: string(tokenHash), IP: ip, End: token[len(token) - 4:], } + marshalled, err := json.Marshal(&tokenData) + if err != nil { + panic(err) + } + member := redis.Z{ Score: float64(time.Now().Unix() + 4 * 60 * 60), - Member: tokenData, + Member: string(marshalled), } err = client.ZAdd(ctx, "loginTokens", member).Err() @@ -100,7 +108,7 @@ func CheckLoginToken(token string, ip string) bool { client.ZRem(ctx, "loginTokens", e) } - current := make([]loginToken, 0) + current := make([]LoginToken, 0) err = client.ZRange(ctx, "loginTokens", 0, -1).ScanSlice(current) if err != nil {