More refactoring

This commit is contained in:
Mikołaj Pich 2020-03-13 23:22:07 +01:00
parent 6dcccdddcc
commit 79adad04c1
2 changed files with 15 additions and 14 deletions

View file

@ -13,7 +13,7 @@ module.exports.requestHooks = [
async (context) => {
if (!context.request.hasHeader('PrivateKey')) return;
const pkey = "-----BEGIN PRIVATE KEY-----\n" + getOrThrow(context, "PrivateKey") + "\n-----END PRIVATE KEY-----";
const pkey = getOrThrow(context, "PrivateKey");
const fingerprint = getOrThrow(context, "Fingerprint");
const deviceModel = getOrThrow(context, "DeviceModel");
const firebaseToken = getOrThrow(context, "FirebaseToken");
@ -23,19 +23,22 @@ module.exports.requestHooks = [
const timestampStrHeader = new Date(timestamp + 1000).toUTCString();
const body = getWrappedBody(context.request.getMethod(), context.request.getBodyText(), fingerprint, firebaseToken, timestamp);
const {signature, digest, canonicalUrl} = getSignatureValues(fingerprint, pkey, body, context.request.getUrl(), timestamp);
// set body
context.request.setBodyText(body);
const {digest, signature, headers, keyId, canonicalUrl} = getSignatureValues(fingerprint, pkey, body, context.request.getUrl(), timestamp);
// set headers
context.request.setHeader('User-Agent', 'okhttp/3.11.0');
context.request.setHeader('vOS', 'Android');
context.request.setHeader('vDeviceModel', deviceModel);
context.request.setHeader('vAPI', 1);
context.request.setHeader('vDate', timestampStrHeader);
context.request.setHeader('vCanonicalUrl', canonicalUrl);
context.request.setHeader('Signature', `keyId="${keyId}",headers="${headers}",algorithm="sha256withrsa",signature=Base64(SHA256withRSA(${signature}))`);
if (body != null) context.request.setHeader('Digest', `SHA-256=${digest}`);
context.request.setHeader('Signature', signature);
if (body != null) context.request.setHeader('Digest', digest);
// cleaning...
context.request.removeHeader('PrivateKey');
context.request.removeHeader('Fingerprint');
context.request.removeHeader('DeviceModel');
@ -47,7 +50,7 @@ module.exports.requestHooks = [
function getWrappedBody(method, body, fingerprint, firebaseToken, timestamp) {
if (method !== 'POST') {
if (method !== 'GET') throw 'Incorrect request method (GET or POST).';
else return null
else return null;
}
return JSON.stringify({

View file

@ -8,10 +8,10 @@ function getDigest(body) {
return hash.digest("base64");
}
function getSignature(values, pkey) {
function getSignatureValue(values, pkey) {
const sign = aCrypto.createSign('RSA-SHA256');
sign.update(values);
return sign.sign(pkey, "base64");
return sign.sign("-----BEGIN PRIVATE KEY-----\n" + pkey + "\n-----END PRIVATE KEY-----", "base64");
}
function getCanonicalUrl(fullUrl) {
@ -38,14 +38,12 @@ function getSignatureValues(fingerprint, pkey, body, fullUrl, timestamp) {
const canonicalUrl = getCanonicalUrl(fullUrl);
const digest = getDigest(body);
const {headers, values} = getHeadersList(body, digest, canonicalUrl, timestamp);
const signature = getSignature(values, pkey);
const signatureValue = getSignatureValue(values, pkey);
return {
"digest": digest,
"keyId": fingerprint,
"headers": headers,
"signature": signature,
"canonicalUrl": canonicalUrl
"digest": `SHA-256=${digest}`,
"canonicalUrl": canonicalUrl,
"signature": `keyId="${fingerprint}",headers="${headers}",algorithm="sha256withrsa",signature=Base64(SHA256withRSA(${signatureValue}))`
};
}