diff --git a/src/navidrome.ts b/src/navidrome.ts index eff4832..3a8172d 100644 --- a/src/navidrome.ts +++ b/src/navidrome.ts @@ -39,17 +39,29 @@ export class Navidrome implements MusicService { this.encryption = encryption; } - generateToken = async ({ username, password }: Credentials) => { + get = async ( + { username, password }: Credentials, + path: string, + q: {} = {} + ): Promise => { const s = randomString(); return axios - .get( - `${this.url}/rest/ping.view?u=${username}&t=${t( - password, - s - )}&s=${s}&v=1.16.1.0&c=bonob` - ) + .get(`${this.url}${path}`, { + params: { + ...q, + u: username, + t: t(password, s), + s: s, + v: "1.16.1", + c: "bonob", + }, + }) .then((response) => new X2JS().xml2js(response.data) as SubconicEnvelope) - .then((json) => json["subsonic-response"]) + .then((json) => json["subsonic-response"]); + }; + + generateToken = async (credentials: Credentials) => { + return this.get(credentials, "/rest/ping.view") .then((json) => { if (isError(json)) throw json.error._message; else return json; @@ -57,12 +69,10 @@ export class Navidrome implements MusicService { .then((_) => { return { authToken: Buffer.from( - JSON.stringify( - this.encryption.encrypt(JSON.stringify({ username, password })) - ) + JSON.stringify(this.encryption.encrypt(JSON.stringify(credentials))) ).toString("base64"), - userId: username, - nickname: username, + userId: credentials.username, + nickname: credentials.username, }; }); }; diff --git a/tests/in_memory_music_service.test.ts b/tests/in_memory_music_service.test.ts index 732891b..a42f013 100644 --- a/tests/in_memory_music_service.test.ts +++ b/tests/in_memory_music_service.test.ts @@ -22,7 +22,7 @@ describe("InMemoryMusicService", () => { expect(musicLibrary).toBeDefined(); }); - it.only("should fail with an exception if an invalid token is used", async () => { + it("should fail with an exception if an invalid token is used", async () => { const credentials = { username: "bob", password: "smith" }; service.hasUser(credentials); diff --git a/tests/navidrome.test.ts b/tests/navidrome.test.ts index acc056c..fb38d79 100644 --- a/tests/navidrome.test.ts +++ b/tests/navidrome.test.ts @@ -51,10 +51,16 @@ describe("navidrome", () => { expect(token.userId).toEqual(username); expect(axios.get).toHaveBeenCalledWith( - `${url}/rest/ping.view?u=${username}&t=${t( - password, - salt - )}&s=${salt}&v=1.16.1.0&c=bonob` + `${url}/rest/ping.view`, + { + params: { + u: username, + t: t(password, salt), + s: salt, + v: "1.16.1", + c: "bonob", + }, + } ); }); }); @@ -68,7 +74,9 @@ describe("navidrome", () => { `, }); - return expect(navidrome.generateToken({ username, password })).rejects.toMatch("Wrong username or password"); + return expect( + navidrome.generateToken({ username, password }) + ).rejects.toMatch("Wrong username or password"); }); }); });