Use axios params on GET rather than string concat

This commit is contained in:
simojenki
2021-03-01 18:02:39 +11:00
parent 333c9eace9
commit cedd31d8a5
3 changed files with 37 additions and 19 deletions

View File

@@ -39,17 +39,29 @@ export class Navidrome implements MusicService {
this.encryption = encryption; this.encryption = encryption;
} }
generateToken = async ({ username, password }: Credentials) => { get = async (
{ username, password }: Credentials,
path: string,
q: {} = {}
): Promise<SubsonicResponse> => {
const s = randomString(); const s = randomString();
return axios return axios
.get( .get(`${this.url}${path}`, {
`${this.url}/rest/ping.view?u=${username}&t=${t( params: {
password, ...q,
s u: username,
)}&s=${s}&v=1.16.1.0&c=bonob` t: t(password, s),
) s: s,
v: "1.16.1",
c: "bonob",
},
})
.then((response) => new X2JS().xml2js(response.data) as SubconicEnvelope) .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) => { .then((json) => {
if (isError(json)) throw json.error._message; if (isError(json)) throw json.error._message;
else return json; else return json;
@@ -57,12 +69,10 @@ export class Navidrome implements MusicService {
.then((_) => { .then((_) => {
return { return {
authToken: Buffer.from( authToken: Buffer.from(
JSON.stringify( JSON.stringify(this.encryption.encrypt(JSON.stringify(credentials)))
this.encryption.encrypt(JSON.stringify({ username, password }))
)
).toString("base64"), ).toString("base64"),
userId: username, userId: credentials.username,
nickname: username, nickname: credentials.username,
}; };
}); });
}; };

View File

@@ -22,7 +22,7 @@ describe("InMemoryMusicService", () => {
expect(musicLibrary).toBeDefined(); 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" }; const credentials = { username: "bob", password: "smith" };
service.hasUser(credentials); service.hasUser(credentials);

View File

@@ -51,10 +51,16 @@ describe("navidrome", () => {
expect(token.userId).toEqual(username); expect(token.userId).toEqual(username);
expect(axios.get).toHaveBeenCalledWith( expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/ping.view?u=${username}&t=${t( `${url}/rest/ping.view`,
password, {
salt params: {
)}&s=${salt}&v=1.16.1.0&c=bonob` u: username,
t: t(password, salt),
s: salt,
v: "1.16.1",
c: "bonob",
},
}
); );
}); });
}); });
@@ -68,7 +74,9 @@ describe("navidrome", () => {
</subsonic-response>`, </subsonic-response>`,
}); });
return expect(navidrome.generateToken({ username, password })).rejects.toMatch("Wrong username or password"); return expect(
navidrome.generateToken({ username, password })
).rejects.toMatch("Wrong username or password");
}); });
}); });
}); });