updated token storage

This commit is contained in:
2022-12-09 14:19:26 -06:00
parent 94f3ff9a26
commit b7c156d1e0
5 changed files with 46 additions and 10 deletions

View File

@@ -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"`

View File

@@ -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

View File

@@ -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)

27
storage/bots.go Normal file
View File

@@ -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() {
}

View File

@@ -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 {