Fixed trusted keys functionality and added trusted keys .env toggle

This commit is contained in:
emad-salah 2020-08-11 11:46:59 +01:00
parent bff9739e19
commit 52d45f697f
2 changed files with 14 additions and 9 deletions

View file

@ -4,3 +4,4 @@ MS_TO_TOKEN_EXPIRATION=4500000
DISABLE_SHOCK_ENCRYPTION=false DISABLE_SHOCK_ENCRYPTION=false
CACHE_HEADERS_MANDATORY=true CACHE_HEADERS_MANDATORY=true
SHOCK_CACHE=true SHOCK_CACHE=true
TRUSTED_KEYS=true

View file

@ -190,9 +190,10 @@ module.exports = async (
resolve(unlockResponse) resolve(unlockResponse)
}) })
} catch (err) { } catch (err) {
logger.error(err) logger.error('Unlock Error:', err)
if (err.message === 'unknown service lnrpc.WalletUnlocker') { if (err.message === 'unknown service lnrpc.WalletUnlocker') {
resolve({ resolve({
field: 'walletUnlocker',
message: 'Wallet already unlocked' message: 'Wallet already unlocked'
}) })
return return
@ -478,7 +479,7 @@ module.exports = async (
const tokenValid = await auth.validateToken(token) const tokenValid = await auth.validateToken(token)
return tokenValid return tokenValid
} catch (err) { } catch (err) {
return err return false
} }
} }
@ -500,7 +501,7 @@ module.exports = async (
const publicKey = await GunDB.authenticate(alias, password) const publicKey = await GunDB.authenticate(alias, password)
if (!publicKey) { if (!publicKey) {
res.status(400).json({ res.status(401).json({
field: 'alias', field: 'alias',
errorMessage: 'Invalid alias/password combination', errorMessage: 'Invalid alias/password combination',
success: false success: false
@ -508,16 +509,19 @@ module.exports = async (
return false return false
} }
const trustedKeysEnabled =
process.env.TRUSTED_KEYS === 'true' || !process.env.TRUSTED_KEYS
const trustedKeys = await Storage.get('trustedPKs') const trustedKeys = await Storage.get('trustedPKs')
const [isKeyTrusted] = trustedKeys.filter( // Falls back to true if trusted keys is disabled in .env
const [isKeyTrusted = !trustedKeysEnabled] = (trustedKeys || []).filter(
trustedKey => trustedKey === publicKey trustedKey => trustedKey === publicKey
) )
const walletUnlocked = health.LNDStatus.walletStatus === 'unlocked' const walletUnlocked = health.LNDStatus.walletStatus === 'unlocked'
if (!walletUnlocked) { if (!walletUnlocked) {
await unlockWallet(password) const unlockedWallet = await unlockWallet(password)
if (!isKeyTrusted) { if (!isKeyTrusted && unlockedWallet.field !== 'walletUnlocker') {
await Storage.set('trustedPKs', [...trustedKeys, publicKey]) await Storage.set('trustedPKs', [...trustedKeys, publicKey])
} }
} }
@ -529,7 +533,7 @@ module.exports = async (
) )
if (!validatedToken) { if (!validatedToken) {
res.status(403).json({ res.status(401).json({
field: 'alias', field: 'alias',
errorMessage: 'Invalid alias/password combination', errorMessage: 'Invalid alias/password combination',
success: false success: false
@ -695,9 +699,9 @@ module.exports = async (
GunDB.mySEA GunDB.mySEA
) )
const trustedPKs = await Storage.get('trustedPKs') const trustedKeys = await Storage.get('trustedPKs')
await Storage.setItem('trustedPKs', [ await Storage.setItem('trustedPKs', [
...(trustedPKs || []), ...(trustedKeys || []),
publicKey publicKey
]) ])