@truschery/eimzo
Version:
A lightweight Node.js package for seamless integration with Eimzo, providing easy-to-use functionality for creating digital signatures and managing API keys.
11 lines (10 loc) • 4.74 kB
JavaScript
import t from"@truschery/eimzo-api";var e;!function(t){t[t.UNDEFINED_ERROR=0]="UNDEFINED_ERROR",t[t.UNDEFINED_VERSION=1]="UNDEFINED_VERSION",t[t.NOT_ACTUAL_VERSION=2]="NOT_ACTUAL_VERSION",
//PKCS7
t[t.PASSWORD_ENTRY_CANCELED=3]="PASSWORD_ENTRY_CANCELED",t[t.SIGN_STRING_IS_EMPTY=4]="SIGN_STRING_IS_EMPTY",t[t.TIMESTAMP_FAIL=5]="TIMESTAMP_FAIL"}(e||(e={}));class s{constructor(t){this.setEimzoApiKeys(t),this.getTimestamp=t.getTimestamp,Object.freeze(this.keys),Object.freeze(this.getTimestamp),Object.freeze(this)}getKeys(){const t=[];for(let e in this.keys)t.push(e),t.push(this.keys[e]);return t}setEimzoApiKeys(t){this.keys=t.keys??{},t.debug&&(this.keys.localhost="localhost")}}class i{constructor(t,e){this.config=t,this.client=e}}class r{constructor(t){this.alias=new Map,// Замена на ИНН
t=(t=(t=t.toUpperCase()).replace("1.2.860.3.16.1.1=","INN=")).replace("1.2.860.3.16.1.2=","PINFL="),this.providedAlias=t.toUpperCase(),this.parse(this.providedAlias)}parse(t){t.split(",").forEach(t=>{const[e,s]=t.split("=");this.alias.set(e.toLowerCase(),s)})}get(t,e){return this.alias.has(t)?this.alias.get(t):e}}function a(t){return null==t||""===t||Array.isArray(t)&&0===t.length}class n{constructor(t,e,s){this.signAction=e,this.loadKeyAction=s;const i=new r(t.alias);this.alias=t.alias,this.file={disk:t.disk,name:t.name,path:t.path};const a=i.get("validfrom")??new Date,n=i.get("validto")??new Date;this.fullName=i.get("cn"),this.serialNumber=i.get("serialnumber"),this.name=i.get("name"),this.surname=i.get("surname"),this.inn=i.get("inn"),this.uid=i.get("uid"),this.pinfl=i.get("pinfl"),this.organization=i.get("o"),this.type=i.get("t"),this.validFrom=new Date(a),this.validTo=new Date(n),this.businesscategory=i.get("businesscategory"),this.address=i.get("l"),this.city=i.get("st")}async sign(t,e){return this.signAction(this,t,e)}loadKey(){return this.loadKeyAction(this.file.disk,this.file.path,this.file.name,this.alias)}isExpired(){return!this.isValid()}isValid(){if(a(t=this.validTo)||!(t instanceof Date))return!1;var t;const e=Date.now();return this.validTo.getTime()>=e}isPhysical(){return this.inn===this.uid}}class o extends i{constructor(t,e,s){super(t,e),this.pkcs7=s,this.certificates=[]}async list(t){const e=await this.client.pfx.listAllCertificates();return this.certificates=this.parseCertificate(e?.certificates),s=this.certificates,(i=t)?s.filter(t=>{if(void 0!==i.isValid&&t.isValid()!==i.isValid)return!1;if(i.inn){const e=Array.isArray(i.inn)?i.inn:[i.inn];if(!t.inn||!e.includes(t.inn))return!1}if(i.pinfl){const e=Array.isArray(i.pinfl)?i.pinfl:[i.pinfl];if(!t.pinfl||!e.includes(t.pinfl))return!1}
// TODO[2703]: add legal filter
if(i.search){const e=i.search.toLowerCase(),s=t.name?.toLowerCase().includes(e),r=t.fullName?.toLowerCase().includes(e),a=t.surname?.toLowerCase().includes(e),n=t.organization?.toLowerCase().includes(e);if(!(s||a||n||r))return!1}return!0}):s;var s,i}parseCertificate(t){if(a(t))throw new Error("[Eimzo Certificates] Certificates Is Empty");return t.map(t=>new n(t,(t,e,s)=>this.pkcs7.sign(t,e,s),(t,e,s,i)=>this.client.pfx.loadKey(t,e,s,i)))}}class c extends Error{constructor(t,e){super(t),this.name="EimzoError",Error.call(this),
//@ts-ignore
Error.captureStackTrace?Error.captureStackTrace(this,this.constructor):this.stack=(new Error).stack,this.message=t,this.code=e}data(){return{message:this.message,code:this.code}}}class h extends i{async sign(t,s,i){if(a(s))throw new c("Invalid String Value Is Empty Or Not String",e.SIGN_STRING_IS_EMPTY);const r=/^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/.test(s)?s:btoa(s),n=await t.loadKey(),o=i?.detached?"yes":"no",h=await this.createPkcs7(r,n.keyId,o);if(this.config.getTimestamp)try{return await this.config.getTimestamp(h.signature_hex)}catch(t){throw new c("An error occurred while generating the timestamp.",e.TIMESTAMP_FAIL)}return h.pkcs7_64}createPkcs7(t,s,i="no"){try{return this.client.pkcs7.createPkcs7(t,s,i)}catch(t){
//@ts-ignore
if(-5===t.status)throw new c("Password Entry Canceled",e.PASSWORD_ENTRY_CANCELED);throw new c("Undefined Error creating Pkcs7",e.UNDEFINED_ERROR)}}}class l{constructor(e){this.config=new s(e),this.client=new t,this.pkcs7=new h(this.config,this.client),this.pfx=new o(this.config,this.client,this.pkcs7),this.config.getKeys().length>0&&this.installApiKeys()}async getVersion(){const t=await this.client.send({name:"version"});if(!t.major||!t.minor)throw new c("undefined E-IMZO version",e.UNDEFINED_VERSION);return this.version={major:parseFloat(t.major),minor:parseFloat(t.minor),full:parseFloat(t.major)+"."+parseFloat(t.minor)},this.version}installApiKeys(){(new t).onlySend({name:"apikey",arguments:this.config.getKeys()})}}export{l as default};