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/common.go

62 lines
1.3 KiB
Go

11 months ago
package api
import (
"encoding/base64"
"fmt"
"net/http"
"github.com/pagefaultgames/pokerogue-server/api/account"
"github.com/pagefaultgames/pokerogue-server/api/daily"
"github.com/pagefaultgames/pokerogue-server/db"
11 months ago
)
func Init() {
scheduleStatRefresh()
daily.Init()
}
func getTokenFromRequest(r *http.Request) ([]byte, error) {
8 months ago
if r.Header.Get("Authorization") == "" {
return nil, fmt.Errorf("missing token")
11 months ago
}
8 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)
11 months ago
}
if len(token) != account.TokenSize {
return nil, fmt.Errorf("invalid token length: got %d, expected %d", len(token), account.TokenSize)
}
return token, nil
}
func getUsernameFromRequest(r *http.Request) (string, error) {
token, err := getTokenFromRequest(r)
if err != nil {
return "", err
11 months ago
}
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) {
token, err := getTokenFromRequest(r)
11 months ago
if err != nil {
return nil, err
11 months ago
}
uuid, err := db.FetchUUIDFromToken(token)
11 months ago
if err != nil {
return nil, fmt.Errorf("failed to validate token: %s", err)
}
return uuid, nil
}