This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 974
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3390 from brave/basic-auth
move basic auth state to appStore
- Loading branch information
Showing
19 changed files
with
703 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const electron = require('electron') | ||
const app = electron.app | ||
const appActions = require('../../js/actions/appActions') | ||
const appConstants = require('../../js/constants/appConstants') | ||
const appDispatcher = require('../../js/dispatcher/appDispatcher') | ||
const appStore = require('../../js/stores/appStore') | ||
|
||
// URLs to callback for auth. | ||
let authCallbacks = {} | ||
|
||
const cleanupAuthCallback = (tabId) => { | ||
delete authCallbacks[tabId] | ||
} | ||
|
||
const runAuthCallback = (tabId, detail) => { | ||
let cb = authCallbacks[tabId] | ||
if (cb) { | ||
delete authCallbacks[tabId] | ||
if (detail) { | ||
let username = detail.get('username') | ||
let password = detail.get('password') | ||
cb(username, password) | ||
} else { | ||
cb() | ||
} | ||
} | ||
} | ||
|
||
const doAction = (action) => { | ||
switch (action.actionType) { | ||
case appConstants.APP_SET_LOGIN_RESPONSE_DETAIL: | ||
appDispatcher.waitFor([appStore.dispatchToken], () => { | ||
runAuthCallback(action.tabId, action.detail) | ||
}) | ||
break | ||
default: | ||
} | ||
} | ||
|
||
const basicAuth = { | ||
init: () => { | ||
appDispatcher.register(doAction) | ||
app.on('login', (e, webContents, request, authInfo, cb) => { | ||
e.preventDefault() | ||
let tabId = webContents.getId() | ||
authCallbacks[tabId] = cb | ||
webContents.on('destroyed', () => { | ||
cleanupAuthCallback(tabId) | ||
}) | ||
webContents.on('crashed', () => { | ||
cleanupAuthCallback(tabId) | ||
}) | ||
appActions.setLoginRequiredDetail(tabId, { | ||
request, | ||
authInfo | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = basicAuth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const tabState = require('./tabState') | ||
const { makeImmutable } = require('./immutableUtil') | ||
|
||
const loginRequiredDetail = 'loginRequiredDetail' | ||
tabState.addTransientFields([loginRequiredDetail]) | ||
|
||
const basicAuthState = { | ||
setLoginRequiredDetail: (appState, tabId, detail) => { | ||
appState = makeImmutable(appState) | ||
detail = makeImmutable(detail) | ||
let tab = tabState.getOrCreateByTabId(appState, tabId) | ||
if (!detail || detail.size === 0) { | ||
tab = tab.delete(loginRequiredDetail) | ||
} else { | ||
tab = tab.set(loginRequiredDetail, detail) | ||
} | ||
return tabState.updateTab(appState, tabId, tab) | ||
}, | ||
|
||
getLoginRequiredDetail: (appState, tabId) => { | ||
appState = makeImmutable(appState) | ||
let tab = tabState.getByTabId(appState, tabId) | ||
return tab && tab.get(loginRequiredDetail) | ||
}, | ||
|
||
setLoginResponseDetail: (appState, tabId, detail) => { | ||
appState = makeImmutable(appState) | ||
let tab = tabState.getByTabId(appState, tabId) | ||
if (!tab) { | ||
return appState | ||
} | ||
tab = tab.delete(loginRequiredDetail) | ||
return tabState.updateTab(appState, tabId, tab) | ||
} | ||
} | ||
|
||
module.exports = basicAuthState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const Immutable = require('immutable') | ||
|
||
const immutableUtils = { | ||
makeImmutable: (obj) => { | ||
if (!obj) { | ||
return null | ||
} | ||
return obj.toJS ? obj : Immutable.fromJS(obj) | ||
} | ||
} | ||
|
||
module.exports = immutableUtils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const { makeImmutable } = require('./immutableUtil') | ||
|
||
let transientFields = ['tabId', 'windowId'] | ||
|
||
const tabState = { | ||
defaultTabState: makeImmutable({ | ||
windowId: -1, | ||
frameKey: -1, | ||
tabId: -1 | ||
}), | ||
|
||
getTabIndexByTabId: (state, tabId) => { | ||
return state.get('tabs').findIndex((tab) => tab.get('tabId') === tabId) | ||
}, | ||
|
||
createTab: (props) => { | ||
props = makeImmutable(props) | ||
return tabState.defaultTabState.merge(props) | ||
}, | ||
|
||
getOrCreateByTabId: (state, tabId) => { | ||
let tab = tabState.getByTabId(state, tabId) | ||
return tab || tabState.createTab({tabId}) | ||
}, | ||
|
||
getByTabId: (state, tabId) => { | ||
return state.get('tabs').find((tab) => tab.get('tabId') === tabId) | ||
}, | ||
|
||
closeTab: (state, tabId) => { | ||
let index = tabState.getTabIndexByTabId(state, tabId) | ||
if (index === -1) { | ||
return state | ||
} | ||
|
||
let tabs = state.get('tabs').delete(index) | ||
state = state.set('tabs', tabs) | ||
return state | ||
}, | ||
|
||
updateTab: (state, tabId, tab) => { | ||
let tabs = state.get('tabs') | ||
let index = tabState.getTabIndexByTabId(state, tabId) | ||
tabs = tabs.delete(index).insert(index, tab) | ||
return state.set('tabs', tabs) | ||
}, | ||
|
||
addTransientFields: (fields) => { | ||
transientFields = transientFields.concat(fields) | ||
}, | ||
|
||
getTransientFields: () => { | ||
return transientFields | ||
}, | ||
|
||
getPersistentTabState: (tab) => { | ||
tab = makeImmutable(tab) | ||
tabState.getTransientFields().forEach((field) => { | ||
tab = tab.delete(field) | ||
}) | ||
return tab | ||
} | ||
} | ||
|
||
module.exports = tabState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.