GoScrobble/internal/goscrobble/tokens.go

26 lines
565 B
Go
Raw Normal View History

2021-03-28 08:52:34 +00:00
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 getUserForToken(token string) (string, error) {
var uuid string
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")
}
return uuid, nil
}