You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rogueserver/api/account-helper.go

54 lines
1.2 KiB
Go

11 months ago
package api
import (
"encoding/base64"
"fmt"
"net/http"
"github.com/pagefaultgames/pokerogue-server/db"
11 months ago
)
7 months ago
func getUsernameFromRequest(r *http.Request) (string, error) {
if r.Header.Get("Authorization") == "" {
11 months ago
return "", fmt.Errorf("missing token")
}
7 months ago
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
11 months ago
if err != nil {
return "", fmt.Errorf("failed to decode token: %s", err)
}
if len(token) != 32 {
return "", fmt.Errorf("invalid token length: got %d, expected 32", len(token))
}
username, err := db.FetchUsernameFromToken(token)
11 months ago
if err != nil {
return "", fmt.Errorf("failed to validate token: %s", err)
}
return username, nil
}
func getUUIDFromRequest(r *http.Request) ([]byte, error) {
7 months ago
if r.Header.Get("Authorization") == "" {
11 months ago
return nil, fmt.Errorf("missing token")
}
7 months ago
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
11 months ago
if err != nil {
return nil, fmt.Errorf("failed to decode token: %s", err)
}
if len(token) != 32 {
return nil, fmt.Errorf("invalid token length: got %d, expected 32", len(token))
}
uuid, err := db.FetchUUIDFromToken(token)
11 months ago
if err != nil {
return nil, fmt.Errorf("failed to validate token: %s", err)
}
return uuid, nil
}