From 0980fe2de09671ad13c26b94c16e7d213a041b37 Mon Sep 17 00:00:00 2001 From: reyraa Date: Fri, 16 Jun 2017 12:54:30 +0200 Subject: [PATCH 1/3] Use Javascript URL method to normalize URL before setting active peer --- src/services/api/peers.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/services/api/peers.js b/src/services/api/peers.js index 20705896c..c87d46356 100644 --- a/src/services/api/peers.js +++ b/src/services/api/peers.js @@ -49,14 +49,21 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope, dialog) => * @method setActive */ setActive(network) { + const addHttp = (url) => { + const reg = /^(?:f|ht)tps?:\/\//i; + return reg.test(url) ? url : `http://${url}`; + }; + this.network = network; let conf = { }; if (network) { conf = network; if (network.address) { - conf.node = network.address.split(':')[1].replace('//', ''); - conf.port = network.address.match(/:([0-9]{1,5})\/?$/) && network.address.match(/:([0-9]{1,5})\/?$/)[1]; - conf.ssl = network.address.split(':')[0] === 'https'; + const normalizedUrl = new URL(addHttp(network.address)); + + conf.node = normalizedUrl.hostname; + conf.port = normalizedUrl.port; + conf.ssl = normalizedUrl.protocol === 'https'; } if (conf.testnet === undefined && conf.port !== undefined) { conf.testnet = conf.port === '7000'; From 131b2e287a7a269a59c5e96c0744bf2b44ca5fa2 Mon Sep 17 00:00:00 2001 From: reyraa Date: Fri, 16 Jun 2017 13:37:25 +0200 Subject: [PATCH 2/3] Add validation Node address before logging in --- src/components/login/login.js | 15 ++++++++++++++- src/components/login/login.pug | 8 ++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/components/login/login.js b/src/components/login/login.js index 32f9226c9..10230554e 100644 --- a/src/components/login/login.js +++ b/src/components/login/login.js @@ -49,7 +49,20 @@ app.component('login', { this.$cookies.remove('network'); } - this.$scope.$watch('$ctrl.input_passphrase', val => this.valid = this.Passphrase.isValidPassphrase(val)); + this.validity = { + url: true, + }; + + this.$scope.$watch('$ctrl.input_passphrase', val => this.validity.passphrase = this.Passphrase.isValidPassphrase(val)); + this.$scope.$watch('$ctrl.network.address', (val) => { + try { + const url = new URL(val); + this.validity.url = url.port !== ''; + } catch (e) { + this.validity.url = false; + } + }); + this.$timeout(this.devTestAccount.bind(this), 200); /** diff --git a/src/components/login/login.pug b/src/components/login/login.pug index 77337a76f..cbe45d13a 100644 --- a/src/components/login/login.pug +++ b/src/components/login/login.pug @@ -6,17 +6,17 @@ md-card md-select.network(ng-model='$ctrl.network', aria-label='Network') md-option(ng-repeat='network in $ctrl.networks', ng-value='network') {{ network.name }} div(ng-if='$ctrl.network.custom') - md-input-container.md-block + md-input-container.md-block(md-is-error='!$ctrl.validity.url') label.pass Node address input(type="text", ng-model="$ctrl.network.address") - md-input-container.md-block(md-is-error='$ctrl.valid === 0') + md-input-container.md-block(md-is-error='$ctrl.validity.passphrase === 0') label.pass Enter your passphrase input.passphrase(type="{{ $ctrl.show_passphrase ? 'text' : 'password' }}", ng-model='$ctrl.input_passphrase', ng-disabled='$ctrl.generatingNewPassphrase', autofocus) md-input-container.md-block md-checkbox.md-primary(ng-model="$ctrl.show_passphrase", aria-label="Show passphrase") Show passphrase md-content(layout='row', layout-align='center center') // md-button(ng-disabled='$ctrl.generatingNewPassphrase', ng-click='$ctrl.devTestAccount()') Dev Test Account - md-button.md-primary.new-account-button(ng-disabled='$ctrl.random || $ctrl.generatingNewPassphrase', + md-button.md-primary.new-account-button(ng-disabled='$ctrl.random || $ctrl.generatingNewPassphrase || !$ctrl.validity.url', data-open-dialog='new-account', data-options='{network: $ctrl.network}') NEW ACCOUNT - md-button.md-raised.md-primary.login-button(md-autofocus, ng-disabled='($ctrl.valid != undefined && $ctrl.valid !== 1) || $root.loggingIn', type='submit') Login + md-button.md-raised.md-primary.login-button(md-autofocus, ng-disabled='($ctrl.validity.passphrase !== 1 || !$ctrl.validity.url) || $root.loggingIn', type='submit') Login passphrase(ng-if='$ctrl.generatingNewPassphrase', data-on-save='onSave', data-target='primary-pass', data-ok-button-label='Login') From b439a6a84a6be12c028e679f3a1635c17ec17218 Mon Sep 17 00:00:00 2001 From: reyraa Date: Fri, 16 Jun 2017 13:40:03 +0200 Subject: [PATCH 3/3] Adapt unit tests --- test/components/login/login.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/components/login/login.spec.js b/test/components/login/login.spec.js index f396d8951..2f5c4a9fa 100644 --- a/test/components/login/login.spec.js +++ b/test/components/login/login.spec.js @@ -93,7 +93,7 @@ describe('Login controller', () => { expect(controller.valid).to.not.equal(1); controller.input_passphrase = VALID_PASSPHRASE; $scope.$apply(); - expect(controller.valid).to.equal(1); + expect(controller.validity.passphrase).to.equal(1); expect(spy).to.have.been.calledWith(); }); });