|
| 1 | +// Copyright 2015, 2016 Ethcore (UK) Ltd. |
| 2 | +// This file is part of Parity. |
| 3 | + |
| 4 | +// Parity is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// Parity is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Parity. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import Api from './api'; |
| 18 | + |
| 19 | +const sysuiToken = window.localStorage.getItem('sysuiToken'); |
| 20 | + |
| 21 | +export default class SecureApi extends Api { |
| 22 | + constructor (url, updateTokenCallback) { |
| 23 | + super(new Api.Transport.Ws(url, sysuiToken)); |
| 24 | + |
| 25 | + this._isConnecting = true; |
| 26 | + this._connectState = 0; |
| 27 | + this._needsToken = false; |
| 28 | + this._updateTokenCallback = (token) => updateTokenCallback(token); |
| 29 | + |
| 30 | + this._followConnection(); |
| 31 | + } |
| 32 | + |
| 33 | + _followConnection = () => { |
| 34 | + const nextTick = () => setTimeout(this._followConnection, 250); |
| 35 | + const setToken = () => { |
| 36 | + window.localStorage.setItem('sysuiToken', this._transport.token); |
| 37 | + this._updateTokenCallback(this._transport.token); |
| 38 | + }; |
| 39 | + const setManual = () => { |
| 40 | + this._connectedState = 100; |
| 41 | + this._needsToken = true; |
| 42 | + this._isConnecting = false; |
| 43 | + }; |
| 44 | + const lastError = this._transport.lastError; |
| 45 | + const isConnected = this._transport.isConnected; |
| 46 | + |
| 47 | + switch (this._connectState) { |
| 48 | + // token = <passed via constructor> |
| 49 | + case 0: |
| 50 | + if (isConnected) { |
| 51 | + this._isConnecting = false; |
| 52 | + return setToken(); |
| 53 | + } else if (lastError) { |
| 54 | + this.updateToken('initial', 1); |
| 55 | + } |
| 56 | + break; |
| 57 | + |
| 58 | + // token = 'initial' |
| 59 | + case 1: |
| 60 | + if (isConnected) { |
| 61 | + this._connectState = 2; |
| 62 | + this.personal |
| 63 | + .generateAuthorizationToken() |
| 64 | + .then((token) => { |
| 65 | + this.updateToken(token, 2); |
| 66 | + }) |
| 67 | + .catch((error) => { |
| 68 | + console.error('_followConnection', error); |
| 69 | + setManual(); |
| 70 | + }); |
| 71 | + return; |
| 72 | + } else if (lastError) { |
| 73 | + return setManual(); |
| 74 | + } |
| 75 | + break; |
| 76 | + |
| 77 | + // token = <personal_generateAuthorizationToken> |
| 78 | + case 2: |
| 79 | + if (isConnected) { |
| 80 | + this._isConnecting = false; |
| 81 | + return setToken(); |
| 82 | + } else if (lastError) { |
| 83 | + return setManual(); |
| 84 | + } |
| 85 | + break; |
| 86 | + } |
| 87 | + |
| 88 | + nextTick(); |
| 89 | + } |
| 90 | + |
| 91 | + updateToken (token, connectedState = 0) { |
| 92 | + this._connectState = connectedState; |
| 93 | + this._transport.updateToken(token.replace(/[^a-zA-Z0-9]/g, '')); |
| 94 | + this._followConnection(); |
| 95 | + } |
| 96 | + |
| 97 | + get isConnecting () { |
| 98 | + return this._isConnecting; |
| 99 | + } |
| 100 | + |
| 101 | + get isConnected () { |
| 102 | + return this._transport.isConnected; |
| 103 | + } |
| 104 | + |
| 105 | + get needsToken () { |
| 106 | + return this._needsToken; |
| 107 | + } |
| 108 | + |
| 109 | + get secureToken () { |
| 110 | + return this._transport.token; |
| 111 | + } |
| 112 | +} |
0 commit comments