From 409ea503c74d8fa2cb383e635ece8f1f428761e7 Mon Sep 17 00:00:00 2001 From: Josh Wilson Date: Mon, 14 Nov 2016 12:04:03 +0100 Subject: [PATCH] Add in optional button to reauth with safelauncher when success was not had --- app/background-process.js | 55 +++----- .../api-manifests/browser.js | 1 + app/background-process/browser.js | 6 +- .../safe-storage/settings.js | 29 +++- app/background-process/safe-storage/store.js | 30 +++++ app/builtin-pages.js | 1 - app/builtin-pages/views/safe-status.js | 127 +++++------------- 7 files changed, 111 insertions(+), 138 deletions(-) diff --git a/app/background-process.js b/app/background-process.js index d2819f9385..be77c6117a 100644 --- a/app/background-process.js +++ b/app/background-process.js @@ -7,7 +7,7 @@ import { app, Menu, ipcMain } from 'electron' import log from 'loglevel' import env from './env' -import store, { getStore, reStore, saveStore } from './background-process/safe-storage/store'; +import store, { getStore, reStore, saveStore, handleAuthError } from './background-process/safe-storage/store'; // set setting does not trigger save import { updateSettings } from './background-process/safe-storage/settings'; @@ -34,15 +34,22 @@ import * as openURL from './background-process/open-url' import { auth } from 'safe-js'; -// // configure logging -log.setLevel('trace') +const safeBrowserApp = +{ + //TODO: pull from package.json + name: "SafeBrowser", + id: "safe-browser", + version: "0.4.0", + vendor: "josh.wilson", + permissions : [ "SAFE_DRIVE_ACCESS"] +}; -let sitedataActions = {}; -const dispatch = store.dispatch; -let currentValue; + +// // configure logging +log.setLevel('trace') // load the installed protocols plugins.registerStandardSchemes() @@ -50,18 +57,10 @@ plugins.registerStandardSchemes() app.on('ready', function () { - const app = - { - //TODO: pull from package.json - name: "SafeBrowser", - id: "safe-browser", - version: "0.4.0", - vendor: "josh.wilson", - permissions : [ "SAFE_DRIVE_ACCESS"] - }; - - let token = auth.authorise( app ).then( tok => + let token = auth.authorise( safeBrowserApp ).then( tok => { + store.dispatch( updateSettings( { 'authSuccess': true } ) ); + getStore( tok.token ) .then( json => { @@ -76,27 +75,7 @@ app.on('ready', function () { }) }) - .catch( err => - { - if( err.code === -12 ) - { - store.dispatch( updateSettings( { 'authMessage': 'SAFE Launcher does not appear to be open.' } ) ); - return; - } - else if( err.code === 'ECONNREFUSED' ) - { - store.dispatch( updateSettings( { 'authMessage': 'SAFE Launcher does not appear to be open.' } ) ); - return; - } - else if( err === 'Unauthorized' ) - { - store.dispatch( updateSettings( { 'authMessage':'The browser failed to authorise with the SAFE launcher.' } ) ); - return; - } - - store.dispatch( updateSettings( { 'authMessage': '' + err } ) ); - - }); + .catch( handleAuthError ); // databases diff --git a/app/background-process/api-manifests/browser.js b/app/background-process/api-manifests/browser.js index 033bc3ca15..e32f4c97f7 100644 --- a/app/background-process/api-manifests/browser.js +++ b/app/background-process/api-manifests/browser.js @@ -3,6 +3,7 @@ export default { getInfo: 'promise', checkForUpdates: 'promise', restartBrowser: 'sync', + reauthenticateSAFE: 'promise', getSettings: 'promise', getSetting: 'promise', diff --git a/app/background-process/browser.js b/app/background-process/browser.js index ae3aee3d7b..0c1393192c 100644 --- a/app/background-process/browser.js +++ b/app/background-process/browser.js @@ -61,7 +61,7 @@ export function setup () { getInfo, checkForUpdates, restartBrowser, - + reauthenticateSAFE, getSetting, getSettings, setSetting, @@ -185,6 +185,10 @@ export function getSetting (key) { return settingsDb.get(key) } +export function reauthenticateSAFE () { + return settingsDb.reauthenticateSAFE() +} + export function getSettings () { return settingsDb.getAll() } diff --git a/app/background-process/safe-storage/settings.js b/app/background-process/safe-storage/settings.js index 7deed77c85..bab81869c6 100644 --- a/app/background-process/safe-storage/settings.js +++ b/app/background-process/safe-storage/settings.js @@ -1,10 +1,10 @@ import { app } from 'electron' import log from '../../log' -import store from './store'; +import store, { handleAuthError } from './store'; import { List, Map, fromJS } from 'immutable'; import { createActions } from 'redux-actions'; - +import { auth } from 'safe-js'; const UPDATE_SETTINGS = 'UPDATE_SETTINGS'; @@ -77,6 +77,31 @@ export function get (key) } + +const safeBrowserApp = +{ + //TODO: pull from package.json + name: "SafeBrowser", + id: "safe-browser", + version: "0.4.0", + vendor: "josh.wilson", + permissions : [ "SAFE_DRIVE_ACCESS"] +}; + + + +export function reauthenticateSAFE () { + + return auth.authorise( safeBrowserApp ).then( tok => + { + store.dispatch( updateSettings( { 'authSuccess': true } ) ); + + store.dispatch( updateSettings( { 'authToken' : tok.token } ) ); + store.dispatch( updateSettings( { 'authMessage': 'Authorised with launcher.' } ) ); + + } ).catch( handleAuthError ); +}; + export function getAll () { return new Promise( ( resolve, reject) => { diff --git a/app/background-process/safe-storage/store.js b/app/background-process/safe-storage/store.js index 9a349c034f..a3c198b0af 100644 --- a/app/background-process/safe-storage/store.js +++ b/app/background-process/safe-storage/store.js @@ -93,6 +93,10 @@ const save = ( ) => export const saveStore = _.debounce( save, 200 ); + + + + export const reStore = ( storeState ) => { if( storeState.errorCode ) @@ -132,3 +136,29 @@ const dispatchForEach = ( array, action ) => return; }) } + + + + + +export const handleAuthError = ( err ) => +{ + store.dispatch( updateSettings( { 'authSuccess': false} ) ); + if( err.code === -12 ) + { + store.dispatch( updateSettings( { 'authMessage': 'SAFE Launcher does not appear to be open.' } ) ); + return; + } + else if( err.code === 'ECONNREFUSED' ) + { + store.dispatch( updateSettings( { 'authMessage': 'SAFE Launcher does not appear to be open.' } ) ); + return; + } + else if( err === 'Unauthorized' ) + { + store.dispatch( updateSettings( { 'authMessage':'The browser failed to authorise with the SAFE launcher.' } ) ); + return; + } + + store.dispatch( updateSettings( { 'authMessage': '' + err } ) ); +} \ No newline at end of file diff --git a/app/builtin-pages.js b/app/builtin-pages.js index fbbe3ef582..c019398134 100644 --- a/app/builtin-pages.js +++ b/app/builtin-pages.js @@ -32,7 +32,6 @@ window.history.replaceState = _wr('replaceState'); // = // // -console.log( "FAVS PAGE???", favorites ); var views = { start: safeStatus, favorites, archives, history, downloads, settings } var currentView = getLocationView() diff --git a/app/builtin-pages/views/safe-status.js b/app/builtin-pages/views/safe-status.js index 6418d10158..a93b4f8590 100644 --- a/app/builtin-pages/views/safe-status.js +++ b/app/builtin-pages/views/safe-status.js @@ -5,8 +5,10 @@ This uses the beakerBookmarks APIs, which is exposed by webview-preload to all s import * as yo from 'yo-yo' import co from 'co' -// bookmarks, cached in memory -var bookmarks = [] +// safeStatus, cached in memory +var safeStatus = {}; +var reAuthMessage = 'Reauthorise with SAFE Launcher'; +var authSuccess = false; export function setup () { @@ -15,10 +17,20 @@ export function setup () { export function show () { document.title = 'SAFE Network Status' + + co(function*() { + + authSuccess = authSuccess || false; + authSuccess = yield beakerBrowser.getSetting( 'authSuccess' ); + + render() + }) + + co(function*() { - bookmarks = bookmarks || {}; - bookmarks = yield beakerBrowser.getSetting( 'authMessage' ); + safeStatus = safeStatus || {}; + safeStatus = yield beakerBrowser.getSetting( 'authMessage' ); render() }) @@ -32,35 +44,19 @@ export function hide () { function render () { - const renderRow = (row, i) => row.isEditing ? renderRowEditing(row, i) : renderRowDefault(row, i) - - const renderRowEditing = (row, i) => yo`
- -
- - -
-
- ` - - const renderRowDefault = (row, i) => yo`
- - - ${row.title} - -
- - -
-
` - // optional help text - var helpEl = '' - var testEl = '' + var statusEl = '' + var reAuthEl = '' - if (bookmarks && bookmarks.length > 0 ) { - helpEl = yo`
- ${bookmarks} + if ( ! authSuccess ) { + reAuthEl = yo`
+
${reAuthMessage}
+
` + } + + if (safeStatus && safeStatus.length > 0 ) { + statusEl = yo`
+
${safeStatus}
` } @@ -69,11 +65,9 @@ function render () {
`) } @@ -81,70 +75,11 @@ function render () { // event handlers // = -function onClickEdit (i) { +function onClickReAuth (i) { return e => { e.preventDefault() e.stopPropagation() - // capture initial value - bookmarks[i].editTitle = bookmarks[i].title - bookmarks[i].editUrl = bookmarks[i].url - - // enter edit-mode - bookmarks[i].isEditing = true - render() - document.querySelector(`[data-row="${i}"] input`).focus() - } -} - -function onKeyUp (i) { - return e => { - // enter-key - if (e.keyCode == 13) { - // capture the old url - var oldUrl = bookmarks[i].url - - // update values - bookmarks[i].title = document.querySelector(`[data-row="${i}"] [name="title"]`).value - bookmarks[i].url = document.querySelector(`[data-row="${i}"] [name="url"]`).value - - // exit edit-mode - bookmarks[i].isEditing = false - render() - - // save in backend - beakerBookmarks.changeTitle(oldUrl, bookmarks[i].title) - beakerBookmarks.changeUrl(oldUrl, bookmarks[i].url) - } - - // escape-key - else if (e.keyCode == 27) { - // exit edit-mode - bookmarks[i].isEditing = false - render() - } - - // all else - else { - // update edit values - if (e.target.name == 'title') - bookmarks[i].editTitle = e.target.value - if (e.target.name == 'url') - bookmarks[i].editUrl = e.target.value - } - + beakerBrowser.reauthenticateSAFE( ); } } - -function onClickDelete (i) { - return e => { - e.preventDefault() - e.stopPropagation() - - // delete bookmark - var b = bookmarks[i] - bookmarks.splice(i, 1) - beakerBookmarks.remove(b.url) - render() - } -} \ No newline at end of file