2018-06-27 23:50:31 +02:00
|
|
|
import {
|
|
|
|
Component,
|
2018-10-08 20:23:30 +02:00
|
|
|
NgZone,
|
2018-06-27 23:50:31 +02:00
|
|
|
OnDestroy,
|
|
|
|
OnInit,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
import { Angulartics2 } from 'angulartics2';
|
|
|
|
|
|
|
|
import { ApiService } from 'jslib/abstractions/api.service';
|
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
|
|
|
|
|
|
|
import { TwoFactorProviderType } from 'jslib/enums/twoFactorProviderType';
|
2018-10-08 20:23:30 +02:00
|
|
|
|
|
|
|
import { PasswordVerificationRequest } from 'jslib/models/request/passwordVerificationRequest';
|
|
|
|
import { UpdateTwoFactorU2fDeleteRequest } from 'jslib/models/request/updateTwoFactorU2fDeleteRequest';
|
2018-06-27 23:50:31 +02:00
|
|
|
import { UpdateTwoFactorU2fRequest } from 'jslib/models/request/updateTwoFactorU2fRequest';
|
2018-07-21 06:26:16 +02:00
|
|
|
import {
|
|
|
|
ChallengeResponse,
|
|
|
|
TwoFactorU2fResponse,
|
|
|
|
} from 'jslib/models/response/twoFactorU2fResponse';
|
2018-06-27 23:50:31 +02:00
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
import { TwoFactorBaseComponent } from './two-factor-base.component';
|
|
|
|
|
2018-06-27 23:50:31 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-two-factor-u2f',
|
|
|
|
templateUrl: 'two-factor-u2f.component.html',
|
|
|
|
})
|
2018-06-28 05:55:50 +02:00
|
|
|
export class TwoFactorU2fComponent extends TwoFactorBaseComponent implements OnInit, OnDestroy {
|
2018-07-18 23:10:26 +02:00
|
|
|
type = TwoFactorProviderType.U2f;
|
2018-10-08 20:23:30 +02:00
|
|
|
name: string;
|
|
|
|
keys: any[];
|
|
|
|
keyIdAvailable: number = null;
|
|
|
|
keysConfiguredCount = 0;
|
2018-06-27 23:50:31 +02:00
|
|
|
u2fError: boolean;
|
|
|
|
u2fListening: boolean;
|
|
|
|
u2fResponse: string;
|
2018-10-08 20:23:30 +02:00
|
|
|
challengePromise: Promise<ChallengeResponse>;
|
2018-06-27 23:50:31 +02:00
|
|
|
formPromise: Promise<any>;
|
|
|
|
|
2018-06-28 04:05:33 +02:00
|
|
|
private u2fScript: HTMLScriptElement;
|
2018-06-27 23:50:31 +02:00
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
constructor(apiService: ApiService, i18nService: I18nService,
|
|
|
|
analytics: Angulartics2, toasterService: ToasterService,
|
2018-10-08 20:23:30 +02:00
|
|
|
platformUtilsService: PlatformUtilsService, private ngZone: NgZone) {
|
2018-07-18 23:10:26 +02:00
|
|
|
super(apiService, i18nService, analytics, toasterService, platformUtilsService);
|
2018-06-27 23:50:31 +02:00
|
|
|
this.u2fScript = window.document.createElement('script');
|
|
|
|
this.u2fScript.src = 'scripts/u2f.js';
|
|
|
|
this.u2fScript.async = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
window.document.body.appendChild(this.u2fScript);
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
window.document.body.removeChild(this.u2fScript);
|
|
|
|
}
|
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
auth(authResponse: any) {
|
|
|
|
super.auth(authResponse);
|
|
|
|
this.processResponse(authResponse.response);
|
2018-06-27 23:50:31 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
submit() {
|
2018-10-08 20:23:30 +02:00
|
|
|
if (this.u2fResponse == null || this.keyIdAvailable == null) {
|
|
|
|
// Should never happen.
|
|
|
|
return Promise.reject();
|
2018-06-27 23:50:31 +02:00
|
|
|
}
|
2018-06-28 05:55:50 +02:00
|
|
|
const request = new UpdateTwoFactorU2fRequest();
|
|
|
|
request.masterPasswordHash = this.masterPasswordHash;
|
|
|
|
request.deviceResponse = this.u2fResponse;
|
2018-10-08 20:23:30 +02:00
|
|
|
request.id = this.keyIdAvailable;
|
|
|
|
request.name = this.name;
|
2018-06-28 05:55:50 +02:00
|
|
|
|
|
|
|
return super.enable(async () => {
|
|
|
|
this.formPromise = this.apiService.putTwoFactorU2f(request);
|
|
|
|
const response = await this.formPromise;
|
|
|
|
await this.processResponse(response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-08 20:23:30 +02:00
|
|
|
disable() {
|
|
|
|
return super.disable(this.formPromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
async remove(key: any) {
|
|
|
|
if (this.keysConfiguredCount <= 1 || key.removePromise != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const name = key.name != null ? key.name : this.i18nService.t('u2fkeyX', key.id);
|
|
|
|
const confirmed = await this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t('removeU2fConfirmation'), name,
|
|
|
|
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
|
|
|
|
if (!confirmed) {
|
2018-06-27 23:50:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-10-08 20:23:30 +02:00
|
|
|
const request = new UpdateTwoFactorU2fDeleteRequest();
|
|
|
|
request.id = key.id;
|
|
|
|
request.masterPasswordHash = this.masterPasswordHash;
|
|
|
|
try {
|
|
|
|
key.removePromise = this.apiService.deleteTwoFactorU2f(request);
|
|
|
|
const response = await key.removePromise;
|
|
|
|
key.removePromise = null;
|
|
|
|
await this.processResponse(response);
|
|
|
|
} catch { }
|
|
|
|
}
|
2018-06-27 23:50:31 +02:00
|
|
|
|
2018-10-08 20:23:30 +02:00
|
|
|
async readKey() {
|
|
|
|
if (this.keyIdAvailable == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const request = new PasswordVerificationRequest();
|
|
|
|
request.masterPasswordHash = this.masterPasswordHash;
|
|
|
|
try {
|
|
|
|
this.challengePromise = this.apiService.getTwoFactorU2fChallenge(request);
|
|
|
|
const challenge = await this.challengePromise;
|
|
|
|
this.readDevice(challenge);
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
|
|
|
|
private readDevice(u2fChallenge: ChallengeResponse) {
|
2018-06-27 23:50:31 +02:00
|
|
|
// tslint:disable-next-line
|
|
|
|
console.log('listening for key...');
|
2018-10-08 20:23:30 +02:00
|
|
|
this.resetU2f(true);
|
|
|
|
(window as any).u2f.register(u2fChallenge.appId, [{
|
|
|
|
version: u2fChallenge.version,
|
|
|
|
challenge: u2fChallenge.challenge,
|
|
|
|
}], [], (data: any) => {
|
|
|
|
this.ngZone.run(() => {
|
|
|
|
this.u2fListening = false;
|
|
|
|
if (data.errorCode) {
|
|
|
|
this.u2fError = true;
|
|
|
|
// tslint:disable-next-line
|
|
|
|
console.log('error: ' + data.errorCode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.u2fResponse = JSON.stringify(data);
|
|
|
|
});
|
|
|
|
}, 15);
|
|
|
|
}
|
2018-06-27 23:50:31 +02:00
|
|
|
|
2018-10-08 20:23:30 +02:00
|
|
|
private resetU2f(listening = false) {
|
2018-06-27 23:50:31 +02:00
|
|
|
this.u2fResponse = null;
|
|
|
|
this.u2fError = false;
|
2018-10-08 20:23:30 +02:00
|
|
|
this.u2fListening = listening;
|
2018-06-27 23:50:31 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
private processResponse(response: TwoFactorU2fResponse) {
|
2018-10-08 20:23:30 +02:00
|
|
|
this.resetU2f();
|
|
|
|
this.keys = [];
|
|
|
|
this.keyIdAvailable = null;
|
|
|
|
this.name = null;
|
|
|
|
this.keysConfiguredCount = 0;
|
|
|
|
for (let i = 1; i <= 5; i++) {
|
|
|
|
if (response.keys != null) {
|
|
|
|
const key = response.keys.filter((k) => k.id === i);
|
|
|
|
if (key.length > 0) {
|
|
|
|
this.keysConfiguredCount++;
|
|
|
|
this.keys.push({
|
|
|
|
id: i, name: key[0].name,
|
|
|
|
configured: true,
|
|
|
|
compromised: key[0].compromised,
|
|
|
|
removePromise: null,
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.keys.push({ id: i, name: null, configured: false, compromised: false, removePromise: null });
|
|
|
|
if (this.keyIdAvailable == null) {
|
|
|
|
this.keyIdAvailable = i;
|
|
|
|
}
|
|
|
|
}
|
2018-06-27 23:50:31 +02:00
|
|
|
this.enabled = response.enabled;
|
|
|
|
}
|
|
|
|
}
|