Skip to content

Commit

Permalink
Add in optional button to reauth with safelauncher when success was n…
Browse files Browse the repository at this point in the history
…ot had
  • Loading branch information
joshuef committed Nov 14, 2016
1 parent f12a66b commit 409ea50
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 138 deletions.
55 changes: 17 additions & 38 deletions app/background-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -34,34 +34,33 @@ 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()

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 =>
{
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/background-process/api-manifests/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export default {
getInfo: 'promise',
checkForUpdates: 'promise',
restartBrowser: 'sync',
reauthenticateSAFE: 'promise',

getSettings: 'promise',
getSetting: 'promise',
Expand Down
6 changes: 5 additions & 1 deletion app/background-process/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function setup () {
getInfo,
checkForUpdates,
restartBrowser,

reauthenticateSAFE,
getSetting,
getSettings,
setSetting,
Expand Down Expand Up @@ -185,6 +185,10 @@ export function getSetting (key) {
return settingsDb.get(key)
}

export function reauthenticateSAFE () {
return settingsDb.reauthenticateSAFE()
}

export function getSettings () {
return settingsDb.getAll()
}
Expand Down
29 changes: 27 additions & 2 deletions app/background-process/safe-storage/settings.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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) =>
{
Expand Down
30 changes: 30 additions & 0 deletions app/background-process/safe-storage/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ const save = ( ) =>

export const saveStore = _.debounce( save, 200 );





export const reStore = ( storeState ) =>
{
if( storeState.errorCode )
Expand Down Expand Up @@ -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 } ) );
}
1 change: 0 additions & 1 deletion app/builtin-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
127 changes: 31 additions & 96 deletions app/builtin-pages/views/safe-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {


Expand All @@ -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()
})
Expand All @@ -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`<div class="ll-row ll-row-is-editing" data-row=${i}>
<span class="ll-link">
<div class="ll-inputs">
<input name="title" value=${row.editTitle} onkeyup=${onKeyUp(i)} />
<input name="url" value=${row.editUrl} onkeyup=${onKeyUp(i)} />
</div>
</div>
</div>`

const renderRowDefault = (row, i) => yo`<div class="ll-row" data-row=${i}>
<a class="ll-link" href=${row.url} title=${row.title}>
<img class="favicon" src=${'beaker-favicon:'+row.url} />
<span class="ll-title">${row.title}</span>
</a>
<div class="ll-actions">
<span class="icon icon-pencil" onclick=${onClickEdit(i)} title="Edit bookmark"></span>
<span class="icon icon-cancel" onclick=${onClickDelete(i)} title="Delete bookmark"></span>
</div>
</div>`

// optional help text
var helpEl = ''
var testEl = ''
var statusEl = ''
var reAuthEl = ''

if (bookmarks && bookmarks.length > 0 ) {
helpEl = yo`<div class="ll-help">
<span class="icon icon-info-circled">${bookmarks}</span>
if ( ! authSuccess ) {
reAuthEl = yo`<div class="ll-help">
<div class="icon icon-rocket" onclick=${onClickReAuth()} style="cursor: pointer;" >${reAuthMessage}</div>
</div>`
}

if (safeStatus && safeStatus.length > 0 ) {
statusEl = yo`<div class="ll-help">
<div class="icon icon-info-circled">${safeStatus}</div>
</div>`
}

Expand All @@ -69,82 +65,21 @@ function render () {
<div class="safe-status links-list">
<div class="ll-heading">
SAFE Network
<small class="ll-heading-right">
</small>
</div>
${helpEl}
${testEl}
${statusEl}
${reAuthEl}
</div>
</div>`)
}

// 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()
}
}

0 comments on commit 409ea50

Please sign in to comment.