Merge pull request 'OpenSubsonic: getUser, so that apps can log in' (#26) from fix/opensubsonic-getuser into main
This commit is contained in:
commit
e37f1165ae
6 changed files with 207 additions and 29 deletions
|
|
@ -311,6 +311,8 @@ A device password opens your music in that app and nothing else: it cannot sign
|
|||
| `scrobble` | A listen is recorded like one made in the web app (and shared on Mastodon if you connected it). A "now playing" message is not recorded. |
|
||||
| `star`, `unstar`, `getStarred2` | Your favourites (see **Favourites**): songs, albums and artists, with the `starred` date on each, and `getAlbumList2` of type `starred`. |
|
||||
| `setRating`, `getNowPlaying` | Answered with nothing: there are no ratings, and what you play is nobody else's business. |
|
||||
| `getUser` | Who the app is logged in as. It is always you, with the roles of an app: play, download, playlists, covers, and never admin. |
|
||||
| `getArtistInfo`, `getArtistInfo2`, `getAlbumInfo`, `getAlbumInfo2`, `getTopSongs`, `getSimilarSongs`, `getSimilarSongs2`, `getLyrics`, `getLyricsBySongId`, `getInternetRadioStations`, `getScanStatus`, `startScan`, `getPlayQueue`, `savePlayQueue`, `getPlayQueueByIndex`, `savePlayQueueByIndex` | Answered with nothing (a saved queue is not kept), so that apps that ask them right after the login, like Feishin, work. A call Sonda does not know is an error `0` and shows in the server log. |
|
||||
|
||||
Sonda does not transcode: an app always gets the original file, whatever format or bit rate it asks for. The music of friends (federation) is not shown to apps.
|
||||
|
||||
|
|
|
|||
|
|
@ -402,3 +402,23 @@ func stamp(t time.Time) string {
|
|||
}
|
||||
return t.UTC().Format("2006-01-02T15:04:05.000Z")
|
||||
}
|
||||
|
||||
// user tells who the login is and what the app may do. An app is for the music: it can play, make playlists and read
|
||||
// covers, and it is never an admin, whoever the person is. It cannot ask about anybody else.
|
||||
func (h *handler) user(k *call) (*Response, *Error) {
|
||||
if name := k.param("username"); name != "" && !strings.EqualFold(strings.TrimSpace(name), k.principal.Email) {
|
||||
return nil, &Error{Code: ErrNotAuthorized, Message: "an app can only ask about its own user"}
|
||||
}
|
||||
libs, err := h.Libraries.List(k.ctx)
|
||||
if err != nil {
|
||||
return nil, failure(err)
|
||||
}
|
||||
folders := make([]int64, len(libs))
|
||||
for i, l := range libs {
|
||||
folders[i] = l.ID
|
||||
}
|
||||
return &Response{User: &User{
|
||||
Username: k.principal.Email, Email: k.principal.Email, ScrobblingEnabled: true,
|
||||
DownloadRole: true, PlaylistRole: true, CoverArtRole: true, StreamRole: true, Folder: folders,
|
||||
}}, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -79,33 +79,51 @@ func (k *call) param(name string) string { return k.c.Request.Form.Get(name) }
|
|||
func Register(r gin.IRoutes, d Deps) {
|
||||
h := &handler{Deps: d}
|
||||
h.calls = map[string]func(*call) (*Response, *Error){
|
||||
"ping": func(*call) (*Response, *Error) { return &Response{}, nil },
|
||||
"getLicense": func(*call) (*Response, *Error) { return &Response{License: &License{Valid: true}}, nil },
|
||||
"tokenInfo": h.tokenInfo,
|
||||
"getMusicFolders": h.musicFolders,
|
||||
"getArtists": h.artists,
|
||||
"getArtist": h.artist,
|
||||
"getAlbum": h.album,
|
||||
"getSong": h.song,
|
||||
"getGenres": h.genres,
|
||||
"getAlbumList2": h.albumList2,
|
||||
"getRandomSongs": h.randomSongs,
|
||||
"getSongsByGenre": h.songsByGenre,
|
||||
"search3": h.search3,
|
||||
"getStarred2": h.starred2,
|
||||
"star": h.star,
|
||||
"unstar": h.unstar,
|
||||
"setRating": nothing,
|
||||
"stream": h.stream,
|
||||
"download": h.download,
|
||||
"getCoverArt": h.coverArt,
|
||||
"getPlaylists": h.playlists,
|
||||
"getPlaylist": h.playlist,
|
||||
"createPlaylist": h.createPlaylist,
|
||||
"updatePlaylist": h.updatePlaylist,
|
||||
"deletePlaylist": h.deletePlaylist,
|
||||
"scrobble": h.scrobble,
|
||||
"getNowPlaying": h.nowPlaying,
|
||||
"ping": func(*call) (*Response, *Error) { return &Response{}, nil },
|
||||
"getLicense": func(*call) (*Response, *Error) { return &Response{License: &License{Valid: true}}, nil },
|
||||
"tokenInfo": h.tokenInfo,
|
||||
"getMusicFolders": h.musicFolders,
|
||||
"getArtists": h.artists,
|
||||
"getArtist": h.artist,
|
||||
"getAlbum": h.album,
|
||||
"getSong": h.song,
|
||||
"getGenres": h.genres,
|
||||
"getAlbumList2": h.albumList2,
|
||||
"getRandomSongs": h.randomSongs,
|
||||
"getSongsByGenre": h.songsByGenre,
|
||||
"search3": h.search3,
|
||||
"getStarred2": h.starred2,
|
||||
"star": h.star,
|
||||
"unstar": h.unstar,
|
||||
"setRating": nothing,
|
||||
"stream": h.stream,
|
||||
"download": h.download,
|
||||
"getCoverArt": h.coverArt,
|
||||
"getUser": h.user,
|
||||
"getStarred": h.starred,
|
||||
"getLyricsBySongId": func(*call) (*Response, *Error) { return &Response{LyricsList: &Empty{}}, nil },
|
||||
"getLyrics": func(*call) (*Response, *Error) { return &Response{Lyrics: &Empty{}}, nil },
|
||||
"getPlayQueueByIndex": nothing,
|
||||
"savePlayQueueByIndex": nothing,
|
||||
"startScan": func(*call) (*Response, *Error) { return &Response{ScanStatus: &ScanStatus{}}, nil },
|
||||
"getArtistInfo": func(*call) (*Response, *Error) { return &Response{ArtistInfo: &Empty{}}, nil },
|
||||
"getArtistInfo2": func(*call) (*Response, *Error) { return &Response{ArtistInfo2: &Empty{}}, nil },
|
||||
"getAlbumInfo": func(*call) (*Response, *Error) { return &Response{AlbumInfo: &Empty{}}, nil },
|
||||
"getAlbumInfo2": func(*call) (*Response, *Error) { return &Response{AlbumInfo: &Empty{}}, nil },
|
||||
"getTopSongs": func(*call) (*Response, *Error) { return &Response{TopSongs: &Empty{}}, nil },
|
||||
"getSimilarSongs": func(*call) (*Response, *Error) { return &Response{SimilarSongs: &Empty{}}, nil },
|
||||
"getSimilarSongs2": func(*call) (*Response, *Error) { return &Response{SimilarSongs2: &Empty{}}, nil },
|
||||
"getInternetRadioStations": func(*call) (*Response, *Error) { return &Response{InternetRadioStations: &Empty{}}, nil },
|
||||
"getScanStatus": func(*call) (*Response, *Error) { return &Response{ScanStatus: &ScanStatus{}}, nil },
|
||||
"getPlayQueue": nothing,
|
||||
"savePlayQueue": nothing,
|
||||
"getPlaylists": h.playlists,
|
||||
"getPlaylist": h.playlist,
|
||||
"createPlaylist": h.createPlaylist,
|
||||
"updatePlaylist": h.updatePlaylist,
|
||||
"deletePlaylist": h.deletePlaylist,
|
||||
"scrobble": h.scrobble,
|
||||
"getNowPlaying": h.nowPlaying,
|
||||
}
|
||||
r.GET("/rest/:method", h.serve)
|
||||
r.HEAD("/rest/:method", h.serve)
|
||||
|
|
@ -140,6 +158,8 @@ func (h *handler) dispatch(c *gin.Context, method string) (*Response, *Error) {
|
|||
}
|
||||
fn, known := h.calls[method]
|
||||
if !known {
|
||||
// The request log shows the path and the error: what an app asks that Sonda does not answer is worth seeing.
|
||||
_ = c.Error(errors.New("opensubsonic: unknown method " + method))
|
||||
return nil, &Error{Code: ErrGeneric, Message: "unknown method: " + method}
|
||||
}
|
||||
k := &call{c: c, ctx: c.Request.Context()}
|
||||
|
|
|
|||
|
|
@ -1325,3 +1325,81 @@ func TestStarredThingsAreListedAndMarkedWithTheirDate(t *testing.T) {
|
|||
t.Fatalf("starred songs %d", n)
|
||||
}
|
||||
}
|
||||
|
||||
// ---- what an app asks for as soon as it has logged in ----
|
||||
|
||||
func TestAnAppAsksWhoItIsAndGetsNoPowerOverTheServer(t *testing.T) {
|
||||
h, _ := newTestServer()
|
||||
u := good(t, get(t, h, "GET", "/rest/getUser.view", ok("username", "tom@example.org")))["user"].(map[string]any)
|
||||
if u["username"] != "tom@example.org" || u["email"] != "tom@example.org" || u["scrobblingEnabled"] != true {
|
||||
t.Fatalf("user %v", u)
|
||||
}
|
||||
// An app is for the music: it can play, make playlists and read covers, and it is never an admin.
|
||||
for role, want := range map[string]bool{
|
||||
"adminRole": false, "settingsRole": false, "uploadRole": false, "podcastRole": false, "jukeboxRole": false, "shareRole": false,
|
||||
"commentRole": false, "videoConversionRole": false,
|
||||
"streamRole": true, "downloadRole": true, "playlistRole": true, "coverArtRole": true,
|
||||
} {
|
||||
if got, ok := u[role].(bool); !ok || got != want {
|
||||
t.Errorf("%s = %v, want %v", role, u[role], want)
|
||||
}
|
||||
}
|
||||
if f := u["folder"].([]any); len(f) != 2 || f[0].(float64) != 1 || f[1].(float64) != 2 {
|
||||
t.Errorf("folders %v", f)
|
||||
}
|
||||
// No name asked for: the person of the login.
|
||||
if u := good(t, get(t, h, "GET", "/rest/getUser", ok()))["user"].(map[string]any); u["username"] != "tom@example.org" {
|
||||
t.Errorf("user %v", u)
|
||||
}
|
||||
// Another person is not for an app to look at.
|
||||
failed(t, get(t, h, "GET", "/rest/getUser", ok("username", "ann@example.org")), ErrNotAuthorized)
|
||||
if u := good(t, get(t, h, "GET", "/rest/getUser", ok("username", "TOM@example.org")))["user"].(map[string]any); u["username"] != "tom@example.org" {
|
||||
t.Errorf("the same address in capitals: %v", u)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWhatAnAppAsksThatSondaDoesNotKnowAnswersWithNothing(t *testing.T) {
|
||||
h, _ := newTestServer()
|
||||
for method, key := range map[string]string{
|
||||
"getArtistInfo2": "artistInfo2",
|
||||
"getArtistInfo": "artistInfo",
|
||||
"getAlbumInfo2": "albumInfo",
|
||||
"getAlbumInfo": "albumInfo",
|
||||
"getTopSongs": "topSongs",
|
||||
"getSimilarSongs": "similarSongs",
|
||||
"getSimilarSongs2": "similarSongs2",
|
||||
"getInternetRadioStations": "internetRadioStations",
|
||||
"getScanStatus": "scanStatus",
|
||||
} {
|
||||
r := good(t, get(t, h, "GET", "/rest/"+method, ok("id", "ar-1")))
|
||||
if _, has := r[key].(map[string]any); !has {
|
||||
t.Errorf("%s: no %s in %v", method, key, r)
|
||||
}
|
||||
}
|
||||
if s := good(t, get(t, h, "GET", "/rest/getScanStatus", ok()))["scanStatus"].(map[string]any); s["scanning"] != false {
|
||||
t.Errorf("scan status %v", s)
|
||||
}
|
||||
// A saved queue is not kept: there is none, and saving one is not an error.
|
||||
good(t, get(t, h, "GET", "/rest/getPlayQueue", ok()))
|
||||
good(t, get(t, h, "POST", "/rest/savePlayQueue", ok("id", "tr-70")))
|
||||
// These need a login like the rest.
|
||||
failed(t, get(t, h, "GET", "/rest/getUser", url.Values{"f": {"json"}}), ErrMissingParam)
|
||||
}
|
||||
|
||||
func TestTheOlderCallsOfAnAppAreAnsweredToo(t *testing.T) {
|
||||
h, _ := newStarServer()
|
||||
q := ok()
|
||||
q.Add("id", "tr-70")
|
||||
good(t, get(t, h, "GET", "/rest/star", q))
|
||||
s := good(t, get(t, h, "GET", "/rest/getStarred", ok()))["starred"].(map[string]any)
|
||||
if songs := s["song"].([]any); len(songs) != 1 || songs[0].(map[string]any)["id"] != "tr-70" {
|
||||
t.Fatalf("starred %v", s)
|
||||
}
|
||||
for method, key := range map[string]string{"getLyricsBySongId": "lyricsList", "getLyrics": "lyrics", "startScan": "scanStatus"} {
|
||||
if r := good(t, get(t, h, "GET", "/rest/"+method, ok("id", "tr-70"))); r[key] == nil {
|
||||
t.Errorf("%s: %v", method, r)
|
||||
}
|
||||
}
|
||||
good(t, get(t, h, "GET", "/rest/getPlayQueueByIndex", ok()))
|
||||
good(t, get(t, h, "POST", "/rest/savePlayQueueByIndex", ok("id", "tr-70", "currentIndex", "0")))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,9 +34,50 @@ type Response struct {
|
|||
SongsByGenre *SongList `xml:"songsByGenre" json:"songsByGenre,omitempty"`
|
||||
SearchResult3 *SearchResult `xml:"searchResult3" json:"searchResult3,omitempty"`
|
||||
Starred2 *SearchResult `xml:"starred2" json:"starred2,omitempty"`
|
||||
Starred *SearchResult `xml:"starred" json:"starred,omitempty"`
|
||||
LyricsList *Empty `xml:"lyricsList" json:"lyricsList,omitempty"`
|
||||
Lyrics *Empty `xml:"lyrics" json:"lyrics,omitempty"`
|
||||
Playlists *PlaylistList `xml:"playlists" json:"playlists,omitempty"`
|
||||
Playlist *Playlist `xml:"playlist" json:"playlist,omitempty"`
|
||||
NowPlaying *NowPlaying `xml:"nowPlaying" json:"nowPlaying,omitempty"`
|
||||
User *User `xml:"user" json:"user,omitempty"`
|
||||
ArtistInfo *Empty `xml:"artistInfo" json:"artistInfo,omitempty"`
|
||||
ArtistInfo2 *Empty `xml:"artistInfo2" json:"artistInfo2,omitempty"`
|
||||
AlbumInfo *Empty `xml:"albumInfo" json:"albumInfo,omitempty"`
|
||||
TopSongs *Empty `xml:"topSongs" json:"topSongs,omitempty"`
|
||||
SimilarSongs *Empty `xml:"similarSongs" json:"similarSongs,omitempty"`
|
||||
SimilarSongs2 *Empty `xml:"similarSongs2" json:"similarSongs2,omitempty"`
|
||||
InternetRadioStations *Empty `xml:"internetRadioStations" json:"internetRadioStations,omitempty"`
|
||||
ScanStatus *ScanStatus `xml:"scanStatus" json:"scanStatus,omitempty"`
|
||||
}
|
||||
|
||||
// Empty is an answer that has nothing in it: Sonda has no such thing to tell, and an app should not break for it.
|
||||
type Empty struct{}
|
||||
|
||||
// ScanStatus says whether the library is being scanned. Apps ask it to show a progress bar; Sonda scans in
|
||||
// the background and tells admins, not apps.
|
||||
type ScanStatus struct {
|
||||
Scanning bool `xml:"scanning,attr" json:"scanning"`
|
||||
}
|
||||
|
||||
// User is the person of a login, and what the app may do for them.
|
||||
type User struct {
|
||||
Username string `xml:"username,attr" json:"username"`
|
||||
Email string `xml:"email,attr,omitempty" json:"email,omitempty"`
|
||||
ScrobblingEnabled bool `xml:"scrobblingEnabled,attr" json:"scrobblingEnabled"`
|
||||
AdminRole bool `xml:"adminRole,attr" json:"adminRole"`
|
||||
SettingsRole bool `xml:"settingsRole,attr" json:"settingsRole"`
|
||||
DownloadRole bool `xml:"downloadRole,attr" json:"downloadRole"`
|
||||
UploadRole bool `xml:"uploadRole,attr" json:"uploadRole"`
|
||||
PlaylistRole bool `xml:"playlistRole,attr" json:"playlistRole"`
|
||||
CoverArtRole bool `xml:"coverArtRole,attr" json:"coverArtRole"`
|
||||
CommentRole bool `xml:"commentRole,attr" json:"commentRole"`
|
||||
PodcastRole bool `xml:"podcastRole,attr" json:"podcastRole"`
|
||||
StreamRole bool `xml:"streamRole,attr" json:"streamRole"`
|
||||
JukeboxRole bool `xml:"jukeboxRole,attr" json:"jukeboxRole"`
|
||||
ShareRole bool `xml:"shareRole,attr" json:"shareRole"`
|
||||
VideoConversionRole bool `xml:"videoConversionRole,attr" json:"videoConversionRole"`
|
||||
Folder []int64 `xml:"folder" json:"folder"`
|
||||
}
|
||||
|
||||
// PlaylistList are the playlists of a person.
|
||||
|
|
|
|||
|
|
@ -90,9 +90,26 @@ func (h *handler) mark(k *call, on bool) (*Response, *Error) {
|
|||
|
||||
// starred2 is the favourites of the person: artists, albums and songs, the newest first.
|
||||
func (h *handler) starred2(k *call) (*Response, *Error) {
|
||||
res, bad := h.starredOf(k)
|
||||
if bad != nil {
|
||||
return nil, bad
|
||||
}
|
||||
return &Response{Starred2: res}, nil
|
||||
}
|
||||
|
||||
// starred is the same for the apps that ask the older way.
|
||||
func (h *handler) starred(k *call) (*Response, *Error) {
|
||||
res, bad := h.starredOf(k)
|
||||
if bad != nil {
|
||||
return nil, bad
|
||||
}
|
||||
return &Response{Starred: res}, nil
|
||||
}
|
||||
|
||||
func (h *handler) starredOf(k *call) (*SearchResult, *Error) {
|
||||
res := &SearchResult{}
|
||||
if h.Favorites == nil {
|
||||
return &Response{Starred2: res}, nil
|
||||
return res, nil
|
||||
}
|
||||
all, err := h.Favorites.IDs(k.ctx, k.principal.UserID)
|
||||
if err != nil {
|
||||
|
|
@ -119,7 +136,7 @@ func (h *handler) starred2(k *call) (*Response, *Error) {
|
|||
if res.Song, err = h.songs(k, tracks); err != nil {
|
||||
return nil, failure(err)
|
||||
}
|
||||
return &Response{Starred2: res}, nil
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// artistsByID reads artists by their ids, in the order of the ids.
|
||||
|
|
|
|||
Loading…
Reference in a new issue