UNPKG

@identity.com/dsr

Version:

The Dynamic Scope Request (DSR) javascript library provides capability around securely requesting credential information between an ID Requester and an ID Holder

1 lines 3.39 kB
const sift=require("sift").default,{schemaLoader}=require("@identity.com/credential-commons"),{VALIDATION_MODE}=require("../ScopeRequest");function DsrResolver(){return this.convertMongoOperatorToJavascript=a=>"$gt"===a?">":"$gte"===a?">=":"$lt"===a?"<":"$lte"===a?"<=":null,this.filterCredentialsByIdentifier=(a,b,c,d,e)=>{const f=a.substring(11,a.lastIndexOf("-")),g=b.filter(sift({identifier:{$regex:`${f}`}}));if(c.identifier){const a=[];if(c.constraints.meta){if(c.constraints.meta.issued){const b=Object.keys(c.constraints.meta.issued.is)[0],d=this.convertMongoOperatorToJavascript(Object.keys(c.constraints.meta.issued.is)[0]),e=c.constraints.meta.issued.is[b],f={};f.$where=`new Date(this.issued).getTime() ${d} ${e}`,a.push(f)}if(c.constraints.meta.expiry){const b=Object.keys(c.constraints.meta.expiry.is)[0],d=this.convertMongoOperatorToJavascript(Object.keys(c.constraints.meta.expiry.is)[0]),e=c.constraints.meta.expiry.is[b],f={};f.$where=`new Date(this.expiry).getTime() ${d} ${e}`,a.push(f)}if(c.constraints.meta.issuer){const b=Object.keys(c.constraints.meta.issuer.is)[0],d=c.constraints.meta.issuer.is[b],e={};e.issuer=d,a.push(e)}}if(d.mode&&d.mode===VALIDATION_MODE.ADVANCED){c.constraints&&c.constraints.claims&&c.constraints.claims.forEach(b=>{const c=`claim.${b.path}`,d=Object.keys(b.is)[0],e=b.is[d],f={};f[c]=e,a.push(f)});e.credentials.push(...g.filter(sift({$and:a})))}else c.constraints&&c.constraints.claims?c.constraints.claims.forEach(b=>{const c=`claim.${b.path}`,d=Object.keys(b.is)[0],f=b.is[d],h={};h[c]=f,a.push(h);e.failedConstraints.push(...g.filter(sift({$nor:a})));const i={$and:a,$nin:e.failedConstraints};e.credentials.push(...g.filter(sift(i)))}):e.credentials.push(...g)}else e.credentials.push(...g)},this.filterCredentialsByClaim=(a,b,c,d,e)=>{const f=a.substring(6,a.lastIndexOf("-")),g=schemaLoader.ucaDefinitions.find(a=>a.identifier===f),h=[],i=[];if(g.alsoKnown)g.alsoKnown.forEach(a=>{const c=a.substring(a.indexOf(":")+1,a.lastIndexOf(":")).toLowerCase(),d=a.substring(a.lastIndexOf(":")+1),e={};e[`claim.${c}.${d}`]={$exists:!0},h.push(...b.filter(sift(e)))});else{const{identifier:a}=g,c=a.substring(a.indexOf(":")+1,a.lastIndexOf(":")).toLowerCase(),d=a.substring(a.lastIndexOf(":")+1),e={};e[`claim.${c}.${d}`]={$exists:!0},h.push(...b.filter(sift(e)))}if(c.identifier){const a=c.identifier.substring(c.identifier.indexOf(":")+1,c.identifier.lastIndexOf(":")).toLowerCase();if(!(c.constraints&&c.constraints.claims))e.credentials.push(...h);else if(d.mode&&d.mode===VALIDATION_MODE.ADVANCED){c.constraints.claims.forEach(b=>{const c=`claim.${a}.${b.path}`,d=Object.keys(b.is)[0],e=b.is[d],f={};f[c]=e,i.push(f)});e.credentials.push(...h.filter(sift({$and:i})))}else c.constraints.claims.forEach(b=>{const c=`claim.${a}.${b.path}`,d=Object.keys(b.is)[0],f=b.is[d],g={};g[c]=f,i.push(g);e.failedConstraints.push(...h.filter(sift({$nor:i})));const j={$and:i,$nin:e.failedConstraints};e.credentials.push(...h.filter(sift(j)))})}else e.credentials.push(...h)},this.filterCredentials=(a,b)=>{const c={credentials:[],failedConstraints:[]};for(const d of a.credentialItems)if(d.constraints||!d.aggregate){const e="string"==typeof d?d:d.identifier,f=e.substring(0,e.indexOf("-"));"credential"===f?this.filterCredentialsByIdentifier(e,b,d,a,c):"claim"===f&&this.filterCredentialsByClaim(e,b,d,a,c)}return c},this}module.exports=DsrResolver;