From 44710854ca2d6d1fb40786f53ee11599ab770582 Mon Sep 17 00:00:00 2001 From: bahbka Date: Wed, 9 Jul 2014 10:20:30 +0400 Subject: Added authentication. --- README.md | 22 ++++- appinfo.json | 4 +- resources/configuration.html | 54 ++++++----- src/js/pebble-js-app.src.js | 222 ++++++++++++++++++++++++++++++++++++++++++- stuff/pebble-my-data.pbw | Bin 24241 -> 28583 bytes 5 files changed, 274 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 93a5e33..3933f08 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Inspired by [Pebble Cards](http://keanulee.com/pebblecards). * Ability to change up/down buttons behavior from JSON (scrolling or up=1|2,down=1|2 params) * Append coordinates to URL (configurable) * Append HTTP request header Pebble-Token (unique to device/app pair), can be used for server-side device identification +* Authentication (see documentation) * Scrollable data area * Custom update interval, specified in JSON * Vibrate on update if specified in JSON @@ -31,6 +32,9 @@ Inspired by [Pebble Cards](http://keanulee.com/pebblecards). ## Changelog +### 2.2.0 +- Authentication (see documentation) + ### 2.1.2 - Ability to change up/down buttons behavior from JSON (scrolling or up=1|2,down=1|2 params) - Added HTTP request header Pebble-Token (unique to device/app pair), can be used for server-side device identification @@ -77,7 +81,8 @@ JSON output example (some fields are optional): "scroll": 1, "light": 1, "blink": 3, - "updown": 1 + "updown": 1, + "auth": "salt" } GET param short=1 or long=1 added to URL on short or long select button update @@ -129,6 +134,21 @@ Next update delay in seconds. - 0 use up/down buttons for scrolling - 1 use up/down buttons for update, appending up=1|2/down=1|2 params (1=short/2=long) +### auth +Salt for Pebble-Auth hash (see below) + +## Auth + +Authentication algorithm example (reinvent the wheel): +1. -> Pebble makes HTTP request with Pebble-Token header (Pebble App Token by default, unique to device/app pair, can be changed at configuration page, clear to restore default) +2. <- Server answers with JSON like { ..., "content": "logging in...", "refresh": 5, "auth": "randomsalt", ... } +3. Pebble calculates MD5(MD5(password)+"randomsalt"), saves it as auth token and uses as Pebble-Auth HTTP request header in future requests. +4. -> Pebble makes HTTP request after 5 seconds with Pebble-Token header and with Pebble-Auth header (calculated and stored in previous step) +5. Server checks Pebble-Token and Pebble-Auth headers if data equal data in database (Pebble-Token <=> login, calculate MD5(password_md5_db+"randomsalt")) +6. <- Server answers with private content (seems your need https for more security), or some error if auth failed; auth field in JSON not needed anymore, until you desire to regenerate auth token with new salt (paranoid mode) or to clear Pebble-Auth header + +To clear Pebble-Auth header, send { ..., "auth": "", ...} (eg logout). + ## Bugs Sometime after install JS app fails to start, issue related Pebble App. Force stop Pebble App and start it again. diff --git a/appinfo.json b/appinfo.json index cd6ab51..2bc396d 100644 --- a/appinfo.json +++ b/appinfo.json @@ -3,8 +3,8 @@ "shortName": "My Data", "longName": "My Data", "companyName": "bahbka", - "versionCode": 212, - "versionLabel": "2.1.2", + "versionCode": 220, + "versionLabel": "2.2.0", "watchapp": { "watchface": false }, diff --git a/resources/configuration.html b/resources/configuration.html index 25c135a..4f21aae 100644 --- a/resources/configuration.html +++ b/resources/configuration.html @@ -1,6 +1,7 @@ +vim: sw=2 ts=2 expandtab ai +--> @@ -11,7 +12,7 @@ vim: sw=2 ts=2 expandtab ai --> small { color: gray } a { color: white } input { height: 1.5em; font-size: 1.2em; font-weight: bold } - .url { width: 93%; margin: 0.5em } + .text { width: 93%; margin: 0.5em; text-align: center } .submit { width: 93%; margin: 0.4em } .param { display: inline-table; width: 95%; height: 3em } .label,.checkbox { display: table-cell; vertical-align: middle } @@ -50,16 +51,37 @@ vim: sw=2 ts=2 expandtab ai --> } return window.location.href = "pebblejs://close#" + encodeURIComponent(JSON.stringify(config)); } + + function toggle_visibility(id) { + var e = document.getElementById(id); + if(e.style.display == 'block') + e.style.display = 'none'; + else + e.style.display = 'block'; + }

My Data

- v2.1.2, by bahbka + v2.2.0, by bahbka
- Server URL: - + Server URL + + +
+ + Server auth (optional) + + +
@@ -91,25 +113,9 @@ vim: sw=2 ts=2 expandtab ai -->
-
- Sample server output:
- (see documentation on github) -
-{
-  "content": "Hello\\nWorld!",
-  "refresh": 300, // refresh delay, seconds
-  "vibrate": 0,   // 0..3, 0 - don't vibrate
-  "font": 1,      // 1..8, try them all
-  "theme": 0,     // 0 - black, 1 - white
-  "scroll": 1,    // scroll up after update
-  "light": 1,     // turn on light
-  "blink": 1,     // blink content (count)
-  "updown": 1     // change up/down behavior
-}
-
-GET param select=1 or select=2 added to URL on
-short or long select button update
-    
+
+ see documentation on github
+ discussion at pebble forums diff --git a/src/js/pebble-js-app.src.js b/src/js/pebble-js-app.src.js index 099a2f9..bf08528 100644 --- a/src/js/pebble-js-app.src.js +++ b/src/js/pebble-js-app.src.js @@ -25,7 +25,16 @@ function http_request(url) { var req = new XMLHttpRequest(); req.open('GET', url, true); - req.setRequestHeader('Pebble-Token', Pebble.getAccountToken()); + + if (!config["token"]) { + config["token"] = Pebble.getAccountToken(); + } + req.setRequestHeader('Pebble-Token', config["token"]); + + var auth = window.localStorage.getItem('pebble-my-data-auth'); + if (auth) { + req.setRequestHeader('Pebble-Auth', auth); + } req.onload = function(e) { @@ -37,6 +46,14 @@ function http_request(url) { response["msg_type"] = MSG.JSON_RESPONSE; Pebble.sendAppMessage(response); + if (response["auth"] != null) { + if (response["auth"] == "") { + window.localStorage.removeItem('pebble-my-data-auth'); + } else if (config["password"]) { + window.localStorage.setItem('pebble-my-data-auth', MD5(MD5(config["password"]) + response["auth"])); + } + } + } catch(e) { console.log("json parse error"); Pebble.sendAppMessage({ "msg_type": MSG.ERROR }); @@ -145,6 +162,9 @@ Pebble.addEventListener('showConfiguration', function () { } else { url = ""; } + if (!config["token"]) { + config["token"] = Pebble.getAccountToken(); + } //console.log("put options = " + JSON.stringify(config)); Pebble.openURL('data:text/html,'+encodeURI('_HTMLMARKER_