Commit 61e78f86 authored by Jin's avatar Jin

Merge branch 'store-refresh-not-auth-token' into 'master'

need refresh token instead of access token

the refresh token does not expire and allows to
query new access tokens later

See merge request !4
parents 9bfd089a da0e9712
......@@ -28,7 +28,8 @@
<tr>
<th>&nbsp;</th>
<th>Name</th>
<th>ID</th>
<th>VDC ID</th>
<th>User ID</th>
<th>Status</th>
<th>&nbsp;</th>
</tr>
......
var g_vdcs = {};
var g_timeout_id;
var g_auth = { "dSUID": undefined, "code": undefined };
var g_auth = {
"dSUID": undefined,
"code": undefined,
"refresh_token": undefined,
"access_token": undefined
};
var g_client_id = "52823f931877590c917b23f7";
var g_client_secret = "Q5rt19RMedbTkHblsIE5mKK3rhKKQQcCJ9AJwr";
var vdc_strings =
{
BUTTON_CONNECT: "authenticate",
BUTTON_CONNECT: "register",
BUTTON_DISCONNECT: "dissociate",
STATUS_CONNECTED: "connected",
STATUS_DISCONNECTED: "not connected"
......@@ -60,11 +67,10 @@ var vdc_start_search_timeout = function()
var vdc_is_authenticated = function(device)
{
if ((device) && (device.authcode) && (device.authcode.length > 0))
if ((device) && (device.connected) && (device.username) && (device.username.length > 0))
{
return true;
}
return false;
};
......@@ -104,7 +110,7 @@ var vdc_get_client_id_callback = function(device, data)
for (i = 0; i < props.length; i++)
{
var property = props[i];
if (property.name === "x-netatmo-client_id")
if (property.name === "x-netatmo-client-id")
{
client_id = property.value;
}
......@@ -189,6 +195,16 @@ var vdc_auth_button_clicked = function(dsuid, evt)
"Something went wrong, please reload the page", true);
}
if (vdc_is_authenticated()) {
$.getJSON("/json/property/vdcquery?", {
query: "setProperty(deviceId=" + dsuid + ")/" +
"(x-netatmo-username=\"\")" +
"(x-netatmo-refresh_token=\"\")"
},
function() {}).fail(vdc_request_failed);
return;
}
var callback = function(data)
{
vdc_get_client_id_callback(device, data);
......@@ -196,7 +212,7 @@ var vdc_auth_button_clicked = function(dsuid, evt)
$.getJSON("/json/property/vdcquery?query=getProperty(deviceId=" +
device.dSUID +
")/x-netatmo-client_id",
")/x-netatmo-client-id",
callback).fail(vdc_request_failed);
};
......@@ -204,7 +220,7 @@ var vdc_auth2_button_clicked = function(dsuid, evt)
{
var redirect_uri = encodeURIComponent(window.location.origin + window.location.pathname);
var url = "https://api.netatmo.net/oauth2/authorize?response_type=code&client_id=" +
"52823f931877590c917b23f7" + "&redirect_uri=" + redirect_uri + "&state=" + dsuid + "&scope=read_station";
g_client_id + "&redirect_uri=" + redirect_uri + "&state=" + dsuid + "&scope=read_station";
top.location.href=url;
}
......@@ -219,19 +235,21 @@ var vdc_render_add_device = function(dsuid, connected)
vdc_render_remove_device(dsuid);
var state = vdc_strings.STATUS_DISCONNECTED;
var button = vdc_strings.BUTTON_CONNECT;
if (connected)
{
state = vdc_strings.STATUS_CONNECTED;
button = vdc_strings.BUTTON_DISCONNECT;
}
var row = "<tr id=\"" + dsuid + "\"><td>" +
"<img src=\"icon16.png" + "\"/></td><td>" +
device.name + "</td><td>" +
device.DisplayID + "</td><td id=\"state_" + dsuid + "\">" +
device.username + "</td><td>" +
state + "</td><td>" +
"<button id=\"btn_" + dsuid + "\" type=\"button\">" +
vdc_strings.BUTTON_CONNECT +
button +
"</button></td></tr>";
$("#device_list_content").append(row);
......@@ -262,6 +280,7 @@ var vdc_setup = function()
{
g_auth["code"] = getURLParameter("code");
g_auth["dSUID"] = getURLParameter("state");
g_auth["access_token"] = getURLParameter("access_token");
// clean potential reload leftovers from the modal window
window.location.hash = "";
......@@ -332,27 +351,79 @@ var vdc_search = function()
vdc_search_callback).fail(vdc_request_failed).fail(vdc_request_failed);
};
var vdc_check_auth = function()
var vdc_check_auth = function(device)
{
console.log("check_auth: " + g_auth.code + "/" + g_auth.access_token);
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;
var redirect_uri = encodeURIComponent(window.location.origin +
window.location.pathname);
var query = {
"client_id" : g_client_id,
"client_secret" : g_client_secret,
"grant_type" : "authorization_code",
"code" : g_auth.code,
"redirect_uri" : window.location.origin + window.location.pathname,
"scope" : "read_station"
};
var url = "https://api.netatmo.net/oauth2/token";
var jqxhr = $.post(url, query, function(data) {
console.log('Post response loaded: ', data, 'target: ', data.target);
g_auth.access_token = data.access_token;
g_auth.refresh_token = data.refresh_token;
g_auth.refresh_timeout = data.expires_in;
g_auth.code = undefined;
vdc_check_auth(device);
})
.done(function() {
console.log("post second success");
})
.fail(function(data) {
console.log("post errur: ", data);
})
.always(function() {
console.log("post finished");
});
return false;
}
});
}
if ((device.dSUID !== undefined) && (g_auth.refresh_token !== undefined))
{
jQuery.each(g_vdcs, function(key, val)
{
if (key === g_auth.dSUID)
{
var callback = function() {
g_auth.refresh_token = undefined;
};
var redirect_uri = encodeURIComponent(window.location.origin + window.location.pathname);
$.getJSON("/json/property/vdcquery?",
{
query: "setProperty(deviceId=" + g_auth.dSUID + ")/" +
"(x-action-authcode=\"" + g_auth.code + "\")" +
",(x-action-redirect_uri=\"" + redirect_uri + "\")"
$.getJSON("/json/property/vdcquery?", {
query: "setProperty(deviceId=" + g_auth.dSUID + ")/" +
"(x-netatmo-refresh_token=\"" + g_auth.refresh_token + "\")" +
",(x-netatmo-refresh_timeout=\"" + g_auth.refresh_timeout + "\")" +
",(x-netatmo-access_token=\"" + g_auth.access_token + "\")"
},
callback).fail(vdc_request_failed);
$.getJSON("https://api.netatmo.net/api/getuser?access_token=" + g_auth.access_token,
function(data) {
device.username = data.body.mail;
$.getJSON("/json/property/vdcquery?", {
query: "setProperty(deviceId=" + g_auth.dSUID + ")/" +
"(x-netatmo-username=\"" + device.username + "\")"
},
callback).fail(vdc_request_failed);
},
callback).fail(vdc_request_failed);
return false;
}
});
......@@ -384,7 +455,7 @@ var vdc_query_device = function(device, remaining)
}
else
{
vdc_check_auth();
vdc_check_auth(device);
}
}
};
......
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