mirror of
https://github.com/bitwarden/browser.git
synced 2025-02-11 00:31:45 +01:00
messaging service implementation
This commit is contained in:
parent
6452cfaf7f
commit
825cc5fd37
@ -20,7 +20,7 @@ const routes: Routes = [
|
|||||||
{
|
{
|
||||||
path: 'vault',
|
path: 'vault',
|
||||||
component: VaultComponent,
|
component: VaultComponent,
|
||||||
canActivate: [AuthGuardService]
|
canActivate: [AuthGuardService],
|
||||||
},
|
},
|
||||||
{ path: 'hint', component: HintComponent },
|
{ path: 'hint', component: HintComponent },
|
||||||
];
|
];
|
||||||
|
@ -4,7 +4,27 @@ import {
|
|||||||
} from 'angular2-toaster';
|
} from 'angular2-toaster';
|
||||||
import { Angulartics2GoogleAnalytics } from 'angulartics2/ga';
|
import { Angulartics2GoogleAnalytics } from 'angulartics2/ga';
|
||||||
|
|
||||||
import { Component } from '@angular/core';
|
import {
|
||||||
|
Component,
|
||||||
|
OnInit,
|
||||||
|
} from '@angular/core';
|
||||||
|
import { Router } from '@angular/router';
|
||||||
|
|
||||||
|
import { ToasterService } from 'angular2-toaster';
|
||||||
|
import { Angulartics2 } from 'angulartics2';
|
||||||
|
|
||||||
|
import { BroadcasterService } from './services/broadcaster.service';
|
||||||
|
|
||||||
|
import { AuthService } from 'jslib/abstractions/auth.service';
|
||||||
|
import { CipherService } from 'jslib/abstractions/cipher.service';
|
||||||
|
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
||||||
|
import { FolderService } from 'jslib/abstractions/folder.service';
|
||||||
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
||||||
|
import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration.service';
|
||||||
|
import { SettingsService } from 'jslib/abstractions/settings.service';
|
||||||
|
import { SyncService } from 'jslib/abstractions/sync.service';
|
||||||
|
import { TokenService } from 'jslib/abstractions/token.service';
|
||||||
|
import { UserService } from 'jslib/abstractions/user.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-root',
|
selector: 'app-root',
|
||||||
@ -13,7 +33,7 @@ import { Component } from '@angular/core';
|
|||||||
<toaster-container [toasterconfig]="toasterConfig"></toaster-container>
|
<toaster-container [toasterconfig]="toasterConfig"></toaster-container>
|
||||||
<router-outlet></router-outlet>`,
|
<router-outlet></router-outlet>`,
|
||||||
})
|
})
|
||||||
export class AppComponent {
|
export class AppComponent implements OnInit {
|
||||||
toasterConfig: ToasterConfig = new ToasterConfig({
|
toasterConfig: ToasterConfig = new ToasterConfig({
|
||||||
showCloseButton: true,
|
showCloseButton: true,
|
||||||
mouseoverTimerStop: true,
|
mouseoverTimerStop: true,
|
||||||
@ -21,7 +41,59 @@ export class AppComponent {
|
|||||||
limit: 5,
|
limit: 5,
|
||||||
});
|
});
|
||||||
|
|
||||||
constructor(angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics) {
|
constructor(private angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics,
|
||||||
// ctor
|
private broadcasterService: BroadcasterService, private userService: UserService,
|
||||||
|
private tokenService: TokenService, private folderService: FolderService, private cryptoService: CryptoService,
|
||||||
|
private settingsService: SettingsService, private syncService: SyncService,
|
||||||
|
private passwordGenerationService: PasswordGenerationService, private cipherService: CipherService,
|
||||||
|
private authService: AuthService, private router: Router, private analytics: Angulartics2,
|
||||||
|
private toasterService: ToasterService, private i18nService: I18nService) { }
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
this.broadcasterService.subscribe(async (message: any) => {
|
||||||
|
switch (message.command) {
|
||||||
|
case 'loggedIn':
|
||||||
|
break;
|
||||||
|
case 'logout':
|
||||||
|
const userId = await this.userService.getUserId();
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
this.syncService.setLastSync(new Date(0)),
|
||||||
|
this.tokenService.clearToken(),
|
||||||
|
this.cryptoService.clearKeys(),
|
||||||
|
this.userService.clear(),
|
||||||
|
this.settingsService.clear(userId),
|
||||||
|
this.cipherService.clear(userId),
|
||||||
|
this.folderService.clear(userId),
|
||||||
|
this.passwordGenerationService.clear(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
this.doneLoggingOut(message.expired);
|
||||||
|
break;
|
||||||
|
case 'doneLoggingOut':
|
||||||
|
this.doneLoggingOut(message.expired);
|
||||||
|
break;
|
||||||
|
case 'locked':
|
||||||
|
break;
|
||||||
|
case 'unlocked':
|
||||||
|
break;
|
||||||
|
case 'syncStarted':
|
||||||
|
break;
|
||||||
|
case 'syncCompleted':
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private doneLoggingOut(expired: boolean) {
|
||||||
|
this.authService.logOut(() => {
|
||||||
|
this.analytics.eventTrack.next({ action: 'Logged Out' });
|
||||||
|
if (expired) {
|
||||||
|
this.toasterService.popAsync('warning', this.i18nService.t('loggedOut'),
|
||||||
|
this.i18nService.t('loginExpired'));
|
||||||
|
}
|
||||||
|
this.router.navigate(['login']);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,19 +5,23 @@ import {
|
|||||||
} from '@angular/router';
|
} from '@angular/router';
|
||||||
|
|
||||||
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
||||||
|
import { MessagingService } from 'jslib/abstractions/messaging.service';
|
||||||
import { UserService } from 'jslib/abstractions/user.service';
|
import { UserService } from 'jslib/abstractions/user.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthGuardService implements CanActivate {
|
export class AuthGuardService implements CanActivate {
|
||||||
constructor(private cryptoService: CryptoService, private userService: UserService, private router: Router) { }
|
constructor(private cryptoService: CryptoService, private userService: UserService, private router: Router,
|
||||||
|
private messagingService: MessagingService) { }
|
||||||
|
|
||||||
canActivate(): boolean {
|
async canActivate() {
|
||||||
if (!this.userService.isAuthenticated()) {
|
const isAuthed = await this.userService.isAuthenticated();
|
||||||
this.router.navigate(['vault']);
|
if (!isAuthed) {
|
||||||
|
this.messagingService.send('logout');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.cryptoService.getKey() == null) {
|
const key = await this.cryptoService.getKey();
|
||||||
|
if (key == null) {
|
||||||
this.router.navigate(['lock']);
|
this.router.navigate(['lock']);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
3
src/app/services/broadcaster.service.ts
Normal file
3
src/app/services/broadcaster.service.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import { EventEmitter } from '@angular/core';
|
||||||
|
|
||||||
|
export class BroadcasterService extends EventEmitter<any> { }
|
@ -14,6 +14,7 @@ import { DesktopStorageService } from '../../services/desktopStorage.service';
|
|||||||
import { I18nService } from '../../services/i18n.service';
|
import { I18nService } from '../../services/i18n.service';
|
||||||
|
|
||||||
import { AuthGuardService } from './auth-guard.service';
|
import { AuthGuardService } from './auth-guard.service';
|
||||||
|
import { BroadcasterService } from './broadcaster.service';
|
||||||
import { ValidationService } from './validation.service';
|
import { ValidationService } from './validation.service';
|
||||||
|
|
||||||
import { Analytics } from 'jslib/misc/analytics';
|
import { Analytics } from 'jslib/misc/analytics';
|
||||||
@ -67,7 +68,8 @@ webFrame.registerURLSchemeAsPrivileged('file');
|
|||||||
const i18nService = new I18nService(window.navigator.language, './locales');
|
const i18nService = new I18nService(window.navigator.language, './locales');
|
||||||
const utilsService = new UtilsService();
|
const utilsService = new UtilsService();
|
||||||
const platformUtilsService = new DesktopPlatformUtilsService(i18nService);
|
const platformUtilsService = new DesktopPlatformUtilsService(i18nService);
|
||||||
const messagingService = new DesktopMessagingService();
|
const broadcasterService = new BroadcasterService();
|
||||||
|
const messagingService = new DesktopMessagingService(broadcasterService);
|
||||||
const storageService: StorageServiceAbstraction = new DesktopStorageService();
|
const storageService: StorageServiceAbstraction = new DesktopStorageService();
|
||||||
const secureStorageService: StorageServiceAbstraction = new DesktopSecureStorageService();
|
const secureStorageService: StorageServiceAbstraction = new DesktopSecureStorageService();
|
||||||
const constantsService = new ConstantsService({}, 0);
|
const constantsService = new ConstantsService({}, 0);
|
||||||
@ -136,6 +138,9 @@ function initFactory(i18n: I18nService, platformUtils: DesktopPlatformUtilsServi
|
|||||||
{ provide: ApiServiceAbstraction, useValue: apiService },
|
{ provide: ApiServiceAbstraction, useValue: apiService },
|
||||||
{ provide: SyncServiceAbstraction, useValue: syncService },
|
{ provide: SyncServiceAbstraction, useValue: syncService },
|
||||||
{ provide: UserServiceAbstraction, useValue: userService },
|
{ provide: UserServiceAbstraction, useValue: userService },
|
||||||
|
{ provide: MessagingServiceAbstraction, useValue: messagingService },
|
||||||
|
{ provide: BroadcasterService, useValue: broadcasterService },
|
||||||
|
{ provide: SettingsServiceAbstraction, useValue: settingsService },
|
||||||
{
|
{
|
||||||
provide: APP_INITIALIZER,
|
provide: APP_INITIALIZER,
|
||||||
useFactory: initFactory,
|
useFactory: initFactory,
|
||||||
|
@ -594,5 +594,11 @@
|
|||||||
},
|
},
|
||||||
"featureUnavailable": {
|
"featureUnavailable": {
|
||||||
"message": "Feature Unavailable"
|
"message": "Feature Unavailable"
|
||||||
|
},
|
||||||
|
"loggedOut": {
|
||||||
|
"message": "Logged out"
|
||||||
|
},
|
||||||
|
"loginExpired": {
|
||||||
|
"message": "Your login session has expired."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
14
src/main.ts
14
src/main.ts
@ -30,6 +30,20 @@ import { I18nService } from './services/i18n.service';
|
|||||||
const i18nService = new I18nService('en', './locales/');
|
const i18nService = new I18nService('en', './locales/');
|
||||||
i18nService.init().then(() => { });
|
i18nService.init().then(() => { });
|
||||||
|
|
||||||
|
ipcMain.on('messagingService', async (event: any, message: any) => {
|
||||||
|
switch (message.command) {
|
||||||
|
case 'loggedIn':
|
||||||
|
break;
|
||||||
|
case 'logout':
|
||||||
|
break;
|
||||||
|
case 'syncCompleted':
|
||||||
|
console.log('sync completed!!');
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let win: BrowserWindow;
|
let win: BrowserWindow;
|
||||||
const args = process.argv.slice(1);
|
const args = process.argv.slice(1);
|
||||||
const watch = args.some((val) => val === '--watch');
|
const watch = args.some((val) => val === '--watch');
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
import {
|
import { ipcRenderer } from 'electron';
|
||||||
MessagingService,
|
|
||||||
PlatformUtilsService,
|
import { MessagingService } from 'jslib/abstractions';
|
||||||
} from 'jslib/abstractions';
|
|
||||||
|
import { BroadcasterService } from '../app/services/broadcaster.service';
|
||||||
|
|
||||||
export class DesktopMessagingService implements MessagingService {
|
export class DesktopMessagingService implements MessagingService {
|
||||||
|
constructor(private broadcasterService: BroadcasterService) { }
|
||||||
|
|
||||||
send(subscriber: string, arg: any = {}) {
|
send(subscriber: string, arg: any = {}) {
|
||||||
const message = Object.assign({}, { command: subscriber }, arg);
|
const message = Object.assign({}, { command: subscriber }, arg);
|
||||||
// TODO
|
ipcRenderer.send('messagingService', message);
|
||||||
|
this.broadcasterService.emit(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user