pegasus/backend/internal/jellyfin.go

60 lines
1.4 KiB
Go
Raw Normal View History

2025-09-16 00:05:16 -05:00
// backend/internal/jellyfin.go
2025-09-08 00:48:47 -05:00
package internal
import (
"bytes"
"encoding/json"
2025-09-18 02:48:31 -05:00
"fmt" // <-- keep this; used by fmt.Errorf
2025-09-08 00:48:47 -05:00
"net/http"
"os"
"time"
)
// Minimal Jellyfin client
type jfAuthResult struct {
AccessToken string `json:"AccessToken"`
User struct {
2025-09-16 04:32:16 -05:00
Id string `json:"Id"`
Name string `json:"Name"`
2025-09-08 00:48:47 -05:00
} `json:"User"`
}
type Jellyfin struct {
BaseURL string
Client *http.Client
}
func NewJellyfin() *Jellyfin {
return &Jellyfin{
BaseURL: os.Getenv("JELLYFIN_URL"),
Client: &http.Client{Timeout: 10 * time.Second},
}
}
// Authenticate against /Users/AuthenticateByName using Pw (plaintext)
func (j *Jellyfin) AuthenticateByName(username, password string) (jfAuthResult, error) {
var out jfAuthResult
body := map[string]string{"Username": username, "Pw": password}
b, _ := json.Marshal(body)
req, _ := http.NewRequest("POST", j.BaseURL+"/Users/AuthenticateByName", bytes.NewReader(b))
req.Header.Set("Content-Type", "application/json")
2025-09-16 04:32:16 -05:00
// Jellyfin client descriptor (no token yet)
2025-09-08 00:48:47 -05:00
req.Header.Set("Authorization",
`MediaBrowser Client="Pegasus", Device="Pegasus Web", DeviceId="pegasus-web", Version="1.0.0"`)
resp, err := j.Client.Do(req)
if err != nil {
return out, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return out, fmt.Errorf("login failed: %s", resp.Status)
}
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return out, err
}
return out, nil
}