updated token storage
This commit is contained in:
126
storage/login.go
Normal file
126
storage/login.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.zomo.dev/zomo/discord-retokenizer/util"
|
||||
"github.com/go-redis/redis/v9"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func UpdateUsername(username string) {
|
||||
if username != "" {
|
||||
client.Set(ctx, "username", username, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func UpdatePassword(password string) {
|
||||
if password != "" {
|
||||
passHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
client.Set(ctx, "password", string(passHash), 0)
|
||||
}
|
||||
}
|
||||
|
||||
func CheckLogin(username string, password string, ip string) (bool, string) {
|
||||
if username == "" || password == "" {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
user, err := client.Get(ctx, "username").Result()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
pass, err := client.Get(ctx, "password").Result()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println(user, username)
|
||||
|
||||
if user != username {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
err = bcrypt.CompareHashAndPassword([]byte(pass), []byte(password))
|
||||
if err != nil {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
return true, createLoginToken(ip)
|
||||
}
|
||||
|
||||
type LoginToken struct {
|
||||
Token string `json:"token"`
|
||||
IP string `json:"ip"`
|
||||
End string `json:"end"`
|
||||
}
|
||||
|
||||
func createLoginToken(ip string) string {
|
||||
token := util.GeneratePassword(32)
|
||||
|
||||
tokenHash, err := bcrypt.GenerateFromPassword([]byte(token), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
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: string(marshalled),
|
||||
}
|
||||
|
||||
err = client.ZAdd(ctx, "loginTokens", member).Err()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return token
|
||||
}
|
||||
|
||||
func CheckLoginToken(token string, ip string) bool {
|
||||
|
||||
expired, err := client.ZRangeByScore(ctx, "loginTokens", &redis.ZRangeBy{
|
||||
Min: "-inf",
|
||||
Max: fmt.Sprintf("%d", time.Now().Unix()),
|
||||
}).Result()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, e := range expired {
|
||||
client.ZRem(ctx, "loginTokens", e)
|
||||
}
|
||||
|
||||
current := make([]LoginToken, 0)
|
||||
err = client.ZRange(ctx, "loginTokens", 0, -1).ScanSlice(current)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, c := range current {
|
||||
err = bcrypt.CompareHashAndPassword([]byte(c.Token), []byte(token))
|
||||
if err == nil && ip == c.IP {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user