GoScrobble/internal/goscrobble/tokens.go
Daniel Mason af02bd99cc
0.0.8
- Added Admin/Site config page in frontend for admin users
- Added API POST/GET /config endpoint
2021-03-31 22:17:42 +13:00

33 lines
718 B
Go

package goscrobble
import (
"errors"
"math/rand"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func generateToken(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
return string(b)
}
func getUserUuidForToken(token string) (string, error) {
var uuid string
cachedKey := getRedisVal("user_token:" + token)
if cachedKey == "" {
err := db.QueryRow("SELECT BIN_TO_UUID(`uuid`, true) FROM `users` WHERE `token` = ? AND `active` = 1", token).Scan(&uuid)
if err != nil {
return "", errors.New("Invalid Token")
}
setRedisVal("user_token:"+token, uuid)
} else {
uuid = cachedKey
}
return uuid, nil
}