summaryrefslogtreecommitdiffhomepage
path: root/app/lib/ipc-facade.js
diff options
context:
space:
mode:
authorErik Larkö <erik@mullvad.net>2017-10-13 11:31:57 +0200
committerErik Larkö <erik@mullvad.net>2017-10-13 11:31:57 +0200
commit992f7946dbe9004d88a331b97ea9e030701599ea (patch)
tree858ef774e0751435f2b039af846cef4d29c37a0c /app/lib/ipc-facade.js
parent8f5c5aa9a433a65d8a7f6f6d8e4211f5a82068f5 (diff)
downloadmullvadvpn-992f7946dbe9004d88a331b97ea9e030701599ea.tar.xz
mullvadvpn-992f7946dbe9004d88a331b97ea9e030701599ea.zip
Move the auth flow into backend.js and write tests
Diffstat (limited to 'app/lib/ipc-facade.js')
-rw-r--r--app/lib/ipc-facade.js161
1 files changed, 52 insertions, 109 deletions
diff --git a/app/lib/ipc-facade.js b/app/lib/ipc-facade.js
index 9e191ddf30..2d63f4b9ba 100644
--- a/app/lib/ipc-facade.js
+++ b/app/lib/ipc-facade.js
@@ -3,7 +3,6 @@
import JsonRpcWs, { InvalidReply } from './jsonrpc-ws-ipc';
import { object, string, arrayOf, number } from 'validated/schema';
import { validate } from 'validated/object';
-import log from 'electron-log';
import type { Coordinate2d } from '../types';
@@ -32,26 +31,9 @@ export type RelayEndpoint = {
protocol: 'tcp' | 'udp',
};
-export type IpcCredentials = {
- connectionString: string,
- sharedSecret: string,
-};
-
-export function parseIpcCredentials(data: string): ?IpcCredentials {
- const [connectionString, sharedSecret] = data.split('\n', 2);
- if(connectionString && sharedSecret) {
- return {
- connectionString,
- sharedSecret,
- };
- } else {
- return null;
- }
-}
-
export interface IpcFacade {
- setCredentials(IpcCredentials): void,
+ setConnectionString(string): void,
getAccountData(AccountToken): Promise<AccountData>,
getAccount(): Promise<?AccountToken>,
setAccount(accountToken: ?AccountToken): Promise<void>,
@@ -62,27 +44,20 @@ export interface IpcFacade {
getLocation(): Promise<Location>,
getState(): Promise<BackendState>,
registerStateListener((BackendState) => void): void,
+ setCloseConnectionHandler(() => void): void,
+ auth(sharedSecret: string): Promise<void>,
}
export class RealIpc implements IpcFacade {
_ipc: JsonRpcWs;
- _credentials: ?IpcCredentials;
- _authenticationPromise: ?Promise<void>;
- constructor(credentials: IpcCredentials) {
- this._credentials = credentials;
- this._ipc = new JsonRpcWs(credentials.connectionString);
-
- // force to re-authenticate when connection closed
- this._ipc.setCloseConnectionHandler(() => {
- this._authenticationPromise = null;
- });
+ constructor(connectionString: string) {
+ this._ipc = new JsonRpcWs(connectionString);
}
- setCredentials(credentials: IpcCredentials) {
- this._credentials = credentials;
- this._ipc.setConnectionString(credentials.connectionString);
+ setConnectionString(str: string) {
+ this._ipc.setConnectionString(str);
}
getAccountData(accountToken: AccountToken): Promise<AccountData> {
@@ -100,23 +75,19 @@ export class RealIpc implements IpcFacade {
}
getAccount(): Promise<?AccountToken> {
- return this._ensureAuthenticated().then(() => {
- return this._ipc.send('get_account')
- .then( raw => {
- if (raw === undefined || raw === null || typeof raw === 'string') {
- return raw;
- } else {
- throw new InvalidReply(raw);
- }
- });
- });
+ return this._ipc.send('get_account')
+ .then( raw => {
+ if (raw === undefined || raw === null || typeof raw === 'string') {
+ return raw;
+ } else {
+ throw new InvalidReply(raw);
+ }
+ });
}
setAccount(accountToken: ?AccountToken): Promise<void> {
- return this._ensureAuthenticated().then(() => {
- return this._ipc.send('set_account', accountToken)
- .then(this._ignoreResponse);
- });
+ return this._ipc.send('set_account', accountToken)
+ .then(this._ignoreResponse);
}
_ignoreResponse(_response: mixed): void {
@@ -124,60 +95,48 @@ export class RealIpc implements IpcFacade {
}
setCustomRelay(relayEndpoint: RelayEndpoint): Promise<void> {
- return this._ensureAuthenticated().then(() => {
- return this._ipc.send('set_custom_relay', [relayEndpoint])
- .then(this._ignoreResponse);
- });
+ return this._ipc.send('set_custom_relay', [relayEndpoint])
+ .then(this._ignoreResponse);
}
connect(): Promise<void> {
- return this._ensureAuthenticated().then(() => {
- return this._ipc.send('connect')
- .then(this._ignoreResponse);
- });
+ return this._ipc.send('connect')
+ .then(this._ignoreResponse);
}
disconnect(): Promise<void> {
- return this._ensureAuthenticated().then(() => {
- return this._ipc.send('disconnect')
- .then(this._ignoreResponse);
- });
+ return this._ipc.send('disconnect')
+ .then(this._ignoreResponse);
}
getIp(): Promise<Ip> {
- return this._ensureAuthenticated().then(() => {
- return this._ipc.send('get_ip')
- .then(raw => {
- if (typeof raw === 'string' && raw) {
- return raw;
- } else {
- throw new InvalidReply(raw, 'Expected a string');
- }
- });
- });
+ return this._ipc.send('get_ip')
+ .then(raw => {
+ if (typeof raw === 'string' && raw) {
+ return raw;
+ } else {
+ throw new InvalidReply(raw, 'Expected a string');
+ }
+ });
}
getLocation(): Promise<Location> {
- return this._ensureAuthenticated().then(() => {
- return this._ipc.send('get_location')
- .then(raw => {
- try {
- const validated: any = validate(LocationSchema, raw);
- return (validated: Location);
- } catch (e) {
- throw new InvalidReply(raw, e);
- }
- });
- });
+ return this._ipc.send('get_location')
+ .then(raw => {
+ try {
+ const validated: any = validate(LocationSchema, raw);
+ return (validated: Location);
+ } catch (e) {
+ throw new InvalidReply(raw, e);
+ }
+ });
}
getState(): Promise<BackendState> {
- return this._ensureAuthenticated().then(() => {
- return this._ipc.send('get_state')
- .then(raw => {
- return this._parseBackendState(raw);
- });
- });
+ return this._ipc.send('get_state')
+ .then(raw => {
+ return this._parseBackendState(raw);
+ });
}
_parseBackendState(raw: mixed): BackendState {
@@ -200,35 +159,19 @@ export class RealIpc implements IpcFacade {
}
registerStateListener(listener: (BackendState) => void) {
- this._ensureAuthenticated().then(() => {
- this._ipc.on('new_state', (rawEvent) => {
- const parsedEvent : BackendState = this._parseBackendState(rawEvent);
+ this._ipc.on('new_state', (rawEvent) => {
+ const parsedEvent : BackendState = this._parseBackendState(rawEvent);
- listener(parsedEvent);
- });
+ listener(parsedEvent);
});
}
- _ensureAuthenticated(): Promise<void> {
- if(this._credentials) {
- const credentials = this._credentials;
- if(!this._authenticationPromise) {
- this._authenticationPromise = this._authenticate(credentials.sharedSecret);
- }
- return this._authenticationPromise;
- } else {
- return Promise.reject(new Error('Missing authentication credentials.'));
- }
+ auth(sharedSecret: string): Promise<void> {
+ return this._ipc.send('auth', sharedSecret)
+ .then(this._ignoreResponse);
}
- _authenticate(sharedSecret: string): Promise<void> {
- return this._ipc.send('auth', sharedSecret)
- .then(() => {
- log.info('Authenticated with backend');
- })
- .catch((e) => {
- log.error('Failed to authenticate with backend: ', e.message);
- throw e;
- });
+ setCloseConnectionHandler(handler: () => void) {
+ console.log('appa', handler);
}
}