Newer
Older
import { Component, Input, OnInit } from '@angular/core';
import { LoadingController, ModalController } from '@ionic/angular';
import { of } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { NotificationService } from '../notification.service';
import { TranslateService } from '@ngx-translate/core';
import { ActionsService } from '../actions.service';
import { SignaturesContentService } from '../signatures.service';

Florian Azizian
committed
import { AuthService } from '../auth.service';
import { FunctionsService } from '../functions.service';
@Component({
selector: 'signature-method-modal',
templateUrl: 'signature-method-modal.component.html',
styleUrls: ['./signature-method-modal.component.scss']
})
export class SignatureMethodModalComponent implements OnInit {
filters: any = {
// onlySmartcards: false,
expired: false,
onlyWithPrivateKey: true
};
provider: any = null;
cert: any = null;
certPem: any = null;
privateKey: any = null;
certificate: any;
signatureLength: any = null;

Florian Azizian
committed
@Input() signatureMode: string;
public modalController: ModalController,
public http: HttpClient,
public translate: TranslateService,
public notificationService: NotificationService,
public loadingController: LoadingController,
public signaturesService: SignaturesContentService,
public actionsService: ActionsService,

Florian Azizian
committed
private functionsService: FunctionsService,
public authService: AuthService
async ngOnInit(): Promise<void> {
this.signatures = await this.actionsService.getElementsFromDoc();

Alex ORLUC
committed

Florian Azizian
committed
const signatureModeData = this.authService.signatureRoles.filter((mode: any) => mode.id === this.signatureMode)[0];
if (!this.functionsService.empty(signatureModeData.issuer)) {
this.filters.issuerDNMatch = new RegExp(signatureModeData.issuer, 'i');
}
if (!this.functionsService.empty(signatureModeData.subject)) {
this.filters.subjectDNMatch = new RegExp(signatureModeData.subject, 'i');
}
if (!this.functionsService.empty(signatureModeData.keyUsage)) {
this.filters.keyUsage.push(signatureModeData.keyUsage);
}

Florian Azizian
committed
}

Alex ORLUC
committed
async certificateChosen(certData: any) {
this.loadingController.create({
message: this.translate.instant('lang.processing'),
spinner: 'dots'
}).then(async (load: HTMLIonLoadingElement) => {
load.present();
try {
this.provider = await certData.detail.server.getCrypto(certData.detail.providerId);
this.cert = await this.provider.certStorage.getItem(certData.detail.certificateId);
this.certPem = await this.provider.certStorage.exportCert('pem', this.cert);
this.privateKey = await this.provider.keyStorage.getItem(certData.detail.privateKeyId);
} catch (e) {
this.notificationService.error('lang.fortifyReadException');
load.dismiss();
this.modalController.dismiss(false);
return;
}
this.certificate = {
const result = await this.sendAndSign();
this.modalController.dismiss(result);

Alex ORLUC
committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
async sendAndSign() {
let allSignaturesComplete: boolean = false;
let res: any = {};
while (!allSignaturesComplete) {
let signDocComplete: any = false;
while (signDocComplete === false) {
res = await this.fusionStampAndGenerateSignature(res.tmpUniqueId);
if (res === null) {
return false;
} else if (res !== false) {
signDocComplete = await this.signDocument(res.hashDocument, res.signatureContentLength, res.signatureFieldName, res.tmpUniqueId);
console.log('signDocComplete', signDocComplete);
if (signDocComplete === true) {
this.signatures.shift();
allSignaturesComplete = this.signatures.length === 0;
} else {
return false;
}
} else {
return false;
}
}
}
return allSignaturesComplete;
}
async fusionStampAndGenerateSignature(tmpUniqueId: string = null) {
let res: any = {};
res = await this.actionsService.sendDocument(null, this.certificate, this.signatureLength, tmpUniqueId, this.signatures);
return res;
}
signDocument(hashDocument: any, eSignatureLength: any, signatureFieldName: any, tmpUniqueId: string) {
return new Promise(async (resolve) => {
const alg = {
name: this.privateKey.algorithm.name,
hash: 'SHA-256',
};
const hashDocumentHex = this.fromHex(hashDocument);
try {
hashSignature = await this.provider.subtle.sign(alg, this.privateKey, hashDocumentHex);
} catch (e) {
this.notificationService.error('lang.fortifyReadException');
resolve(false);
return of(false);
}

Alex ORLUC
committed
const note = {
note: this.note
};
certificate: this.certPem,
hashSignature: this.toHex(hashSignature),

Florian Azizian
committed
signatureContentLength: eSignatureLength,
signatureFieldName: signatureFieldName,
tmpUniqueId: tmpUniqueId,

Alex ORLUC
committed
const objToSend = {...note, ...objEsign };
this.http.put('../rest/documents/' + this.signaturesService.mainDocumentId + '/actions/' + this.signaturesService.currentAction, objToSend)
tap(() => {
resolve(true);
}),
catchError((err: any) => {
if (err.error.newSignatureLength !== undefined) {
this.signatureLength = err.error.newSignatureLength;
resolve(false);
} else {
this.notificationService.handleErrors(err);

Alex ORLUC
committed
resolve('error');
return of(false);
})
).subscribe();
});
}
cancelSign() {
this.modalController.dismiss(false);
}
const buf = new Uint8Array(buffer),
splitter = '',
res = [],
len = buf.length;
for (let i = 0; i < len; i++) {
const char = buf[i].toString(16);
}
return res.join(splitter);
}
fromHex(hexString: any) {
const res = new Uint8Array(hexString.length / 2);
for (let i = 0; i < hexString.length; i = i + 2) {
const c = hexString.slice(i, i + 2);
res[i / 2] = parseInt(c, 16);
}
return res.buffer;
}