2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* Collection of helper function that are Ampache specific.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// NPM imports
|
|
|
|
import jsSHA from "jssha";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build an HMAC token for authentication against Ampache API.
|
|
|
|
*
|
|
|
|
* @param password User password to derive HMAC from.
|
|
|
|
* @return An object with the generated HMAC and time used.
|
|
|
|
*
|
|
|
|
* @remark This builds an HMAC as expected by Ampache API, which is not a
|
|
|
|
* standard HMAC.
|
|
|
|
*/
|
2016-08-10 23:50:23 +02:00
|
|
|
export function buildHMAC(password) {
|
2016-08-10 21:36:11 +02:00
|
|
|
const time = Math.floor(Date.now() / 1000);
|
|
|
|
|
|
|
|
let shaObj = new jsSHA("SHA-256", "TEXT");
|
|
|
|
shaObj.update(password);
|
|
|
|
const key = shaObj.getHash("HEX");
|
|
|
|
|
|
|
|
shaObj = new jsSHA("SHA-256", "TEXT");
|
|
|
|
shaObj.update(time + key);
|
|
|
|
|
|
|
|
return {
|
|
|
|
time: time,
|
2016-08-10 23:50:23 +02:00
|
|
|
passphrase: shaObj.getHash("HEX"),
|
2016-08-10 21:36:11 +02:00
|
|
|
};
|
|
|
|
}
|