updated login tokens

This commit is contained in:
2022-12-09 13:04:03 -06:00
parent 8ae527683b
commit 94f3ff9a26
4 changed files with 30 additions and 19 deletions

View File

@@ -24,7 +24,7 @@ func getAuthorization(c *gin.Context) (AuthorizationScope, string) {
return AuthorizationScopeNone, "" return AuthorizationScopeNone, ""
} }
if headerSpl[0] == "Bearer" { if headerSpl[0] == "Bearer" {
if storage.CheckLoginToken(headerSpl[1]) { if storage.CheckLoginToken(headerSpl[1], c.ClientIP()) {
return AuthorizationScopeUser, headerSpl[1] return AuthorizationScopeUser, headerSpl[1]
} }
} }

View File

@@ -19,7 +19,7 @@ func login(c *gin.Context) {
return return
} }
loggedIn, token := storage.CheckLogin(loginBody.Username, loginBody.Password) loggedIn, token := storage.CheckLogin(loginBody.Username, loginBody.Password, c.ClientIP())
if loggedIn { if loggedIn {
c.JSON(200, gin.H{ c.JSON(200, gin.H{

View File

@@ -25,7 +25,7 @@ func UpdatePassword(password string) {
} }
} }
func CheckLogin(username string, password string) (bool, string) { func CheckLogin(username string, password string, ip string) (bool, string) {
if username == "" || password == "" { if username == "" || password == "" {
return false, "" return false, ""
} }
@@ -49,18 +49,35 @@ func CheckLogin(username string, password string) (bool, string) {
return false, "" return false, ""
} }
return true, createLoginToken() return true, createLoginToken(ip)
} }
func createLoginToken() string { type loginToken struct {
Token []byte `json:"token"`
IP string `jsong:"ip"`
End string `json:"end"`
}
func createLoginToken(ip string) string {
token := util.GeneratePassword(32) token := util.GeneratePassword(32)
tokenHash, err := bcrypt.GenerateFromPassword([]byte(token), bcrypt.DefaultCost)
if err != nil {
panic(err)
}
tokenData := loginToken{
Token: tokenHash,
IP: ip,
End: token[len(token) - 4:],
}
member := redis.Z{ member := redis.Z{
Score: float64(time.Now().Unix() + 4 * 60 * 60), Score: float64(time.Now().Unix() + 4 * 60 * 60),
Member: token, Member: tokenData,
} }
err := client.ZAdd(ctx, "loginTokens", member).Err() err = client.ZAdd(ctx, "loginTokens", member).Err()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -68,7 +85,7 @@ func createLoginToken() string {
return token return token
} }
func CheckLoginToken(token string) bool { func CheckLoginToken(token string, ip string) bool {
expired, err := client.ZRangeByScore(ctx, "loginTokens", &redis.ZRangeBy{ expired, err := client.ZRangeByScore(ctx, "loginTokens", &redis.ZRangeBy{
Min: "-inf", Min: "-inf",
@@ -83,18 +100,16 @@ func CheckLoginToken(token string) bool {
client.ZRem(ctx, "loginTokens", e) client.ZRem(ctx, "loginTokens", e)
} }
current, err := client.ZRangeByScore(ctx, "loginTokens", &redis.ZRangeBy{ current := make([]loginToken, 0)
Min: fmt.Sprintf("%d", time.Now().Unix()), err = client.ZRange(ctx, "loginTokens", 0, -1).ScanSlice(current)
Max: "inf",
}).Result()
if err != nil { if err != nil {
panic(err) panic(err)
} }
for _, c := range current { for _, c := range current {
fmt.Println(c) err = bcrypt.CompareHashAndPassword([]byte(c.Token), []byte(token))
if c == token { if err == nil && ip == c.IP {
return true return true
} }
} }

View File

@@ -54,11 +54,7 @@ func Init() {
} }
username := redisUri.User.Username() username := redisUri.User.Username()
pass, passSet := redisUri.User.Password() pass, _ := redisUri.User.Password()
if !passSet {
panic("pass not set")
}
client = redis.NewClient(&redis.Options{ client = redis.NewClient(&redis.Options{
Addr: redisUri.Host, Addr: redisUri.Host,