From 7a28bc5288aaf457488000626f0514e1ea65d169 Mon Sep 17 00:00:00 2001 From: simojenki Date: Mon, 1 Mar 2021 18:06:57 +1100 Subject: [PATCH] Move subsonic t and s params into own function --- src/navidrome.ts | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/navidrome.ts b/src/navidrome.ts index 3a8172d..2ddc918 100644 --- a/src/navidrome.ts +++ b/src/navidrome.ts @@ -9,6 +9,14 @@ import randomString from "./random_string"; export const t = (password: string, s: string) => Md5.hashStr(`${password}${s}`); +export const t_and_s = (password: string) => { + const s = randomString(); + return { + t: t(password, s), + s, + }; +}; + export type SubconicEnvelope = { "subsonic-response": SubsonicResponse; }; @@ -43,39 +51,32 @@ export class Navidrome implements MusicService { { username, password }: Credentials, path: string, q: {} = {} - ): Promise => { - const s = randomString(); - return axios + ): Promise => + axios .get(`${this.url}${path}`, { params: { ...q, u: username, - t: t(password, s), - s: s, + ...t_and_s(password), v: "1.16.1", c: "bonob", }, }) .then((response) => new X2JS().xml2js(response.data) as SubconicEnvelope) - .then((json) => json["subsonic-response"]); - }; - - generateToken = async (credentials: Credentials) => { - return this.get(credentials, "/rest/ping.view") + .then((json) => json["subsonic-response"]) .then((json) => { if (isError(json)) throw json.error._message; else return json; - }) - .then((_) => { - return { - authToken: Buffer.from( - JSON.stringify(this.encryption.encrypt(JSON.stringify(credentials))) - ).toString("base64"), - userId: credentials.username, - nickname: credentials.username, - }; }); - }; + + generateToken = async (credentials: Credentials) => + this.get(credentials, "/rest/ping.view").then((_) => ({ + authToken: Buffer.from( + JSON.stringify(this.encryption.encrypt(JSON.stringify(credentials))) + ).toString("base64"), + userId: credentials.username, + nickname: credentials.username, + })); parseToken = (token: string): Credentials => JSON.parse(