hello world
This commit is contained in:
103
storage/api.go
Normal file
103
storage/api.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"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) (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)
|
||||
}
|
||||
|
||||
if user != username {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
err = bcrypt.CompareHashAndPassword([]byte(pass), []byte(password))
|
||||
if err != nil {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
return true, createLoginToken()
|
||||
}
|
||||
|
||||
func createLoginToken() string {
|
||||
token := util.GeneratePassword(32)
|
||||
|
||||
member := redis.Z{
|
||||
Score: float64(time.Now().Unix() + 4 * 60 * 60),
|
||||
Member: token,
|
||||
}
|
||||
|
||||
err := client.ZAdd(ctx, "loginTokens", member).Err()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return token
|
||||
}
|
||||
|
||||
func CheckLoginToken(token 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, err := client.ZRangeByScore(ctx, "loginTokens", &redis.ZRangeBy{
|
||||
Min: fmt.Sprintf("%d", time.Now().Unix()),
|
||||
Max: "inf",
|
||||
}).Result()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, c := range current {
|
||||
fmt.Println(c)
|
||||
if c == token {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
71
storage/storage.go
Normal file
71
storage/storage.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"git.zomo.dev/zomo/discord-retokenizer/util"
|
||||
"github.com/go-redis/redis/v9"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
var client *redis.Client = nil
|
||||
|
||||
func initializeRedis() {
|
||||
username := "default"
|
||||
UpdateUsername( username)
|
||||
password := util.GeneratePassword(16)
|
||||
UpdatePassword( password)
|
||||
fmt.Printf("FIRST TIME SETUP\nusername: %s\npassword: %s\n\n\n", username, password)
|
||||
}
|
||||
|
||||
func validateRedis() {
|
||||
if client == nil {
|
||||
panic("Client == nil")
|
||||
}
|
||||
_, err := client.Get(ctx, "username").Result()
|
||||
if err != nil {
|
||||
if err != redis.Nil {
|
||||
panic(err)
|
||||
}
|
||||
initializeRedis()
|
||||
return
|
||||
}
|
||||
_, err = client.Get(ctx, "password").Result()
|
||||
if err != nil {
|
||||
if err != redis.Nil {
|
||||
panic(err)
|
||||
}
|
||||
initializeRedis()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func Init() {
|
||||
redisUri, err := url.Parse(os.Getenv("REDIS_URI"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if redisUri.Scheme != "redis" {
|
||||
panic("redisUri.Scheme != redis")
|
||||
}
|
||||
|
||||
username := redisUri.User.Username()
|
||||
pass, passSet := redisUri.User.Password()
|
||||
|
||||
if !passSet {
|
||||
panic("pass not set")
|
||||
}
|
||||
|
||||
client = redis.NewClient(&redis.Options{
|
||||
Addr: redisUri.Host,
|
||||
Username: username,
|
||||
Password: pass,
|
||||
DB: 0,
|
||||
})
|
||||
|
||||
validateRedis()
|
||||
}
|
||||
Reference in New Issue
Block a user