-
Notifications
You must be signed in to change notification settings - Fork 471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[New Feature] Using node-keytar to store github token in safe place #321
Open
khanhicetea
wants to merge
4
commits into
hackjutsu:master
Choose a base branch
from
khanhicetea:keytar
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- "8" | ||
before_install: | ||
- sudo apt-get update | ||
- sudo apt-get install -y libsecret-1-dev |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ import { Provider } from 'react-redux' | |
import { createStore, applyMiddleware } from 'redux' | ||
import thunk from 'redux-thunk' | ||
import electronLocalStorage from 'electron-json-storage-sync' | ||
import { Promise } from 'bluebird' | ||
import keytar from 'keytar' | ||
import Async from 'react-promise' | ||
|
||
import './utilities/vendor/bootstrap/css/bootstrap.css' | ||
import AppContainer from './containers/appContainer' | ||
|
@@ -69,7 +72,8 @@ const localPref = new Store({ | |
const CONFIG_OPTIONS = { | ||
client_id: Account.client_id, | ||
client_secret: Account.client_secret, | ||
scopes: ['gist'] | ||
scopes: ['gist'], | ||
keyChainService: 'Lepton Github Token' | ||
} | ||
|
||
let preSyncSnapshot = { | ||
|
@@ -385,17 +389,18 @@ function initUserSession (token) { | |
token: token, | ||
profile: newProfile.login, | ||
image: newProfile.avatar_url | ||
}) | ||
logger.debug('-----> after updateLocalStorage') | ||
}).then(() => { | ||
logger.debug('-----> after updateLocalStorage') | ||
|
||
logger.debug('-----> before syncLocalPref') | ||
syncLocalPref(newProfile.login) | ||
logger.debug('-----> after syncLocalPref') | ||
logger.debug('-----> before syncLocalPref') | ||
syncLocalPref(newProfile.login) | ||
logger.debug('-----> after syncLocalPref') | ||
|
||
remote.getCurrentWindow().setTitle(`${ newProfile.login } | Lepton`) // update the app title | ||
remote.getCurrentWindow().setTitle(`${ newProfile.login } | Lepton`) // update the app title | ||
|
||
logger.info('[Dispatch] updateUserSession ACTIVE') | ||
reduxStore.dispatch(updateUserSession({ activeStatus: 'ACTIVE', profile: newProfile })) | ||
logger.info('[Dispatch] updateUserSession ACTIVE') | ||
reduxStore.dispatch(updateUserSession({ activeStatus: 'ACTIVE', profile: newProfile })) | ||
}) | ||
}) | ||
.catch((err) => { | ||
logger.debug('-----> Failure with ' + JSON.stringify(err)) | ||
|
@@ -415,22 +420,28 @@ function initUserSession (token) { | |
|
||
/** Start: Local storage management **/ | ||
function updateLocalStorage (data) { | ||
try { | ||
logger.debug(`-----> Caching token ${data.token}`) | ||
let rst = electronLocalStorage.set('token', data.token) | ||
logger.debug(`-----> [${rst.status}] Cached token ${data.token}`) | ||
|
||
logger.debug(`-----> Caching profile ${data.profile}`) | ||
rst = electronLocalStorage.set('profile', data.profile) | ||
logger.debug(`-----> [${rst.status}] Cached profile ${data.profile}`) | ||
|
||
logger.debug(`-----> Caching image ${data.image}`) | ||
downloadImage(data.image, data.profile) | ||
|
||
logger.debug(`-----> User info is cached.`) | ||
} catch (e) { | ||
logger.error(`-----> Failed to cache user info. ${JSON.stringify(e)}`) | ||
} | ||
return new Promise((resolve, reject) => { | ||
try { | ||
logger.debug(`-----> Caching token ${data.token} in keychain`) | ||
keytar.setPassword(CONFIG_OPTIONS.keyChainService, data.profile, data.token).then(() => { | ||
logger.debug(`-----> Cached token in keychain`) | ||
|
||
logger.debug(`-----> Caching profile ${data.profile}`) | ||
let rst = electronLocalStorage.set('profile', data.profile) | ||
logger.debug(`-----> [${rst.status}] Cached profile ${data.profile}`) | ||
|
||
logger.debug(`-----> Caching image ${data.image}`) | ||
downloadImage(data.image, data.profile) | ||
|
||
logger.debug(`-----> User info is cached.`) | ||
|
||
resolve() | ||
}) | ||
} catch (e) { | ||
logger.error(`-----> Failed to cache user info. ${JSON.stringify(e)}`) | ||
reject(e) | ||
} | ||
}) | ||
} | ||
|
||
function downloadImage (imageUrl, filename) { | ||
|
@@ -461,21 +472,31 @@ function downloadImage (imageUrl, filename) { | |
} | ||
|
||
function getCachedUserInfo () { | ||
logger.debug('-----> Inside getCachedUserInfo') | ||
const cachedProfile = electronLocalStorage.get('profile') | ||
logger.debug(`-----> [${cachedProfile.status}] cachedProfile is ${cachedProfile.data}`) | ||
const cachedToken = electronLocalStorage.get('token') | ||
logger.debug(`-----> [${cachedToken.status}] cachedToken is ${cachedToken.data}`) | ||
|
||
if (cachedProfile.status && cachedToken.status) { | ||
return { | ||
token: cachedToken.data, | ||
profile: cachedProfile.data, | ||
image: electronLocalStorage.get('image').data | ||
return new Promise((resolve, reject) => { | ||
logger.debug('-----> Inside getCachedUserInfo') | ||
const cachedProfile = electronLocalStorage.get('profile') | ||
logger.debug(`-----> [${cachedProfile.status}] cachedProfile is ${cachedProfile.data}`) | ||
const cachedImage = electronLocalStorage.get('image') | ||
logger.debug(`-----> [${cachedImage.status}] cachedImage is ${cachedImage.data}`) | ||
|
||
if (cachedProfile.status) { | ||
keytar.getPassword(CONFIG_OPTIONS.keyChainService, cachedProfile.data).then((token, err) => { | ||
logger.debug(`-----> [${err}] cachedToken is ${token}`) | ||
|
||
if (err) { | ||
return resolve(null) | ||
} | ||
|
||
return resolve({ | ||
token: token, | ||
profile: cachedProfile.data, | ||
image: cachedImage.data | ||
}) | ||
}) | ||
} else { | ||
return resolve(null) | ||
} | ||
} | ||
|
||
return null | ||
}) | ||
} | ||
|
||
function syncLocalPref (userName) { | ||
|
@@ -692,17 +713,21 @@ const reduxStore = createStore( | |
) | ||
|
||
ReactDom.render( | ||
<Provider store={ reduxStore }> | ||
<AppContainer | ||
searchIndex = { SearchIndex } | ||
localPref = { localPref } | ||
updateLocalStorage = { updateLocalStorage } | ||
loggedInUserInfo = { getCachedUserInfo() } | ||
launchAuthWindow = { launchAuthWindow } | ||
reSyncUserGists = { reSyncUserGists } | ||
updateAboutModalStatus = { updateAboutModalStatus } | ||
updateDashboardModalStatus = { updateDashboardModalStatus } | ||
updateActiveGistAfterClicked = { updateActiveGistAfterClicked } /> | ||
</Provider>, | ||
<Async | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use regular There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I'm not good at JS so I don't get it. Could you teach me why using regular |
||
promise={ getCachedUserInfo() } | ||
then = {cachedUserInfo => | ||
<Provider store={ reduxStore }> | ||
<AppContainer | ||
searchIndex = { SearchIndex } | ||
localPref = { localPref } | ||
updateLocalStorage = { updateLocalStorage } | ||
loggedInUserInfo = { cachedUserInfo } | ||
launchAuthWindow = { launchAuthWindow } | ||
reSyncUserGists = { reSyncUserGists } | ||
updateAboutModalStatus = { updateAboutModalStatus } | ||
updateDashboardModalStatus = { updateDashboardModalStatus } | ||
updateActiveGistAfterClicked = { updateActiveGistAfterClicked } /> | ||
</Provider> | ||
} />, | ||
document.getElementById('container') | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we avoid treating Linux as a special case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OSes use different things to store secret so
node-keytar
(a native node module) have to deal with this issue.From what I know, we have to rebuild native node modules b/c some pre-build libs won't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for explaining:) I'm hesitant to add extra build steps for particular platforms, especially introducing external dependency by installing prerequisites. This often (significantly) increases the maintenance cost by making it hard to dig out or reproduce bugs, especially when the bug is related to the launch step. Maybe this module is not ready for integration at this moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yah, you're right. Using this module can increase failure rate and it's not safe as we want (atom/node-keytar#88)