Replace usernameFromRequest

pull/4/head
maru 8 months ago
parent 808a9e6c7d
commit a087234aa8
No known key found for this signature in database
GPG Key ID: 37689350E9CD0F0D

@ -73,20 +73,6 @@ func tokenFromRequest(r *http.Request) ([]byte, error) {
return token, nil
}
func usernameFromRequest(r *http.Request) (string, error) {
token, err := tokenFromRequest(r)
if err != nil {
return "", err
}
username, err := db.FetchUsernameFromToken(token)
if err != nil {
return "", fmt.Errorf("failed to validate token: %s", err)
}
return username, nil
}
func uuidFromRequest(r *http.Request) ([]byte, error) {
token, err := tokenFromRequest(r)
if err != nil {

@ -39,15 +39,15 @@ import (
// account
func handleAccountInfo(w http.ResponseWriter, r *http.Request) {
username, err := usernameFromRequest(r)
uuid, err := uuidFromRequest(r)
if err != nil {
httpError(w, r, err, http.StatusBadRequest)
return
}
uuid, err := uuidFromRequest(r) // lazy
username, err := db.FetchUsernameFromUUID(uuid)
if err != nil {
httpError(w, r, err, http.StatusBadRequest)
httpError(w, r, err, http.StatusInternalServerError)
return
}

@ -185,16 +185,6 @@ func DeleteClaimedAccountCompensations(uuid []byte) error {
return nil
}
func FetchUsernameFromToken(token []byte) (string, error) {
var username string
err := handle.QueryRow("SELECT a.username FROM accounts a JOIN sessions s ON s.uuid = a.uuid WHERE s.token = ? AND s.expire > UTC_TIMESTAMP()", token).Scan(&username)
if err != nil {
return "", err
}
return username, nil
}
func FetchAccountKeySaltFromUsername(username string) ([]byte, []byte, error) {
var key, salt []byte
err := handle.QueryRow("SELECT hash, salt FROM accounts WHERE username = ?", username).Scan(&key, &salt)
@ -260,3 +250,13 @@ func RemoveSessionFromToken(token []byte) error {
return nil
}
func FetchUsernameFromUUID(uuid []byte) (string, error) {
var username string
err := handle.QueryRow("SELECT username FROM accounts WHERE uuid = ?", uuid).Scan(&username)
if err != nil {
return "", err
}
return username, nil
}

Loading…
Cancel
Save