more work on posting and showing status posts
This commit is contained in:
parent
f73ce7d196
commit
650f107d21
7 changed files with 108 additions and 135 deletions
|
@ -10,6 +10,7 @@ function(HostApp, Timeline, URI, APICalls, Core) {
|
||||||
|
|
||||||
|
|
||||||
function Mentions() {
|
function Mentions() {
|
||||||
|
return // FIXME
|
||||||
|
|
||||||
this.is_not_init = false;
|
this.is_not_init = false;
|
||||||
this.unread_mentions = 0;
|
this.unread_mentions = 0;
|
||||||
|
|
|
@ -87,31 +87,40 @@ function(HostApp, APICalls, Hmac) {
|
||||||
|
|
||||||
Oauth.prototype.register = function (url) {
|
Oauth.prototype.register = function (url) {
|
||||||
var those = this;
|
var those = this;
|
||||||
APICalls.get(url, { callback: function(resp) {
|
debug(url)
|
||||||
|
APICalls.get(url, {
|
||||||
|
no_auth: true,
|
||||||
|
callback: function(resp) {
|
||||||
|
|
||||||
those.profile = JSON.parse(resp.responseText);
|
those.profile = JSON.parse(resp.responseText);
|
||||||
those.entity = those.profile.content.entity;
|
those.entity = those.profile.content.entity;
|
||||||
HostApp.setStringForKey(those.entity, "entity")
|
HostApp.setStringForKey(those.entity, "entity")
|
||||||
HostApp.setServerUrls(those.profile.content.servers[0].urls);
|
HostApp.setServerUrls(those.profile.content.servers[0].urls);
|
||||||
|
|
||||||
var callback = function(resp) {
|
APICalls.post(HostApp.serverUrl("new_post"), JSON.stringify(those.app_info), {
|
||||||
|
content_type: "https://tent.io/types/app/v0#",
|
||||||
|
no_auth: true,
|
||||||
|
callback: function(resp) {
|
||||||
|
|
||||||
var app_id = JSON.parse(resp.responseText).id;
|
var app_id = JSON.parse(resp.responseText).id;
|
||||||
var header_string = resp.getAllResponseHeaders();
|
var header_string = resp.getAllResponseHeaders();
|
||||||
var regexp = /https:\/\/tent.io\/rels\/credentials/i
|
var regexp = /https:\/\/tent.io\/rels\/credentials/i
|
||||||
var url = APICalls.parseHeaderForLink(header_string, regexp);
|
var url = APICalls.parseHeaderForLink(header_string, regexp);
|
||||||
APICalls.http_call(url, "GET", function(resp) {
|
|
||||||
var data = JSON.parse(resp.responseText);
|
|
||||||
those.authRequest(data, app_id);
|
|
||||||
}, null, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
APICalls.post(HostApp.serverUrl("new_post"), JSON.stringify(those.app_info), {callback: callback});
|
APICalls.get(url, {
|
||||||
|
content_type: "https://tent.io/types/app/v0#",
|
||||||
|
no_auth: true,
|
||||||
|
callback: function(resp) {
|
||||||
|
var data = JSON.parse(resp.responseText);
|
||||||
|
those.authRequest(data, app_id);
|
||||||
|
}});
|
||||||
|
}});
|
||||||
|
|
||||||
}});
|
}});
|
||||||
}
|
}
|
||||||
|
|
||||||
Oauth.prototype.authRequest = function(credentials, app_id) {
|
Oauth.prototype.authRequest = function(credentials, app_id) {
|
||||||
|
|
||||||
HostApp.setStringForKey(app_id, "app_id");
|
HostApp.setStringForKey(app_id, "app_id");
|
||||||
HostApp.setStringForKey(credentials.id, "app_hawk_id");
|
HostApp.setStringForKey(credentials.id, "app_hawk_id");
|
||||||
HostApp.setStringForKey(credentials.content.hawk_key, "app_hawk_key");
|
HostApp.setStringForKey(credentials.content.hawk_key, "app_hawk_key");
|
||||||
|
@ -137,20 +146,20 @@ function(HostApp, APICalls, Hmac) {
|
||||||
});
|
});
|
||||||
|
|
||||||
var those = this;
|
var those = this;
|
||||||
var http_method = "POST";
|
|
||||||
var callback = function(resp) {
|
|
||||||
those.requestAccessTokenTicketFinished(resp.responseText);
|
|
||||||
};
|
|
||||||
|
|
||||||
var auth_header = Hmac.makeHawkAuthHeader(
|
var auth_header = Hmac.makeHawkAuthHeader(
|
||||||
url,
|
url,
|
||||||
http_method,
|
"POST",
|
||||||
HostApp.stringForKey("app_hawk_id"),
|
HostApp.stringForKey("app_hawk_id"),
|
||||||
HostApp.stringForKey("app_hawk_key"),
|
HostApp.stringForKey("app_hawk_key"),
|
||||||
requestBody
|
requestBody
|
||||||
);
|
);
|
||||||
|
|
||||||
APICalls.http_call(url, http_method, callback, requestBody, auth_header);
|
APICalls.post(url, requestBody, {
|
||||||
|
content_type: "https://tent.io/types/app/v0#",
|
||||||
|
auth_header: auth_header,
|
||||||
|
callback: function(resp) {
|
||||||
|
those.requestAccessTokenTicketFinished(resp.responseText);
|
||||||
|
}});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
console.error("State is not the same: {" + this.state + "} vs {" + urlVars["state"] + "}")
|
console.error("State is not the same: {" + this.state + "} vs {" + urlVars["state"] + "}")
|
||||||
|
@ -162,6 +171,7 @@ function(HostApp, APICalls, Hmac) {
|
||||||
Oauth.prototype.requestAccessTokenTicketFinished = function(responseBody) {
|
Oauth.prototype.requestAccessTokenTicketFinished = function(responseBody) {
|
||||||
|
|
||||||
var access = JSON.parse(responseBody);
|
var access = JSON.parse(responseBody);
|
||||||
|
debug(access)
|
||||||
|
|
||||||
HostApp.setStringForKey(access["access_token"], "user_access_token");
|
HostApp.setStringForKey(access["access_token"], "user_access_token");
|
||||||
HostApp.setSecret(access["hawk_key"]);
|
HostApp.setSecret(access["hawk_key"]);
|
||||||
|
|
|
@ -51,7 +51,7 @@ function(HostApp, APICalls, Cache) {
|
||||||
document.body.className = "body-timeline";
|
document.body.className = "body-timeline";
|
||||||
document.body.id = "with-sidebar";
|
document.body.id = "with-sidebar";
|
||||||
|
|
||||||
this.setEntityAvatar();
|
//this.setEntityAvatar(); FIXME
|
||||||
this.setOnScroll();
|
this.setOnScroll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ function(Core, APICalls, HostApp, URI) {
|
||||||
document.getElementById("content").appendChild(this.container);
|
document.getElementById("content").appendChild(this.container);
|
||||||
|
|
||||||
var _this = this;
|
var _this = this;
|
||||||
this.reloadIntervall = setInterval(function() { _this.getNewData() }, this.timeout);
|
//this.reloadIntervall = setInterval(function() { _this.getNewData() }, this.timeout); //FIXME back
|
||||||
|
|
||||||
this.getNewData();
|
this.getNewData();
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,7 @@ function(Core, APICalls, HostApp, URI) {
|
||||||
|
|
||||||
Timeline.prototype.newStatus = function(statuses, append) {
|
Timeline.prototype.newStatus = function(statuses, append) {
|
||||||
|
|
||||||
|
statuses = statuses.data;
|
||||||
if(statuses != null && statuses.length > 0) {
|
if(statuses != null && statuses.length > 0) {
|
||||||
|
|
||||||
this.before.loading = false;
|
this.before.loading = false;
|
||||||
|
@ -62,7 +63,7 @@ function(Core, APICalls, HostApp, URI) {
|
||||||
this.since_id_entity = status.entity;
|
this.since_id_entity = status.entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status.type == "https://tent.io/types/post/status/v0.1.0" || status.type == "https://tent.io/types/post/photo/v0.1.0") {
|
if (status.type == "https://tent.io/types/status/v0#" || status.type == "https://tent.io/types/post/photo/v0.1.0") {
|
||||||
|
|
||||||
var new_node = this.getStatusDOMElement(status);
|
var new_node = this.getStatusDOMElement(status);
|
||||||
|
|
||||||
|
@ -100,15 +101,16 @@ function(Core, APICalls, HostApp, URI) {
|
||||||
add_to_search = add_to_search || {};
|
add_to_search = add_to_search || {};
|
||||||
|
|
||||||
var those = this;
|
var those = this;
|
||||||
var url = URI(APICalls.mkApiRootPath("/posts"));
|
var url = URI(HostApp.serverUrl("posts_feed"));
|
||||||
|
|
||||||
var post_types = [
|
var post_types = [
|
||||||
"https://tent.io/types/post/repost/v0.1.0",
|
"https://tent.io/types/status/v0#",
|
||||||
"https://tent.io/types/post/status/v0.1.0",
|
"https://tent.io/types/status/v0#reply",
|
||||||
"https://tent.io/types/post/delete/v0.1.0",
|
"https://tent.io/types/repost/v0#",
|
||||||
"https://tent.io/types/post/photo/v0.1.0"
|
"https://tent.io/types/delete/v0#",
|
||||||
|
//"https://tent.io/types/post/photo/v0.1.0"
|
||||||
];
|
];
|
||||||
url.addSearch("post_types", post_types.join(","));
|
//url.addSearch("types", post_types.join(","));
|
||||||
//url.addSearch("sort_by", "published_at");
|
//url.addSearch("sort_by", "published_at");
|
||||||
url.addSearch("limit", this.posts_limit);
|
url.addSearch("limit", this.posts_limit);
|
||||||
|
|
||||||
|
@ -127,7 +129,6 @@ function(Core, APICalls, HostApp, URI) {
|
||||||
those.reload_blocked = false;
|
those.reload_blocked = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
var json = JSON.parse(resp.responseText);
|
var json = JSON.parse(resp.responseText);
|
||||||
those.newStatus(json, append);
|
those.newStatus(json, append);
|
||||||
|
|
||||||
|
@ -143,7 +144,8 @@ function(Core, APICalls, HostApp, URI) {
|
||||||
|
|
||||||
if (!this.reload_blocked) {
|
if (!this.reload_blocked) {
|
||||||
this.reload_blocked = true;
|
this.reload_blocked = true;
|
||||||
APICalls.http_call(url.toString(), http_method, callback, data); // FIXME: error callback
|
// APICalls.http_call(url.toString(), http_method, callback, data); // FIXME: error callback
|
||||||
|
APICalls.get(url.toString(), { callback: callback });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,80 +22,47 @@ function(jQuery, HostApp, Hmac, Cache) {
|
||||||
}
|
}
|
||||||
return vars;
|
return vars;
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
APICalls.http_call = function(url, http_method, callback, data, auth_header, accepts) {
|
|
||||||
|
|
||||||
if(accepts !== false) accepts = accepts || "application/vnd.tent.post.v0+json";
|
|
||||||
|
|
||||||
var options = {
|
|
||||||
|
|
||||||
beforeSend: function(xhr) {
|
|
||||||
|
|
||||||
if(accepts !== false) xhr.setRequestHeader("Accept", accepts);
|
|
||||||
|
|
||||||
if (data) xhr.setRequestHeader("Content-Length", data.length);
|
|
||||||
|
|
||||||
if (auth_header) { // if is_set? auth_header
|
|
||||||
|
|
||||||
xhr.setRequestHeader("Authorization", auth_header);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
var user_access_token = HostApp.stringForKey("user_access_token");
|
|
||||||
|
|
||||||
if (auth_header !== false && typeof user_access_token != "undefined") {
|
|
||||||
|
|
||||||
auth_header = Hmac.makeAuthHeader(
|
|
||||||
url,
|
|
||||||
http_method,
|
|
||||||
HostApp.secret(),
|
|
||||||
user_access_token
|
|
||||||
);
|
|
||||||
xhr.setRequestHeader("Authorization", auth_header);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
url: url,
|
|
||||||
contentType: 'application/vnd.tent.post.v0+json; type="https://tent.io/types/app/v0#"',
|
|
||||||
type: http_method,
|
|
||||||
complete: callback,
|
|
||||||
data: data,
|
|
||||||
processData: false,
|
|
||||||
error: function(xhr, ajaxOptions, thrownError) {
|
|
||||||
console.error("getURL (" + xhr.status + ")" + xhr.statusText + " " + http_method + " (" + url + "): '" + xhr.responseText + "'");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
debug(url)
|
|
||||||
jQuery.ajax(options);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
APICalls.http_call = function(options) {
|
APICalls.http_call = function(options) {
|
||||||
|
|
||||||
if(!options.content_type) {
|
if (typeof options === "string") {
|
||||||
|
console.error(options + " not implemented yet")
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var content_type = null;
|
||||||
|
|
||||||
|
if(options.http_method == "POST" && !options.content_type) {
|
||||||
console.error("No content type for " + options.url);
|
console.error("No content type for " + options.url);
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
content_type = "application/vnd.tent.post.v0+json; type=\"" + options.content_type + "\"";
|
||||||
}
|
}
|
||||||
|
|
||||||
var settings = {
|
var settings = {
|
||||||
beforeSend: function(xhr) {
|
beforeSend: function(xhr) {
|
||||||
if (options.data) xhr.setRequestHeader("Content-Length", data.length);
|
if (options.data) xhr.setRequestHeader("Content-Length", options.data.length);
|
||||||
if (options.accept) xhr.setRequestHeader("Accept", "application/vnd.tent.post.v0+json");
|
if (options.accept) xhr.setRequestHeader("Accept", options.accept);
|
||||||
|
else xhr.setRequestHeader("Accept", "application/vnd.tent.post.v0+json");
|
||||||
var user_access_token = HostApp.stringForKey("user_access_token");
|
var user_access_token = HostApp.stringForKey("user_access_token");
|
||||||
if (!no_auth && user_access_token) {
|
if (!options.auth_header && !options.no_auth && user_access_token) {
|
||||||
var auth_header = Hmac.makeHawkAuthHeader(
|
var auth_header = Hmac.makeHawkAuthHeader(
|
||||||
options.url,
|
options.url,
|
||||||
options.http_method,
|
options.http_method,
|
||||||
HostApp.secret(),
|
user_access_token,
|
||||||
user_access_token
|
HostApp.secret()//,
|
||||||
|
//HostApp.stringForKey("app_id")
|
||||||
);
|
);
|
||||||
xhr.setRequestHeader("Authorization", auth_header);
|
xhr.setRequestHeader("Authorization", auth_header);
|
||||||
} else {
|
} else if(options.auth_header) {
|
||||||
|
xhr.setRequestHeader("Authorization", options.auth_header);
|
||||||
|
} else if(!options.no_auth) {
|
||||||
console.error("No user_access_token yet - " + options.url);
|
console.error("No user_access_token yet - " + options.url);
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
url: options.url,
|
url: options.url,
|
||||||
contentType: options.content_type,
|
contentType: content_type,
|
||||||
type: url.http_method,
|
type: options.http_method,
|
||||||
complete: options.callback,
|
complete: options.callback,
|
||||||
data: options.data,
|
data: options.data,
|
||||||
processData: false,
|
processData: false,
|
||||||
|
@ -111,13 +78,11 @@ function(jQuery, HostApp, Hmac, Cache) {
|
||||||
var settings = {
|
var settings = {
|
||||||
url: url,
|
url: url,
|
||||||
http_method: "GET",
|
http_method: "GET",
|
||||||
accept: null,
|
|
||||||
data: null,
|
|
||||||
no_auth: false
|
|
||||||
content_type: null
|
|
||||||
};
|
};
|
||||||
|
|
||||||
jQuery.extend(settings, options);
|
for (var key in options) {
|
||||||
|
settings[key] = options[key];
|
||||||
|
}
|
||||||
|
|
||||||
APICalls.http_call(settings);
|
APICalls.http_call(settings);
|
||||||
}
|
}
|
||||||
|
@ -129,7 +94,9 @@ function(jQuery, HostApp, Hmac, Cache) {
|
||||||
data: data
|
data: data
|
||||||
};
|
};
|
||||||
|
|
||||||
jQuery.extend(settings, options);
|
for (var key in options) {
|
||||||
|
settings[key] = options[key];
|
||||||
|
}
|
||||||
|
|
||||||
APICalls.http_call(settings);
|
APICalls.http_call(settings);
|
||||||
}
|
}
|
||||||
|
|
|
@ -162,7 +162,28 @@ function(jQuery, APICalls, URI, HostApp, Cache) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Core.prototype.getStatusDOMElement = function(status) {
|
Core.prototype.getStatusDOMElement = function(status) {
|
||||||
|
/*
|
||||||
|
{
|
||||||
|
"app": {
|
||||||
|
"id": "P8FJjaiRv0AKXfjUMd_4YQ",
|
||||||
|
"name": "Bungloo on Linux",
|
||||||
|
"url": "http:\/\/jabs.nu\/bungloo\/"
|
||||||
|
},
|
||||||
|
"content": {
|
||||||
|
"text": "jeena test"
|
||||||
|
},
|
||||||
|
"entity": "http:\/\/155969d81672.alpha.attic.is",
|
||||||
|
"id": "HlSXe8MREzU4h2fGLGSnCA",
|
||||||
|
"published_at": 1369566009,
|
||||||
|
"received_at": 1369566008799,
|
||||||
|
"type": "https:\/\/tent.io\/types\/status\/v0#",
|
||||||
|
"version": {
|
||||||
|
"id": "a2f702b4615c7d7dd0f98c73d7b55749880bf6e437a77349454ff10745d134c6",
|
||||||
|
"published_at": 1369566009,
|
||||||
|
"received_at": 1369566008799
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
var _this = this;
|
var _this = this;
|
||||||
|
|
||||||
var template = this.getTemplate();
|
var template = this.getTemplate();
|
||||||
|
@ -320,6 +341,7 @@ function(jQuery, APICalls, URI, HostApp, Cache) {
|
||||||
|
|
||||||
this.findMentions(template.message, status.mentions);
|
this.findMentions(template.message, status.mentions);
|
||||||
|
|
||||||
|
/*
|
||||||
for (var i = 0; i < status.mentions.length; i++) {
|
for (var i = 0; i < status.mentions.length; i++) {
|
||||||
var mention = status.mentions[i];
|
var mention = status.mentions[i];
|
||||||
if (mention.entity == HostApp.stringForKey("entity")) {
|
if (mention.entity == HostApp.stringForKey("entity")) {
|
||||||
|
@ -327,10 +349,10 @@ function(jQuery, APICalls, URI, HostApp, Cache) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
var published_at = typeof status.__repost == "undefined" ? status.published_at : status.__repost.published_at;
|
var published_at = typeof status.__repost == "undefined" ? status.version.published_at : status.__repost.published_at;
|
||||||
var time = document.createElement("abbr");
|
var time = document.createElement("abbr");
|
||||||
time.innerText = this.ISODateString(new Date(published_at * 1000));
|
time.innerText = this.ISODateString(new Date(published_at));
|
||||||
time.title = time.innerText;
|
time.title = time.innerText;
|
||||||
time.className = "timeago";
|
time.className = "timeago";
|
||||||
jQuery(time).timeago();
|
jQuery(time).timeago();
|
||||||
|
@ -483,13 +505,13 @@ function(jQuery, APICalls, URI, HostApp, Cache) {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
var url = URI(APICalls.mkApiRootPath("/posts"));
|
var url = URI(HostApp.serverUrl("new_post"));
|
||||||
|
|
||||||
var http_method = "POST";
|
var http_method = "POST";
|
||||||
|
|
||||||
var data = {
|
var data = {
|
||||||
"type": "https://tent.io/types/post/status/v0.1.0",
|
"type": in_reply_to_status_id ? "https://tent.io/types/status/v0#" : "https://tent.io/types/status/v0#reply",
|
||||||
"published_at": parseInt(new Date().getTime() / 1000, 10),
|
"published_at": parseInt(new Date().getTime(), 10),
|
||||||
"permissions": {
|
"permissions": {
|
||||||
"public": !is_private
|
"public": !is_private
|
||||||
},
|
},
|
||||||
|
@ -517,7 +539,11 @@ function(jQuery, APICalls, URI, HostApp, Cache) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
APICalls.http_call(url.toString(), http_method, callback, JSON.stringify(data));
|
// APICalls.http_call(url.toString(), http_method, callback, JSON.stringify(data));
|
||||||
|
APICalls.post(url.toString(), JSON.stringify(data), {
|
||||||
|
content_type: data.type,
|
||||||
|
callback: callback
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,40 +7,7 @@ function(URI, CryptoJS) {
|
||||||
|
|
||||||
var Hmac = {};
|
var Hmac = {};
|
||||||
|
|
||||||
Hmac.makeAuthHeader = function(url, http_method, mac_key, mac_key_id) {
|
Hmac.makeHawkAuthHeader = function(url, http_method, hawk_id, key, app_id) {
|
||||||
|
|
||||||
debug("makeAuthHeader should not be used anymore, bug!")
|
|
||||||
|
|
||||||
url = URI(url);
|
|
||||||
var nonce = Hmac.makeid(8);
|
|
||||||
var time_stamp = parseInt((new Date).getTime() / 1000, 10);
|
|
||||||
|
|
||||||
var port = url.port();
|
|
||||||
if (!port) {
|
|
||||||
port = url.protocol() == "https" ? "443" : "80";
|
|
||||||
}
|
|
||||||
|
|
||||||
var normalizedRequestString = ""
|
|
||||||
+ time_stamp + '\n'
|
|
||||||
+ nonce + '\n'
|
|
||||||
+ http_method + '\n'
|
|
||||||
+ url.path() + url.search() + url.hash() + '\n'
|
|
||||||
+ url.hostname() + '\n'
|
|
||||||
+ port + '\n'
|
|
||||||
+ '\n' ;
|
|
||||||
|
|
||||||
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, mac_key);
|
|
||||||
hmac.update(normalizedRequestString);
|
|
||||||
var hash = hmac.finalize();
|
|
||||||
var mac = hash.toString(CryptoJS.enc.Base64);
|
|
||||||
|
|
||||||
return 'MAC id="' + mac_key_id +
|
|
||||||
'", ts="' + time_stamp +
|
|
||||||
'", nonce="' + nonce +
|
|
||||||
'", mac="' + mac + '"';
|
|
||||||
}
|
|
||||||
|
|
||||||
Hmac.makeHawkAuthHeader = function(url, http_method, hawk_id, key, payload, app_id) {
|
|
||||||
|
|
||||||
url = URI(url);
|
url = URI(url);
|
||||||
var nonce = Hmac.makeid(8);
|
var nonce = Hmac.makeid(8);
|
||||||
|
@ -58,24 +25,22 @@ function(URI, CryptoJS) {
|
||||||
+ url.path() + url.search() + url.hash() + '\n' // request uri
|
+ url.path() + url.search() + url.hash() + '\n' // request uri
|
||||||
+ url.hostname().toLowerCase() + '\n' // host
|
+ url.hostname().toLowerCase() + '\n' // host
|
||||||
+ port + '\n' // port
|
+ port + '\n' // port
|
||||||
+ Hmac.calculatePayloadHash(payload) + '\n' // hash
|
+ '\n' // Hmac.calculatePayloadHash(payload) + '\n' // hash // FIXME implement payload validation
|
||||||
+ '\n' // ext (we don't use it)
|
+ '\n' // ext (we don't use it)
|
||||||
|
|
||||||
var app = "";
|
var app = "";
|
||||||
|
|
||||||
if(app_id) {
|
if(app_id) {
|
||||||
app = ', app="' + app_id + "'";
|
app = ', app="' + app_id + "'";
|
||||||
normalizedRequestString += app_id + "\n" + // app
|
normalizedRequestString += app_id + "\n" + // app
|
||||||
'\n'; // dlg should be empty
|
'\n'; // dlg should be empty
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
|
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
|
||||||
hmac.update(normalizedRequestString);
|
hmac.update(normalizedRequestString);
|
||||||
var hash = hmac.finalize();
|
var hash = hmac.finalize();
|
||||||
var mac = hash.toString(CryptoJS.enc.Base64);
|
var mac = hash.toString(CryptoJS.enc.Base64);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return 'Hawk id="' + hawk_id +
|
return 'Hawk id="' + hawk_id +
|
||||||
'", mac="' + mac +
|
'", mac="' + mac +
|
||||||
'", ts="' + time_stamp +
|
'", ts="' + time_stamp +
|
||||||
|
@ -84,6 +49,8 @@ function(URI, CryptoJS) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Hmac.calculatePayloadHash = function (payload) {
|
Hmac.calculatePayloadHash = function (payload) {
|
||||||
|
if (!payload) return "";
|
||||||
|
|
||||||
var hash = CryptoJS.algo.SHA256.create();
|
var hash = CryptoJS.algo.SHA256.create();
|
||||||
hash.update('hawk.1.payload\n');
|
hash.update('hawk.1.payload\n');
|
||||||
hash.update('application/vnd.tent.post.v0+json\n');
|
hash.update('application/vnd.tent.post.v0+json\n');
|
||||||
|
|
Reference in a new issue