pegasus/backend/internal/jellyfin.go

61 lines
1.6 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"
2026-04-11 00:02:59 -03:00
"fmt"
2025-09-08 00:48:47 -05:00
"net/http"
"os"
"time"
)
2026-04-11 00:02:59 -03:00
// AuthResult is the subset of Jellyfin auth response Pegasus needs.
type AuthResult struct {
2025-09-08 00:48:47 -05:00
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"`
}
2026-04-11 00:02:59 -03:00
// Jellyfin is a tiny client for the auth and refresh endpoints Pegasus uses.
2025-09-08 00:48:47 -05:00
type Jellyfin struct {
BaseURL string
Client *http.Client
}
2026-04-11 00:02:59 -03:00
// NewJellyfin builds a lightweight client for Jellyfin auth and refresh calls.
2025-09-08 00:48:47 -05:00
func NewJellyfin() *Jellyfin {
return &Jellyfin{
BaseURL: os.Getenv("JELLYFIN_URL"),
Client: &http.Client{Timeout: 10 * time.Second},
}
}
2026-04-11 00:02:59 -03:00
// AuthenticateByName signs into Jellyfin with the plaintext password Jellyfin expects.
func (j *Jellyfin) AuthenticateByName(username, password string) (AuthResult, error) {
var out AuthResult
2025-09-08 00:48:47 -05:00
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
}