Move subsonic t and s params into own function

This commit is contained in:
simojenki
2021-03-01 18:06:57 +11:00
parent cedd31d8a5
commit 7a28bc5288

View File

@@ -9,6 +9,14 @@ import randomString from "./random_string";
export const t = (password: string, s: string) => export const t = (password: string, s: string) =>
Md5.hashStr(`${password}${s}`); Md5.hashStr(`${password}${s}`);
export const t_and_s = (password: string) => {
const s = randomString();
return {
t: t(password, s),
s,
};
};
export type SubconicEnvelope = { export type SubconicEnvelope = {
"subsonic-response": SubsonicResponse; "subsonic-response": SubsonicResponse;
}; };
@@ -43,39 +51,32 @@ export class Navidrome implements MusicService {
{ username, password }: Credentials, { username, password }: Credentials,
path: string, path: string,
q: {} = {} q: {} = {}
): Promise<SubsonicResponse> => { ): Promise<SubsonicResponse> =>
const s = randomString(); axios
return axios
.get(`${this.url}${path}`, { .get(`${this.url}${path}`, {
params: { params: {
...q, ...q,
u: username, u: username,
t: t(password, s), ...t_and_s(password),
s: s,
v: "1.16.1", v: "1.16.1",
c: "bonob", 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;
})
.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 => parseToken = (token: string): Credentials =>
JSON.parse( JSON.parse(