diff options
| author | Erik Larkö <erik@mullvad.net> | 2017-07-03 13:41:07 +0200 |
|---|---|---|
| committer | Erik Larkö <erik@mullvad.net> | 2017-07-10 07:18:33 +0200 |
| commit | 9c65adb7a677e9358c178e47e5e786b1e60b127f (patch) | |
| tree | 6ec964ced2fe548a70fefa9435aef4c27c6c96f6 /app | |
| parent | 0b45a621235e66f8145f7b842592acf713be8690 (diff) | |
| download | mullvadvpn-9c65adb7a677e9358c178e47e5e786b1e60b127f.tar.xz mullvadvpn-9c65adb7a677e9358c178e47e5e786b1e60b127f.zip | |
Autologin
Diffstat (limited to 'app')
| -rw-r--r-- | app/app.js | 4 | ||||
| -rw-r--r-- | app/lib/backend.js | 46 | ||||
| -rw-r--r-- | app/lib/ipc-facade.js | 12 |
3 files changed, 61 insertions, 1 deletions
diff --git a/app/app.js b/app/app.js index 7710ff1acf..5ff241089f 100644 --- a/app/app.js +++ b/app/app.js @@ -23,10 +23,12 @@ const store = configureStore(initialState, memoryHistory); // Backend ////////////////////////////////////////////////////////////////////////// const backend = new Backend(store); - ipcRenderer.on('backend-info', (_event, args) => { backend.setLocation(args.addr); backend.sync(); + if(store.getState().settings.autoSecure) { + backend.autologin(); + } }); ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// diff --git a/app/lib/backend.js b/app/lib/backend.js index a8774dc599..9513a3f08b 100644 --- a/app/lib/backend.js +++ b/app/lib/backend.js @@ -186,6 +186,52 @@ export class Backend { }); } + autologin() { + log.info('Attempting to log in automatically'); + + this._store.dispatch(accountActions.loginChange({ + accountNumber: null, + status: 'logging in', + error: null, + })); + + return this._ipc.getAccount() + .then( accountNumber => { + if (!accountNumber) { + throw new Error('No account set in the backend, failing autologin'); + } + log.debug('The backend had an account number stored:', accountNumber); + + this._store.dispatch(accountActions.loginChange({ + accountNumber: accountNumber, + status: 'logging in', + error: null, + })); + + return this._ipc.getAccountData(accountNumber); + }) + .then( accountData => { + log.info('The stored account number still exists', accountData); + + this._store.dispatch(accountActions.loginChange({ + status: 'ok', + paidUntil: accountData.paid_until, + error: null, + })); + + this._store.dispatch(push('/connect')); + }) + .catch( e => { + log.warn('Unable to autologin', e); + + this._store.dispatch(accountActions.loginChange({ + status: 'none', + error: new BackendError('INVALID_ACCOUNT'), + })); + + this._store.dispatch(push('/')); + }); + } logout() { // @TODO: What does it mean for a logout to be successful or failed? diff --git a/app/lib/ipc-facade.js b/app/lib/ipc-facade.js index ff2b7050ed..b5541e4297 100644 --- a/app/lib/ipc-facade.js +++ b/app/lib/ipc-facade.js @@ -24,6 +24,7 @@ export type BackendState = 'secured' | 'unsecured'; export interface IpcFacade { getAccountData(AccountNumber): Promise<AccountData>, + getAccount(): Promise<?AccountNumber>, setAccount(accountNumber: AccountNumber): Promise<void>, setCountry(address: string): Promise<void>, connect(): Promise<void>, @@ -53,6 +54,17 @@ export class RealIpc implements IpcFacade { }); } + getAccount(): Promise<?AccountNumber> { + return this._ipc.send('get_account') + .then( raw => { + if (raw === undefined || raw === null || typeof raw === 'string') { + return raw; + } else { + throw new InvalidReply(raw); + } + }); + } + setAccount(accountNumber: AccountNumber): Promise<void> { return this._ipc.send('set_account', accountNumber) .then(this._ignoreResponse); |
