Skip to content
Snippets Groups Projects
Commit ddfd7daf authored by Zdravko Iliev's avatar Zdravko Iliev
Browse files

add types for signatures and test for non pdf file

parent 5ae2a8e2
No related branches found
No related tags found
1 merge request!1Draft: Resolve "[Document Sealing] Implement PDF parser"
Pipeline #49376 failed
......@@ -11,8 +11,8 @@ describe("PDF parser", () => {
);
const parser = new PDFparser(file);
const actual = await parser.getPDFMeta();
expect(actual.pages).toEqual(2);
});
......@@ -35,4 +35,16 @@ describe("PDF parser", () => {
expect(error).toBeInstanceOf(AppError);
}
});
it("should throw error if file type is different then pdf", async () => {
const file = fs.readFileSync(path.resolve(__dirname, "./test.txt"));
try {
const parser = new PDFparser(file);
const actual = await parser.getPDFMeta();
} catch (error) {
expect(error).toBeInstanceOf(AppError);
expect(error.message).toEqual("Only pdf file type is supported");
}
});
});
test txt file for tests
......@@ -6,3 +6,4 @@ export declare const getByteRange: (pdfBuffer: Buffer) => {
};
export declare const preparePDF: (pdf: any) => Buffer;
export declare const getMetaRegexMatch: (keyName: string) => (str: any) => any;
export declare const isPDF: (buf: Buffer) => boolean;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMetaRegexMatch = exports.preparePDF = exports.getByteRange = exports.checkForSubFilter = void 0;
exports.isPDF = exports.getMetaRegexMatch = exports.preparePDF = exports.getByteRange = exports.checkForSubFilter = void 0;
const errors_1 = require("./errors");
const DEFAULT_BYTE_RANGE_PLACEHOLDER = "**********";
const checkForSubFilter = (pdfBuffer) => {
......@@ -50,3 +50,9 @@ const getMetaRegexMatch = (keyName) => (str) => {
return meta;
};
exports.getMetaRegexMatch = getMetaRegexMatch;
const isPDF = (buf) => {
return (Buffer.isBuffer(buf) &&
buf.lastIndexOf("%PDF-") === 0 &&
buf.lastIndexOf("%%EOF") > -1);
};
exports.isPDF = isPDF;
/// <reference types="node" />
import { IgetMetaResponse } from "./types";
import { IGetMetaResponse } from "./types";
declare class PDFparser {
readonly document: any;
readonly config: any;
constructor(document: Buffer);
getPDFMeta: () => Promise<IgetMetaResponse>;
getPDFMeta: () => Promise<IGetMetaResponse>;
insertQrCode: (imgBytes: ArrayBuffer, url: string, scaleFactor: number) => Promise<ArrayBuffer>;
private createPageLinkAnnotation;
}
......
......@@ -15,12 +15,16 @@ const config_1 = require("./config");
const lib_1 = require("./lib");
const timeUtils_1 = require("./lib/timeUtils");
const errors_1 = require("./lib/errors");
const generalUtils_1 = require("./lib/generalUtils");
class PDFparser {
constructor(document) {
this.getPDFMeta = () => __awaiter(this, void 0, void 0, function* () {
if (!(this.document instanceof Buffer)) {
throw new errors_1.AppError("Document is not Buffer");
}
if (!(0, generalUtils_1.isPDF)(this.document)) {
throw new errors_1.AppError("Only pdf file type is supported");
}
try {
const signaturesMeta = yield (0, lib_1.verifyPDF)(this.document);
const pdfMeta = yield pdfdataextract_1.PdfData.extract(this.document, config_1.config);
......
export interface Icert {
export interface ICert {
clientCertificate: boolean;
issuedBy: {
countryName: string;
......@@ -17,9 +17,19 @@ export interface Icert {
};
pemCertificate: string;
}
export interface IgetMetaResponse {
export interface ISignature {
isExpired: boolean;
meta: {
certs: Array<ICert>;
reason: string;
contactInfo: string;
location: string;
signDate: string;
};
}
export interface IGetMetaResponse {
expired?: boolean;
signatures?: Array<any>;
signatures?: Array<ISignature>;
pages: number;
title: string;
author: string;
......
......@@ -57,3 +57,11 @@ export const getMetaRegexMatch = (keyName: string) => (str) => {
return meta;
};
export const isPDF = (buf: Buffer): boolean => {
return (
Buffer.isBuffer(buf) &&
buf.lastIndexOf("%PDF-") === 0 &&
buf.lastIndexOf("%%EOF") > -1
);
};
import { PDFName, PDFPage, PDFString, PDFDocument } from "pdf-lib";
import { PdfData } from "pdfdataextract";
import { config } from "./config";
import { IgetMetaResponse } from "./types";
import { IGetMetaResponse } from "./types";
import { verifyPDF } from "./lib";
import { formatPdfTime } from "./lib/timeUtils";
import { AppError, GeneralError } from "./lib/errors";
import { isPDF } from "./lib/generalUtils";
class PDFparser {
readonly document;
readonly config;
......@@ -14,11 +15,15 @@ class PDFparser {
this.config = config;
}
getPDFMeta = async (): Promise<IgetMetaResponse> => {
getPDFMeta = async (): Promise<IGetMetaResponse> => {
if (!(this.document instanceof Buffer)) {
throw new AppError("Document is not Buffer");
}
if (!isPDF(this.document)) {
throw new AppError("Only pdf file type is supported");
}
try {
const signaturesMeta = await verifyPDF(this.document);
const pdfMeta = await PdfData.extract(this.document, config);
......
export interface Icert {
export interface ICert {
clientCertificate: boolean;
issuedBy: {
countryName: string;
......@@ -18,11 +18,22 @@ export interface Icert {
pemCertificate: string;
}
export interface IgetMetaResponse {
export interface ISignature {
isExpired: boolean;
meta: {
certs: Array<ICert>;
reason: string;
contactInfo: string;
location: string;
signDate: string;
};
}
export interface IGetMetaResponse {
// verified: boolean;
// authenticity: boolean;
expired?: boolean;
signatures?: Array<any>;
signatures?: Array<ISignature>;
pages: number;
title: string;
author: string;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment