first auth test working

This commit is contained in:
Jeena Paradies 2013-05-22 02:40:52 +02:00
parent 553462209b
commit 422dbd34ff
4 changed files with 140 additions and 71 deletions

View file

@ -9,6 +9,8 @@ function(URI, CryptoJS) {
Hmac.makeAuthHeader = function(url, http_method, mac_key, mac_key_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);
@ -38,6 +40,58 @@ function(URI, CryptoJS) {
'", mac="' + mac + '"';
}
Hmac.makeHawkAuthHeader = function(url, http_method, hawk_id, key, payload, app_id) {
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 = "hawk.1.header\n" // header
+ time_stamp + '\n' // ts
+ nonce + '\n' // nonce
+ http_method.toUpperCase() + '\n' // method
+ url.path() + url.search() + url.hash() + '\n' // request uri
+ url.hostname().toLowerCase() + '\n' // host
+ port + '\n' // port
+ Hmac.calculatePayloadHash(payload) + '\n' // hash
+ '\n' // ext (we don't use it)
var app = "";
if(app_id) {
app = ', app="' + app_id + "'";
normalizedRequestString += app_id + "\n" + // app
'\n'; // dlg should be empty
}
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
hmac.update(normalizedRequestString);
var hash = hmac.finalize();
var mac = hash.toString(CryptoJS.enc.Base64);
return 'Hawk id="' + hawk_id +
'", mac="' + mac +
'", ts="' + time_stamp +
'", nonce="' + nonce + '"' +
app
}
Hmac.calculatePayloadHash = function (payload) {
var hash = CryptoJS.algo.SHA256.create();
hash.update('hawk.1.payload\n');
hash.update('application/vnd.tent.post.v0+json\n');
hash.update(payload || '');
hash.update('\n');
return hash.finalize().toString(CryptoJS.enc.Base64);
},
Hmac.makeid = function(len) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

View file

@ -37,6 +37,14 @@ define(function() {
}
}
HostApp.setServerUrls = function(server_urls) {
HostApp.setStringForKey(JSON.stringify(server_urls), "server_urls");
}
HostApp.serverUrl = function(key) {
return JSON.parse(HostApp.stringForKey("server_urls"))[key];
}
HostApp.openURL = function(url) {
if (OS_TYPE == "mac") {

View file

@ -25,7 +25,7 @@ function(jQuery, HostApp, Hmac, Cache) {
Paths.getURL = function(url, http_method, callback, data, auth_header, accepts) {
if(accepts !== false) accepts = accepts || "application/vnd.tent.v0+json; charset=utf-8";
if(accepts !== false) accepts = accepts || "application/vnd.tent.post.v0+json";
var options = {
@ -56,7 +56,7 @@ function(jQuery, HostApp, Hmac, Cache) {
}
},
url: url,
contentType: "application/vnd.tent.v0+json",
contentType: 'application/vnd.tent.post.v0+json; type="https://tent.io/types/app/v0#"',
type: http_method,
complete: callback,
data: data,
@ -65,7 +65,7 @@ function(jQuery, HostApp, Hmac, Cache) {
console.error("getURL (" + xhr.status + ")" + xhr.statusText + " " + http_method + " (" + url + "): '" + xhr.responseText + "'");
}
}
debug(url)
jQuery.ajax(options);
}
@ -127,7 +127,7 @@ function(jQuery, HostApp, Hmac, Cache) {
if(profile_urls.length > 0) {
var profile_url = profile_urls[0];
if (!profile_url.startsWith("http")) {
profile_url = entity + "/profile";
profile_url = entity + profile_url;
}
}
@ -140,13 +140,13 @@ function(jQuery, HostApp, Hmac, Cache) {
if (resp.status >= 200 && resp.status < 300) {
var doc = document.implementation.createHTMLDocument("");
doc.documentElement.innerHTML = resp.responseText;
var links = $(doc).find("link[rel='https://tent.io/rels/profile']");
var links = $(doc).find("link[rel='https://tent.io/rels/meta-post']");
if (links.length > 0) {
var href = links.get(0).href;
Paths.cache.profile_urls.setItem(entity, href);
if (!href.startsWith("http")) {
href = entity + "/profile";
href = entity + href;
}
callback(href);
@ -184,6 +184,11 @@ function(jQuery, HostApp, Hmac, Cache) {
}
Paths.parseHeaderForProfiles = function(header_string) {
var regexp = /https:\/\/tent.io\/rels\/meta-post/i;
return Paths.parseHeaderForLink(header_string, regexp);
}
Paths.parseHeaderForLink = function(header_string, match) {
var headers = header_string.split(/\n/);
var links = [];
for (var i = 0; i < headers.length; i++) {
@ -197,18 +202,18 @@ function(jQuery, HostApp, Hmac, Cache) {
for (var i = 0; i < links.length; i++) {
items = items.concat(links[i].split(","));
}
var profiles = [];
var things = [];
for (var i = 0; i < items.length; i++) {
var item = items[i];
if (item.match(/https:\/\/tent.io\/rels\/profile/i)) {
if (item.match(match)) {
var n = item.match(/<([^>]*)>/);
if (n) {
profiles.push(n[1]);
things.push(n[1]);
}
}
}
return profiles;
return things;
}
return Paths;