2021-03-28 08:52:34 +00:00
|
|
|
package goscrobble
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math/rand"
|
2021-04-01 10:17:46 +00:00
|
|
|
"time"
|
2021-03-28 08:52:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
2021-04-01 10:17:46 +00:00
|
|
|
// generateToken - Generates a unique token for user input
|
2021-03-28 08:52:34 +00:00
|
|
|
func generateToken(n int) string {
|
2021-04-01 10:17:46 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2021-03-28 08:52:34 +00:00
|
|
|
b := make([]byte, n)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2021-03-31 08:40:20 +00:00
|
|
|
func getUserUuidForToken(token string) (string, error) {
|
2021-03-28 08:52:34 +00:00
|
|
|
var uuid string
|
2021-03-29 07:56:34 +00:00
|
|
|
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
|
2021-03-28 08:52:34 +00:00
|
|
|
}
|
2021-03-29 07:56:34 +00:00
|
|
|
|
2021-03-28 08:52:34 +00:00
|
|
|
return uuid, nil
|
|
|
|
}
|