From ce04b447eeaf1ec60a378a0843a624c7320cf511 Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Fri, 6 Nov 2020 01:20:51 -0500 Subject: [PATCH 1/7] Add support for rpcUrl with basic auth when retrieving chainId on network creation --- ui/app/helpers/utils/util.js | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/ui/app/helpers/utils/util.js b/ui/app/helpers/utils/util.js index 229aac8266fc..4cce18dd394e 100644 --- a/ui/app/helpers/utils/util.js +++ b/ui/app/helpers/utils/util.js @@ -465,20 +465,26 @@ export function constructTxParams({ * or throws an error in case of failure. */ export async function jsonRpcRequest(rpcUrl, rpcMethod, rpcParams = []) { - const jsonRpcResponse = await window - .fetch(rpcUrl, { - method: 'POST', - body: JSON.stringify({ - id: Date.now().toString(), - jsonrpc: '2.0', - method: rpcMethod, - params: rpcParams, - }), - headers: { - 'Content-Type': 'application/json', - }, - cache: 'default', - }) + const headers = { + 'Content-Type': 'application/json' + }; + let matches = rpcUrl.match('(http[s]?):\/\/(.+):(.+)@(.+)'); + if (matches && matches[2] && matches[3]) { + const encoded = Buffer.from(`${matches[1]}:${matches[2]}`).toString('base64'); + headers['Authorization'] = `Basic ${encoded}`; + rpcUrl = `${matches[1]}://${matches[4]}`; + } + const jsonRpcResponse = await window.fetch(rpcUrl, { + method: 'POST', + body: JSON.stringify({ + id: Date.now().toString(), + jsonrpc: '2.0', + method: rpcMethod, + params: rpcParams, + }), + headers, + cache: 'default', + }) .then((httpResponse) => httpResponse.json()) if ( From 01d03d3a41099e56e912ded817b94952285966b4 Mon Sep 17 00:00:00 2001 From: jimthematrix Date: Fri, 6 Nov 2020 09:10:54 -0500 Subject: [PATCH 2/7] Update ui/app/helpers/utils/util.js for linting conventions Co-authored-by: Erik Marks <25517051+rekmarks@users.noreply.github.com> --- ui/app/helpers/utils/util.js | 47 ++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/ui/app/helpers/utils/util.js b/ui/app/helpers/utils/util.js index 4cce18dd394e..0e88b958f2b3 100644 --- a/ui/app/helpers/utils/util.js +++ b/ui/app/helpers/utils/util.js @@ -465,26 +465,37 @@ export function constructTxParams({ * or throws an error in case of failure. */ export async function jsonRpcRequest(rpcUrl, rpcMethod, rpcParams = []) { + let fetchUrl = rpcUrl const headers = { - 'Content-Type': 'application/json' - }; - let matches = rpcUrl.match('(http[s]?):\/\/(.+):(.+)@(.+)'); - if (matches && matches[2] && matches[3]) { - const encoded = Buffer.from(`${matches[1]}:${matches[2]}`).toString('base64'); - headers['Authorization'] = `Basic ${encoded}`; - rpcUrl = `${matches[1]}://${matches[4]}`; + 'Content-Type': 'application/json', } - const jsonRpcResponse = await window.fetch(rpcUrl, { - method: 'POST', - body: JSON.stringify({ - id: Date.now().toString(), - jsonrpc: '2.0', - method: rpcMethod, - params: rpcParams, - }), - headers, - cache: 'default', - }) + + // Convert basic auth URL component to Authorization header + const authMatches = rpcUrl.match('(http[s]?)://(.+):(.+)@(.+)') + // Confirm that we have matches, and a username and password + if (authMatches && authMatches[2] && authMatches[3]) { + // eslint-disable-next-line no-unused-vars + const [_, protocol, username, password, remainderUrl] = authMatches + + const encodedAuth = Buffer.from(`${username}:${password}`).toString( + 'base64', + ) + headers.Authorization = `Basic ${encodedAuth}` + fetchUrl = `${protocol}://${remainderUrl}` + } + + const jsonRpcResponse = await window + .fetch(fetchUrl, { + method: 'POST', + body: JSON.stringify({ + id: Date.now().toString(), + jsonrpc: '2.0', + method: rpcMethod, + params: rpcParams, + }), + headers, + cache: 'default', + }) .then((httpResponse) => httpResponse.json()) if ( From c475874b0c42ffe7cd3a9e1da7f1fa630eaae601 Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Fri, 6 Nov 2020 09:24:41 -0500 Subject: [PATCH 3/7] Replace Buffer with btoa for base64 encoding in browsers --- ui/app/helpers/utils/util.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ui/app/helpers/utils/util.js b/ui/app/helpers/utils/util.js index 0e88b958f2b3..63612f759c4e 100644 --- a/ui/app/helpers/utils/util.js +++ b/ui/app/helpers/utils/util.js @@ -469,21 +469,16 @@ export async function jsonRpcRequest(rpcUrl, rpcMethod, rpcParams = []) { const headers = { 'Content-Type': 'application/json', } - // Convert basic auth URL component to Authorization header const authMatches = rpcUrl.match('(http[s]?)://(.+):(.+)@(.+)') // Confirm that we have matches, and a username and password if (authMatches && authMatches[2] && authMatches[3]) { // eslint-disable-next-line no-unused-vars const [_, protocol, username, password, remainderUrl] = authMatches - - const encodedAuth = Buffer.from(`${username}:${password}`).toString( - 'base64', - ) + const encodedAuth = btoa(`${username}:${password}`) headers.Authorization = `Basic ${encodedAuth}` fetchUrl = `${protocol}://${remainderUrl}` } - const jsonRpcResponse = await window .fetch(fetchUrl, { method: 'POST', From d8f17df6b0439cdc2c231bebc6dae33b2cd4a044 Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Fri, 6 Nov 2020 10:47:24 -0500 Subject: [PATCH 4/7] Switch back to Buffer from btoa after linting errors --- ui/app/helpers/utils/util.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ui/app/helpers/utils/util.js b/ui/app/helpers/utils/util.js index 63612f759c4e..04bac71304c9 100644 --- a/ui/app/helpers/utils/util.js +++ b/ui/app/helpers/utils/util.js @@ -475,7 +475,9 @@ export async function jsonRpcRequest(rpcUrl, rpcMethod, rpcParams = []) { if (authMatches && authMatches[2] && authMatches[3]) { // eslint-disable-next-line no-unused-vars const [_, protocol, username, password, remainderUrl] = authMatches - const encodedAuth = btoa(`${username}:${password}`) + const encodedAuth = Buffer.from(`${username}:${password}`).toString( + 'base64', + ) headers.Authorization = `Basic ${encodedAuth}` fetchUrl = `${protocol}://${remainderUrl}` } From a3588d036490034f9c4bf724b15e3f08e8601470 Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Fri, 6 Nov 2020 11:03:46 -0500 Subject: [PATCH 5/7] Use native URL class to accomplish parsing and reconstructing --- ui/app/helpers/utils/util.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ui/app/helpers/utils/util.js b/ui/app/helpers/utils/util.js index 04bac71304c9..c6aa43658d0f 100644 --- a/ui/app/helpers/utils/util.js +++ b/ui/app/helpers/utils/util.js @@ -470,16 +470,15 @@ export async function jsonRpcRequest(rpcUrl, rpcMethod, rpcParams = []) { 'Content-Type': 'application/json', } // Convert basic auth URL component to Authorization header - const authMatches = rpcUrl.match('(http[s]?)://(.+):(.+)@(.+)') - // Confirm that we have matches, and a username and password - if (authMatches && authMatches[2] && authMatches[3]) { + const { origin, pathname, username, password } = new URL(rpcUrl) + // URLs containing username and password needs special processing + if (username && password) { // eslint-disable-next-line no-unused-vars - const [_, protocol, username, password, remainderUrl] = authMatches const encodedAuth = Buffer.from(`${username}:${password}`).toString( 'base64', ) headers.Authorization = `Basic ${encodedAuth}` - fetchUrl = `${protocol}://${remainderUrl}` + fetchUrl = `${origin}${pathname}` } const jsonRpcResponse = await window .fetch(fetchUrl, { From ec85411d8b7ee6d3c6929ba400eac6dcaa1bbdd0 Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Fri, 6 Nov 2020 11:34:54 -0500 Subject: [PATCH 6/7] Removed unnecessary lint override --- ui/app/helpers/utils/util.js | 1 - 1 file changed, 1 deletion(-) diff --git a/ui/app/helpers/utils/util.js b/ui/app/helpers/utils/util.js index c6aa43658d0f..40780304e2ea 100644 --- a/ui/app/helpers/utils/util.js +++ b/ui/app/helpers/utils/util.js @@ -473,7 +473,6 @@ export async function jsonRpcRequest(rpcUrl, rpcMethod, rpcParams = []) { const { origin, pathname, username, password } = new URL(rpcUrl) // URLs containing username and password needs special processing if (username && password) { - // eslint-disable-next-line no-unused-vars const encodedAuth = Buffer.from(`${username}:${password}`).toString( 'base64', ) From b44ea9893b1ede1d99b88f34e1caab8d49e7596a Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Sat, 7 Nov 2020 23:43:51 -0500 Subject: [PATCH 7/7] Added query string to the reconstructed rpcUrl --- ui/app/helpers/utils/util.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ui/app/helpers/utils/util.js b/ui/app/helpers/utils/util.js index 40780304e2ea..c4aae13f3666 100644 --- a/ui/app/helpers/utils/util.js +++ b/ui/app/helpers/utils/util.js @@ -470,14 +470,14 @@ export async function jsonRpcRequest(rpcUrl, rpcMethod, rpcParams = []) { 'Content-Type': 'application/json', } // Convert basic auth URL component to Authorization header - const { origin, pathname, username, password } = new URL(rpcUrl) + const { origin, pathname, username, password, search } = new URL(rpcUrl) // URLs containing username and password needs special processing if (username && password) { const encodedAuth = Buffer.from(`${username}:${password}`).toString( 'base64', ) headers.Authorization = `Basic ${encodedAuth}` - fetchUrl = `${origin}${pathname}` + fetchUrl = `${origin}${pathname}${search}` } const jsonRpcResponse = await window .fetch(fetchUrl, {