feat: Add EntityEncodingFormat enum

This commit is contained in:
Yarmo Mackenbach 2023-03-12 21:43:43 +01:00
parent d943021579
commit 650c389ae2
2 changed files with 54 additions and 16 deletions

View file

@ -56,6 +56,21 @@ const Fetcher = {
} }
Object.freeze(Fetcher) Object.freeze(Fetcher)
/**
* Entity encoding format
* @readonly
* @enum {string}
*/
const EntityEncodingFormat = {
/** No special formatting */
PLAIN: 'plain',
/** HTML encoded entities */
HTML: 'html',
/** XML encoded entities */
XML: 'xml'
}
Object.freeze(EntityEncodingFormat)
/** /**
* Levels of access restriction for proof fetching * Levels of access restriction for proof fetching
* @readonly * @readonly
@ -131,6 +146,7 @@ Object.freeze(ClaimStatus)
exports.ProxyPolicy = ProxyPolicy exports.ProxyPolicy = ProxyPolicy
exports.Fetcher = Fetcher exports.Fetcher = Fetcher
exports.EntityEncodingFormat = EntityEncodingFormat
exports.ProofAccess = ProofAccess exports.ProofAccess = ProofAccess
exports.ProofFormat = ProofFormat exports.ProofFormat = ProofFormat
exports.ClaimFormat = ClaimFormat exports.ClaimFormat = ClaimFormat

View file

@ -16,17 +16,34 @@ limitations under the License.
const utils = require('./utils') const utils = require('./utils')
const E = require('./enums') const E = require('./enums')
const { bcryptVerify, argon2Verify } = require('hash-wasm') const { bcryptVerify, argon2Verify } = require('hash-wasm')
const entities = require('entities')
/** /**
* @module verifications * @module verifications
* @ignore * @ignore
*/ */
const containsProof = async (data, fingerprint, claimFormat) => { const containsProof = async (data, params) => {
const fingerprintFormatted = utils.generateClaim(fingerprint, claimFormat) const fingerprintFormatted = utils.generateClaim(params.target, params.claimFormat)
const fingerprintURI = utils.generateClaim(fingerprint, E.ClaimFormat.URI) const fingerprintURI = utils.generateClaim(params.target, E.ClaimFormat.URI)
let result = false let result = false
// Decode eventual special entities
switch (params.proofEncodingFormat) {
case E.EntityEncodingFormat.HTML:
data = entities.decodeHTML(data)
break
case E.EntityEncodingFormat.XML:
data = entities.decodeXML(data)
break
case E.EntityEncodingFormat.PLAIN:
default:
break
}
data = entities.decodeHTML(data)
// Check for plaintext proof // Check for plaintext proof
result = data.replace(/\r?\n|\r/g, '') result = data.replace(/\r?\n|\r/g, '')
.toLowerCase() .toLowerCase()
@ -138,7 +155,7 @@ const containsProof = async (data, fingerprint, claimFormat) => {
return result return result
} }
const runJSON = async (proofData, checkPath, checkClaim, checkClaimFormat, checkRelation) => { const runJSON = async (proofData, checkPath, params) => {
if (!proofData) { if (!proofData) {
return false return false
} }
@ -153,21 +170,21 @@ const runJSON = async (proofData, checkPath, checkClaim, checkClaimFormat, check
continue continue
} }
result = await runJSON(item, checkPath, checkClaim, checkClaimFormat, checkRelation) result = await runJSON(item, checkPath, params)
} }
return result return result
} }
if (checkPath.length === 0) { if (checkPath.length === 0) {
switch (checkRelation) { switch (params.claimRelation) {
case E.ClaimRelation.ONEOF: case E.ClaimRelation.ONEOF:
return await containsProof(proofData.join('|'), checkClaim, checkClaimFormat) return await containsProof(proofData.join('|'), params)
case E.ClaimRelation.CONTAINS: case E.ClaimRelation.CONTAINS:
case E.ClaimRelation.EQUALS: case E.ClaimRelation.EQUALS:
default: default:
return await containsProof(proofData, checkClaim, checkClaimFormat) return await containsProof(proofData, params)
} }
} }
@ -178,9 +195,7 @@ const runJSON = async (proofData, checkPath, checkClaim, checkClaimFormat, check
return await runJSON( return await runJSON(
proofData[checkPath[0]], proofData[checkPath[0]],
checkPath.slice(1), checkPath.slice(1),
checkClaim, params
checkClaimFormat,
checkRelation
) )
} }
@ -207,9 +222,12 @@ const run = async (proofData, claimData, fingerprint) => {
res.result = res.result || await runJSON( res.result = res.result || await runJSON(
proofData, proofData,
claimMethod.path, claimMethod.path,
fingerprint, {
claimMethod.format, target: fingerprint,
claimMethod.relation claimFormat: claimMethod.format,
proofEncodingFormat: claimMethod.encoding,
claimRelation: claimMethod.relation
}
) )
} catch (error) { } catch (error) {
res.errors.push(error.message ? error.message : error) res.errors.push(error.message ? error.message : error)
@ -223,8 +241,12 @@ const run = async (proofData, claimData, fingerprint) => {
try { try {
res.result = res.result || await containsProof( res.result = res.result || await containsProof(
proofData, proofData,
fingerprint, {
claimMethod.format target: fingerprint,
claimFormat: claimMethod.format,
proofEncodingFormat: claimMethod.encoding,
claimRelation: claimMethod.relation
}
) )
} catch (error) { } catch (error) {
res.errors.push('err_unknown_text_verification') res.errors.push('err_unknown_text_verification')