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`