@ -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)
return "", fmt.Errorf("failed to validate token: %s", err)
return username, nil
func uuidFromRequest(r *http.Request) ([]byte, error) {
@ -39,15 +39,15 @@ import (
// account
func handleAccountInfo(w http.ResponseWriter, r *http.Request) {
username, err := usernameFromRequest(r)
uuid, err := uuidFromRequest(r)
httpError(w, r, err, http.StatusBadRequest)
return
uuid, err := uuidFromRequest(r) // lazy
username, err := db.FetchUsernameFromUUID(uuid)
httpError(w, r, err, http.StatusInternalServerError)
@ -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)
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 {
func FetchUsernameFromUUID(uuid []byte) (string, error) {
err := handle.QueryRow("SELECT username FROM accounts WHERE uuid = ?", uuid).Scan(&username)