Initial commit

This commit is contained in:
2025-05-11 20:32:14 +08:00
commit d97247159f
80 changed files with 13013 additions and 0 deletions

62
server/utils/jwt.go Normal file
View File

@@ -0,0 +1,62 @@
package utils
import (
"errors"
"github.com/golang-jwt/jwt/v5"
"math/rand"
"os"
"time"
)
var (
key []byte
)
func init() {
secretFilePath := GetStoragePath() + "/jwt_secret.key"
secret, err := os.ReadFile(secretFilePath)
if err != nil {
key = secret
} else {
// Initialize the key with a random value
chars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
key = make([]byte, 32)
for i := range key {
r := rand.Intn(len(chars))
key[i] = byte(chars[r])
}
err = os.WriteFile(secretFilePath, key, 0644)
}
}
func GenerateToken(userID uint) (string, error) {
t := jwt.NewWithClaims(jwt.SigningMethodHS256,
jwt.MapClaims{
"id": userID,
"exp": time.Now().Add(7 * 24 * time.Hour).Unix(),
})
s, err := t.SignedString(key)
if err != nil {
return "", err
}
return s, nil
}
func ParseToken(token string) (uint, error) {
t, err := jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
return key, nil
})
if err != nil {
return 0, err
}
if claims, ok := t.Claims.(jwt.MapClaims); ok && t.Valid {
id := uint(claims["id"].(float64))
expF := claims["exp"].(float64)
exp := time.Unix(int64(expF), 0)
if time.Now().After(exp) {
return 0, errors.New("token expired")
}
return id, nil
}
return 0, errors.New("invalid token")
}

26
server/utils/storage.go Normal file
View File

@@ -0,0 +1,26 @@
package utils
import (
"os"
"runtime"
)
var path string
func GetStoragePath() string {
if path != "" {
return path
}
if runtime.GOOS == "linux" {
path = "/var/lib/nysoure"
} else {
userDir, _ := os.UserHomeDir()
path = userDir + "/.nysoure"
}
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.MkdirAll(path, os.ModePerm); err != nil {
panic("failed to create storage directory")
}
}
return path
}