Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Feature/cookie based auth #159

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions config/daemon/read-cookie-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @flow
import path from 'path';
import fs from 'fs';

import { getZcashFolder } from './get-zcash-folder';

// eslint-disable-next-line
export const readCookieFile = () => new Promise<{ user: string, password: string }>((resolve, reject) => {
const filePath = path.join(getZcashFolder(), '.cookie');

fs.readFile(filePath, (err, data) => {
if (err) return reject(err);

const cookie = data.toString();

const [user, password] = cookie.split(':');
guilhermedecampo marked this conversation as resolved.
Show resolved Hide resolved

resolve({
user,
password,
});
});
});
25 changes: 20 additions & 5 deletions config/daemon/zcashd-child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import store from '../electron-store';
import { parseZcashConf, parseCmdArgs, generateArgsFromConf } from './parse-zcash-conf';
import { isTestnet } from '../is-testnet';
import { getDaemonProcessId } from './get-daemon-process-id';
import { readCookieFile } from './read-cookie-file';
import {
EMBEDDED_DAEMON,
ZCASH_NETWORK,
Expand Down Expand Up @@ -93,6 +94,8 @@ const runDaemon: () => Promise<?ChildProcess> = () => new Promise(async (resolve
mainWindow.webContents.on('dom-ready', () => {
isWindowOpened = true;
});
store.delete('rpcuser');
store.delete('rpcpassword');
guilhermedecampo marked this conversation as resolved.
Show resolved Hide resolved
store.delete('rpcconnect');
store.delete('rpcport');
store.delete(DAEMON_PROCESS_PID);
Expand Down Expand Up @@ -131,7 +134,6 @@ const runDaemon: () => Promise<?ChildProcess> = () => new Promise(async (resolve
await waitForDaemonClose(ZCASHD_PROCESS_NAME);
}

// This will parse and save rpcuser and rpcpassword in the store
let [, optionsFromZcashConf] = await eres(parseZcashConf());

// if the user has a custom datadir and doesn't have a zcash.conf in that folder,
Expand All @@ -153,7 +155,20 @@ const runDaemon: () => Promise<?ChildProcess> = () => new Promise(async (resolve
if (optionsFromZcashConf.rpcconnect) store.set('rpcconnect', optionsFromZcashConf.rpcconnect);
if (optionsFromZcashConf.rpcport) store.set('rpcport', optionsFromZcashConf.rpcport);
if (optionsFromZcashConf.rpcuser) store.set('rpcuser', optionsFromZcashConf.rpcuser);
if (optionsFromZcashConf.rpcpassword) store.set('rpcpassword', optionsFromZcashConf.rpcpassword);
if (optionsFromZcashConf.rpcpassword) {
store.set('rpcpassword', optionsFromZcashConf.rpcpassword);
} else {
// Try fall back to cookie-based authentication if no password is provided
// For now it only works for the .cookie file in the default zcash folder
const [, response] = await eres(readCookieFile());

if (response) {
log('Reading credentials from .cookie file');

store.set('rpcuser', response.user);
store.set('rpcpassword', response.password);
}
}

log('Searching for zcashd.pid');
const daemonProcessId = getDaemonProcessId(optionsFromZcashConf.datadir);
Expand Down Expand Up @@ -200,8 +215,8 @@ const runDaemon: () => Promise<?ChildProcess> = () => new Promise(async (resolve
store.set(ZCASH_NETWORK, optionsFromZcashConf.testnet === '1' ? TESTNET : MAINNET);
}

if (!optionsFromZcashConf.rpcuser) store.set('rpcuser', uuid());
if (!optionsFromZcashConf.rpcpassword) store.set('rpcpassword', uuid());
if (!store.get('rpcuser')) store.set('rpcuser', uuid());
if (!store.get('rpcpassword')) store.set('rpcpassword', uuid());

const rpcCredentials = {
username: store.get('rpcuser'),
Expand All @@ -224,7 +239,7 @@ const runDaemon: () => Promise<?ChildProcess> = () => new Promise(async (resolve

store.set(DAEMON_PROCESS_PID, childProcess.pid);

childProcess.stdout.on('data', (data) => {
childProcess.stdout.on('data', () => {
if (!resolved) {
store.set(DAEMON_START_TIME, Date.now());
resolve(childProcess);
Expand Down
4 changes: 3 additions & 1 deletion services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ const api: APIMethods = METHODS.reduce(
.post(`http://${RPC.host}:${RPC.port}`, {
method: 'POST',
json: true,
auth: `${RPC.user}:${RPC.password}`,
headers: {
guilhermedecampo marked this conversation as resolved.
Show resolved Hide resolved
Authorization: `Basic ${Buffer.from(`${RPC.user}:${RPC.password}`).toString('base64')}`,
},
body: {
method,
jsonrpc: '2.0',
Expand Down