Commit broken attachment loader (S3 403s I can't figure out...)
This commit is contained in:
parent
ea1bd535dc
commit
3103eaa192
3 changed files with 72 additions and 8 deletions
44
js/api.js
44
js/api.js
|
@ -29,6 +29,7 @@ URL_CALLS['devices'] = "/v1/devices";
|
||||||
URL_CALLS['keys'] = "/v1/keys";
|
URL_CALLS['keys'] = "/v1/keys";
|
||||||
URL_CALLS['push'] = "/v1/websocket";
|
URL_CALLS['push'] = "/v1/websocket";
|
||||||
URL_CALLS['messages'] = "/v1/messages";
|
URL_CALLS['messages'] = "/v1/messages";
|
||||||
|
URL_CALLS['attachment'] = "/v1/attachments";
|
||||||
|
|
||||||
var API = new function() {
|
var API = new function() {
|
||||||
|
|
||||||
|
@ -116,9 +117,9 @@ var API = new function() {
|
||||||
user : number,
|
user : number,
|
||||||
password : password,
|
password : password,
|
||||||
jsonData : { signalingKey : btoa(getString(signaling_key)),
|
jsonData : { signalingKey : btoa(getString(signaling_key)),
|
||||||
supportsSms : false,
|
supportsSms : false,
|
||||||
fetchesMessages : true,
|
fetchesMessages : true,
|
||||||
registrationId: registrationId},
|
registrationId : registrationId},
|
||||||
}).then(function(response) {
|
}).then(function(response) {
|
||||||
if (success_callback !== undefined)
|
if (success_callback !== undefined)
|
||||||
success_callback(response);
|
success_callback(response);
|
||||||
|
@ -182,5 +183,40 @@ var API = new function() {
|
||||||
do_auth : true,
|
do_auth : true,
|
||||||
jsonData : jsonData,
|
jsonData : jsonData,
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
|
this.getAttachment = function(id) {
|
||||||
|
return doAjax({
|
||||||
|
call : 'attachment',
|
||||||
|
httpType : 'GET',
|
||||||
|
urlParameters : '/' + id,
|
||||||
|
do_auth : true,
|
||||||
|
}).then(function(response) {
|
||||||
|
console.log(response);
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
$.ajax(response.location, {
|
||||||
|
type : "GET",
|
||||||
|
xhrFields: {
|
||||||
|
responseType: "arraybuffer"
|
||||||
|
},
|
||||||
|
/*headers: {
|
||||||
|
"Content-Type": "application/octet-stream"
|
||||||
|
},*/
|
||||||
|
|
||||||
|
success : function(response, textStatus, jqXHR) {
|
||||||
|
resolve(response);
|
||||||
|
},
|
||||||
|
|
||||||
|
error : function(jqXHR, textStatus, errorThrown) {
|
||||||
|
var code = jqXHR.status;
|
||||||
|
if (code > 999 || code < 100)
|
||||||
|
code = -1;
|
||||||
|
var e = new Error(code);
|
||||||
|
e.name = "HTTPError";
|
||||||
|
reject(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
}(); // API
|
}(); // API
|
||||||
|
|
18
js/crypto.js
18
js/crypto.js
|
@ -538,10 +538,24 @@ window.crypto = (function() {
|
||||||
|
|
||||||
var iv = decodedMessage.slice(1, 1 + 16);
|
var iv = decodedMessage.slice(1, 1 + 16);
|
||||||
var ciphertext = decodedMessage.slice(1 + 16, decodedMessage.byteLength - 10);
|
var ciphertext = decodedMessage.slice(1 + 16, decodedMessage.byteLength - 10);
|
||||||
var ivAndCipherText = decodedMessage.slice(1, decodedMessage.byteLength - 10);
|
var ivAndCiphertext = decodedMessage.slice(1, decodedMessage.byteLength - 10);
|
||||||
var mac = decodedMessage.slice(decodedMessage.byteLength - 10, decodedMessage.byteLength);
|
var mac = decodedMessage.slice(decodedMessage.byteLength - 10, decodedMessage.byteLength);
|
||||||
|
|
||||||
return verifyMACWithVersionByte(ivAndCipherText, mac_key, mac).then(function() {
|
return verifyMACWithVersionByte(ivAndCiphertext, mac_key, mac).then(function() {
|
||||||
|
return window.crypto.subtle.decrypt({name: "AES-CBC", iv: iv}, aes_key, ciphertext);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
crypto.decryptAttachment = function(encryptedBin, keys) {
|
||||||
|
var aes_key = key.slice(0, 32);
|
||||||
|
var mac_key = key.slice(32, 64);
|
||||||
|
|
||||||
|
var iv = encryptedBin.slice(0, 32);
|
||||||
|
var ciphertext = encryptedBin.slice(32, encryptedBin.byteLength - 32);
|
||||||
|
var ivAndCiphertext = encryptedBin.slice(0, encryptedBin.byteLength - 32);
|
||||||
|
var mac = encryptedBin.slice(encryptedBin.byteLength - 32, encryptedBin.byteLength);
|
||||||
|
|
||||||
|
return verifyMAC(ivAndCiphertext, mac_key, mac).then(function() {
|
||||||
return window.crypto.subtle.decrypt({name: "AES-CBC", iv: iv}, aes_key, ciphertext);
|
return window.crypto.subtle.decrypt({name: "AES-CBC", iv: iv}, aes_key, ciphertext);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -463,8 +463,22 @@ function subscribeToPush(message_callback) {
|
||||||
console.log("Successfully decoded message with id: " + message.id);
|
console.log("Successfully decoded message with id: " + message.id);
|
||||||
socket.send(JSON.stringify({type: 1, id: message.id}));
|
socket.send(JSON.stringify({type: 1, id: message.id}));
|
||||||
return crypto.handleIncomingPushMessageProto(proto).then(function(decrypted) {
|
return crypto.handleIncomingPushMessageProto(proto).then(function(decrypted) {
|
||||||
storeMessage(decrypted);
|
var handleAttachment = function(attachment) {
|
||||||
message_callback(decrypted);
|
return API.getAttachment(attachment.id).then(function(encryptedBin) {
|
||||||
|
return crypto.decryptAttachment(encryptedBin, attachment.key).then(function(decryptedBin) {
|
||||||
|
attachment.decrypted = decryptedBin;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
var promises = [];
|
||||||
|
for (var i = 0; i < decrypted.message.attachments.length; i++) {
|
||||||
|
promises[i] = handleAttachment(decrypted.message.attachments[i]);
|
||||||
|
}
|
||||||
|
return Promise.all(promises).then(function() {
|
||||||
|
storeMessage(decrypted);
|
||||||
|
message_callback(decrypted);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
}).catch(function(e) {
|
}).catch(function(e) {
|
||||||
console.log("Error handling incoming message: ");
|
console.log("Error handling incoming message: ");
|
||||||
|
|
Loading…
Add table
Reference in a new issue