Commit 549646e9 authored by Michael Tross's avatar Michael Tross

Merge branch 'oauth' into 'master'

Implemented oauth based authentication



See merge request !2
parents dd4b17e0 326497b1
var g_vdcs = {};
var g_timeout_id;
var g_auth = { "dSUID": undefined, "code": undefined };
var vdc_strings =
{
......@@ -12,7 +13,7 @@ var vdc_strings =
var vdc_config =
{
model_uid: "libdsvdc:netatmo",
search_intervall_ms: 20000
search_intervall_ms: 20000
};
var vdc_render_no_devices = function()
......@@ -23,6 +24,28 @@ var vdc_render_no_devices = function()
};
var getURLParameter = function getURLParameter(p)
{
var url = decodeURIComponent(window.location.search.substring(1));
var variables = url.split('&');
var name;
var i;
for (i = 0; i < variables.length; i++)
{
name = variables[i].split('=');
if (name[0] === p) {
return name[1] === undefined ? true : name[1];
}
}
};
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
var vdc_clear_search_timeout = function()
{
if (g_timeout_id !== undefined)
......@@ -33,7 +56,7 @@ var vdc_clear_search_timeout = function()
var vdc_start_search_timeout = function()
{
g_timeout_id = window.setTimeout(vdc_search,
g_timeout_id = window.setTimeout(vdc_search,
vdc_config.search_intervall_ms);
};
......@@ -57,7 +80,7 @@ var vdc_show_message = function(title, message, reload)
"<div class=\"right\"><p><button type=\"button\" id=\"reload\">Reload</button></p></div>";
}
$("#modal_dialog_message").html(message);
if (reload === true)
{
$("#reload").click(
......@@ -74,6 +97,39 @@ var vdc_show_message = function(title, message, reload)
window.location.hash = "#open_modal_dialog";
};
var vdc_get_client_id_callback = function(device, data)
{
var i;
var props = data.result.ResponseGetProperty;
var client_id = "";
for (i = 0; i < props.length; i++)
{
var property = props[i];
if (property.name === "x-netatmo-client_id")
{
client_id = property.value;
}
}
if (client_id.length === 0)
{
vdc_show_message("Error",
"Could not get client id for authentication", true);
return;
}
var url = "https://api.netatmo.net/oauth2/authorize?client_id=" +
client_id + "&redirect_uri=" +
encodeURIComponent(window.location.origin + "/" +
window.location.pathname) +
"&state=" + device.dSUID;
$("<form action=\"" + url + "\" method=\"POST\">" +
'<input type="hidden" name="aid">' +
'</form>').appendTo($(document.body)).submit();
}
var vdc_show_password_prompt = function(title, message, callback)
{
$("#modal_dialog_title").html(title);
......@@ -109,7 +165,7 @@ var vdc_show_password_prompt = function(title, message, callback)
$("#savepw").click(cb);
}
window.location.hash = "#open_modal_dialog";
};
......@@ -140,18 +196,15 @@ var vdc_auth_button_clicked = function(dsuid, evt)
"Something went wrong, please reload the page", true);
}
var callback = function(username, password)
var callback = function(data)
{
$.getJSON("/json/property/vdcquery?query=setProperty(deviceId=" +
device.dSUID + ")/(x-netatmo-username=\"" + username + "\"," +
"x-netatmo-password=\"" + password + "\")"
).fail(vdc_request_failed);
vdc_get_client_id_callback(device, data);
};
vdc_show_password_prompt("Login",
"Enter your Netatmo account credentials for " +
device.DisplayID, callback);
$.getJSON("/json/property/vdcquery?query=getProperty(deviceId=" +
device.dSUID +
")/x-netatmo-client_id",
callback).fail(vdc_request_failed);
};
var vdc_render_add_device = function(dsuid, connected)
......@@ -205,6 +258,9 @@ var vdc_render_update_device_status = function(dsuid)
var vdc_setup = function()
{
g_auth["code"] = getURLParameter("code");
g_auth["dSUID"] = getURLParameter("state");
// clean potential reload leftovers from the modal window
window.location.hash = "";
......@@ -260,7 +316,7 @@ var vdc_device_query_callback = function(device, data)
}
g_vdcs[device.dSUID] = device;
g_vdcs[device.dSUID].conneced = connected;
g_vdcs[device.dSUID].connected = connected;
g_vdcs[device.dSUID].username = username;
vdc_render_add_device(device.dSUID, connected);
......@@ -274,11 +330,36 @@ var vdc_search = function()
vdc_search_callback).fail(vdc_request_failed).fail(vdc_request_failed);
};
var vdc_check_auth = function()
{
if ((g_auth.dSUID !== undefined) && (g_auth.code !== undefined))
{
jQuery.each(g_vdcs, function(key, val)
{
if (key === g_auth.dSUID)
{
var callback = function()
{
g_auth.dSUID = undefined;
g_auth.code = undefined;
};
$.getJSON("/json/property/vdcquery?query=setProperty(deviceId=" +
g_auth.dSUID + ")/(x-action-authcode=\"" +
g_auth.code + "\")",
callback).fail(vdc_request_failed);
return false;
}
});
}
vdc_start_search_timeout();
};
var vdc_query_device = function(device, remaining)
{
var callback = function(data)
{
if (data.ok === true)
if (data.ok === true)
{
vdc_device_query_callback(device, data);
}
......@@ -293,8 +374,12 @@ var vdc_query_device = function(device, remaining)
if (Object.keys(g_vdcs).length === 0)
{
vdc_render_no_devices();
vdc_start_search_timeout();
}
else
{
vdc_check_auth();
}
vdc_start_search_timeout();
}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment