import crypto from 'crypto';
import { FS_ID, FS_OA1_API_KEY } from '$env/static/private';
import { v4 as uuidv4 } from 'uuid';
export default class FatSecretOauth1 {
constructor(httpMethod, url, inputParameters) {
this.requestUrl = url;
this.httpMethod = httpMethod.toUpperCase();
this.inputParameters = {
...inputParameters,
format: 'json',
oauth_consumer_key: FS_ID,
oauth_nonce: uuidv4(),
oauth_signature_method: 'HMAC-SHA1',
oauth_timestamp: Math.floor(new Date().getTime()),
oauth_version: '1.0'
};
this.paramString = this.buildRequestParameterString();
this.signature = this.buildSignature();
return { paramString: this.paramString, signature: this.signature };
}
buildSignature() {
let method = encodeURIComponent(this.httpMethod);
let url = encodeURIComponent(this.requestUrl);
let params = encodeURIComponent(this.paramString);
let signature = crypto
.createHmac('sha1', `${FS_OA1_API_KEY}&`) //I missed the "&" after the API_KEY before hashing.
.update(`${method}&${url}&${params}`)
.digest()
.toString('base64');
return encodeURIComponent(signature);
}
buildRequestParameterString() {
let params = '';
Object.entries(this.inputParameters)
.sort()
.forEach((cur) => (params += `&${encodeURI(cur[0])}=${encodeURI(cur[1])}`));
params = params.substring(1);
return params;
}
}