@verii/batch-issuer 0.0.0-stage → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +1 -0
- package/README.md +296 -2
- package/package.json +50 -3
- package/src/cli.js +190 -0
- package/src/clients/hub-client.js +181 -0
- package/src/clients/init-client.js +40 -0
- package/src/clients/registrar-client.js +37 -0
- package/src/commands/check-rules.js +187 -0
- package/src/commands/exit-codes.js +30 -0
- package/src/commands/load.js +175 -0
- package/src/csv/load-csv.js +113 -0
- package/src/index.js +28 -0
- package/src/load/load-batch.js +313 -0
- package/src/report/count.js +24 -0
- package/src/report/describe-error.js +37 -0
- package/src/report/format-load-report.js +134 -0
- package/src/report/format-report.js +100 -0
- package/src/spec/load-spec.js +193 -0
- package/src/spec/spec-schema.js +109 -0
- package/src/state/state-file.js +212 -0
- package/src/templates/render-template.js +159 -0
- package/src/validation/check-credential-subjects.js +129 -0
- package/src/validation/check-rows.js +193 -0
- package/src/validation/check-service.js +127 -0
- package/src/validation/find-duplicates.js +147 -0
- package/src/validation/match-existing-depots.js +142 -0
- package/src/validation/reasons.js +21 -0
- package/src/validation/validate-batch.js +105 -0
- package/src/values/auth-values.js +130 -0
- package/src/values/convert-value.js +65 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Velocity Team
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const {
|
|
18
|
+
flow,
|
|
19
|
+
includes,
|
|
20
|
+
isEmpty,
|
|
21
|
+
isEqual,
|
|
22
|
+
map,
|
|
23
|
+
range,
|
|
24
|
+
reject,
|
|
25
|
+
size,
|
|
26
|
+
sortBy,
|
|
27
|
+
zipWith,
|
|
28
|
+
} = require('lodash/fp');
|
|
29
|
+
|
|
30
|
+
const isInternalVpAuth = (service) =>
|
|
31
|
+
service.authMode === 'internal' &&
|
|
32
|
+
includes('verifiable_presentation', service.authMethods);
|
|
33
|
+
|
|
34
|
+
const isPreauth = (service) => includes('preauth', service.authMethods);
|
|
35
|
+
|
|
36
|
+
const isDeactivated = (service) =>
|
|
37
|
+
service.deactivationDate != null &&
|
|
38
|
+
new Date(service.deactivationDate) <= new Date();
|
|
39
|
+
|
|
40
|
+
// The rules in valueIndex order. Only an internal verifiable_presentation
|
|
41
|
+
// service matches depots by their auth values.
|
|
42
|
+
const authRulesOf = (service) =>
|
|
43
|
+
isInternalVpAuth(service)
|
|
44
|
+
? sortBy('valueIndex', service.verifiablePresentationAuthRules)
|
|
45
|
+
: [];
|
|
46
|
+
|
|
47
|
+
const checkAuthMethods = (service) => {
|
|
48
|
+
if (!isInternalVpAuth(service) && !isPreauth(service)) {
|
|
49
|
+
return [
|
|
50
|
+
{
|
|
51
|
+
reason:
|
|
52
|
+
'the service must use internal verifiable_presentation auth or preauth',
|
|
53
|
+
},
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
return isInternalVpAuth(service) && isEmpty(authRulesOf(service))
|
|
57
|
+
? [{ reason: 'the service has no verifiablePresentationAuthRules' }]
|
|
58
|
+
: [];
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const checkActive = (service) =>
|
|
62
|
+
isDeactivated(service)
|
|
63
|
+
? [{ reason: `the service was deactivated at ${service.deactivationDate}` }]
|
|
64
|
+
: [];
|
|
65
|
+
|
|
66
|
+
const toAuthColumn = ({ column }, rule) => ({ column, rule });
|
|
67
|
+
|
|
68
|
+
// depot.authValues gives the column of each rule, in valueIndex order, so it
|
|
69
|
+
// needs exactly one entry per rule, and the rules' value indexes must be 0 to
|
|
70
|
+
// n-1.
|
|
71
|
+
const mapAuthColumns = (spec, service) => {
|
|
72
|
+
const rules = authRulesOf(service);
|
|
73
|
+
const specAuthValues = spec.depot.authValues ?? [];
|
|
74
|
+
if (size(specAuthValues) !== size(rules)) {
|
|
75
|
+
const reason = `depot.authValues has ${size(specAuthValues)} entries, but the service has ${size(rules)} auth rules`;
|
|
76
|
+
return { errors: [{ reason }] };
|
|
77
|
+
}
|
|
78
|
+
if (!isEqual(range(0, size(rules)), map('valueIndex', rules))) {
|
|
79
|
+
const reason = `the service's auth rules must have the valueIndexes 0 to ${size(rules) - 1}`;
|
|
80
|
+
return { errors: [{ reason }] };
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
authColumns: zipWith(toAuthColumn, specAuthValues, rules),
|
|
84
|
+
errors: [],
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const checkTemplateTypes = (spec, templates) => {
|
|
89
|
+
const expectedTypes = spec.depot.expectedCredentialTypes;
|
|
90
|
+
if (isEmpty(expectedTypes)) {
|
|
91
|
+
return [];
|
|
92
|
+
}
|
|
93
|
+
const toError = ({ template, type }) => ({
|
|
94
|
+
reason: `template ${template} has the type ${type}, which is not in depot.expectedCredentialTypes`,
|
|
95
|
+
});
|
|
96
|
+
return flow(
|
|
97
|
+
reject(({ type }) => includes(type, expectedTypes)),
|
|
98
|
+
map(toError),
|
|
99
|
+
)(templates);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Step 1: checks the spec against the issuer service.
|
|
104
|
+
* @param {{ spec: object, templates: Array<object>, service?: object }} context
|
|
105
|
+
* @returns {{ authColumns?: Array<{ column: string, rule: object }>, errors: Array<{ reason: string }> }}
|
|
106
|
+
* the column of each auth rule in valueIndex order, missing when the spec and
|
|
107
|
+
* the service don't fit, and the errors
|
|
108
|
+
*/
|
|
109
|
+
const checkService = ({ spec, templates, service }) => {
|
|
110
|
+
const typeErrors = checkTemplateTypes(spec, templates);
|
|
111
|
+
if (service == null) {
|
|
112
|
+
const reason = `tenant ${spec.tenantId} has no issuer service ${spec.serviceId}`;
|
|
113
|
+
return { errors: [{ reason }, ...typeErrors] };
|
|
114
|
+
}
|
|
115
|
+
const authMethodErrors = checkAuthMethods(service);
|
|
116
|
+
const activeErrors = checkActive(service);
|
|
117
|
+
if (!isEmpty(authMethodErrors)) {
|
|
118
|
+
return { errors: [...authMethodErrors, ...activeErrors, ...typeErrors] };
|
|
119
|
+
}
|
|
120
|
+
const { authColumns, errors: authErrors } = mapAuthColumns(spec, service);
|
|
121
|
+
return {
|
|
122
|
+
authColumns,
|
|
123
|
+
errors: [...activeErrors, ...authErrors, ...typeErrors],
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
module.exports = { checkService, isInternalVpAuth, isPreauth };
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Velocity Team
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const {
|
|
18
|
+
filter,
|
|
19
|
+
first,
|
|
20
|
+
flatMap,
|
|
21
|
+
flow,
|
|
22
|
+
groupBy,
|
|
23
|
+
isEmpty,
|
|
24
|
+
isString,
|
|
25
|
+
join,
|
|
26
|
+
map,
|
|
27
|
+
size,
|
|
28
|
+
take,
|
|
29
|
+
uniq,
|
|
30
|
+
values,
|
|
31
|
+
without,
|
|
32
|
+
} = require('lodash/fp');
|
|
33
|
+
const {
|
|
34
|
+
buildAuthMatchValues,
|
|
35
|
+
} = require('@verii/credentialing-hub-operator-api');
|
|
36
|
+
const { count, formatNumber } = require('../report/count');
|
|
37
|
+
|
|
38
|
+
// A reason lists at most this many rows, so every row of a large group of
|
|
39
|
+
// duplicates gets a short reason, rather than the numbers of all the others.
|
|
40
|
+
const MAX_LISTED_ROWS = 10;
|
|
41
|
+
|
|
42
|
+
// Auth values are compared as the Hub matches them: normalized with
|
|
43
|
+
// buildAuthMatchValues, and compared by type, so "12345" and 12345 differ.
|
|
44
|
+
const authValuesKey = flow(buildAuthMatchValues, JSON.stringify);
|
|
45
|
+
|
|
46
|
+
const duplicateGroups = (keyOf) =>
|
|
47
|
+
flow(
|
|
48
|
+
groupBy(keyOf),
|
|
49
|
+
values,
|
|
50
|
+
filter((group) => size(group) > 1),
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
// "3, 5", or "3, 4, …, 12 and 49,989 more" when there are more rows than
|
|
54
|
+
// listed.
|
|
55
|
+
const listRows = (listedRows, rowCount) => {
|
|
56
|
+
const listed = join(', ', listedRows);
|
|
57
|
+
const unlistedCount = rowCount - size(listedRows);
|
|
58
|
+
return unlistedCount > 0
|
|
59
|
+
? `${listed} and ${formatNumber(unlistedCount)} more`
|
|
60
|
+
: listed;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// Describes the other rows of a row's group, such as "2 other rows: 3, 5".
|
|
64
|
+
// Only the group's first MAX_LISTED_ROWS + 1 rows are looked at, so each
|
|
65
|
+
// row's description takes the same time however large the group is.
|
|
66
|
+
const describeOtherRows = (rowNumbers) => {
|
|
67
|
+
const candidates = take(MAX_LISTED_ROWS + 1, rowNumbers);
|
|
68
|
+
const otherCount = size(rowNumbers) - 1;
|
|
69
|
+
return (row) => {
|
|
70
|
+
const listedRows = flow(without([row]), take(MAX_LISTED_ROWS))(candidates);
|
|
71
|
+
const otherRows = listRows(listedRows, otherCount);
|
|
72
|
+
return `${count(otherCount, 'other row')}: ${otherRows}`;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const hasAuthValues = ({ authValues }) => !isEmpty(authValues);
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Finds rows whose auth values are the same once normalized. Each row of a
|
|
80
|
+
* group gets an error that counts the other rows and lists at most 10 of them.
|
|
81
|
+
* @param {Array<{ row: number, authValues?: Array }>} rows the checked rows
|
|
82
|
+
* @param {Array<{ column: string }>} authColumns the auth value columns
|
|
83
|
+
* @returns {Array<{ row: number, column: string, reason: string }>} an error for each row of each group of duplicates
|
|
84
|
+
*/
|
|
85
|
+
const findDuplicateAuthValues = (rows, authColumns) => {
|
|
86
|
+
const column = join(', ', map('column', authColumns));
|
|
87
|
+
const toErrors = (group) => {
|
|
88
|
+
const describeOthers = describeOtherRows(map('row', group));
|
|
89
|
+
const toError = ({ row }) => ({
|
|
90
|
+
row,
|
|
91
|
+
column,
|
|
92
|
+
reason: `has the same auth values as ${describeOthers(row)}`,
|
|
93
|
+
});
|
|
94
|
+
return map(toError, group);
|
|
95
|
+
};
|
|
96
|
+
return flow(
|
|
97
|
+
filter(hasAuthValues),
|
|
98
|
+
duplicateGroups(({ authValues }) => authValuesKey(authValues)),
|
|
99
|
+
flatMap(toErrors),
|
|
100
|
+
)(rows);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const credentialReferencesOf = ({ row, credentials }) =>
|
|
104
|
+
flow(
|
|
105
|
+
filter(({ credential }) => isString(credential.credentialReference)),
|
|
106
|
+
map(({ template, credential }) => ({
|
|
107
|
+
row,
|
|
108
|
+
template,
|
|
109
|
+
credentialReference: credential.credentialReference,
|
|
110
|
+
})),
|
|
111
|
+
)(credentials);
|
|
112
|
+
|
|
113
|
+
// One reason for the whole group, such as 'credentialReference "E1" is used
|
|
114
|
+
// 3 times in the batch, in 2 rows: 2, 3'.
|
|
115
|
+
const describeCredentialReferenceGroup = (group) => {
|
|
116
|
+
const { credentialReference } = first(group);
|
|
117
|
+
const rowNumbers = flow(map('row'), uniq)(group);
|
|
118
|
+
const rowCount = size(rowNumbers);
|
|
119
|
+
const listedRows = listRows(take(MAX_LISTED_ROWS, rowNumbers), rowCount);
|
|
120
|
+
const uses = count(size(group), 'time');
|
|
121
|
+
return `credentialReference "${credentialReference}" is used ${uses} in the batch, in ${count(rowCount, 'row')}: ${listedRows}`;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Finds credentials that share a credentialReference anywhere in the batch.
|
|
126
|
+
* Each credential of a group gets an error that counts the group's uses and
|
|
127
|
+
* rows, and lists at most 10 of the rows.
|
|
128
|
+
* @param {Array<{ row: number, credentials: Array<object> }>} rows the checked rows
|
|
129
|
+
* @returns {Array<{ row: number, template: string, reason: string }>} an error for each credential of each group of duplicates
|
|
130
|
+
*/
|
|
131
|
+
const findDuplicateCredentialReferences = (rows) => {
|
|
132
|
+
const toErrors = (group) => {
|
|
133
|
+
const reason = describeCredentialReferenceGroup(group);
|
|
134
|
+
return map(({ row, template }) => ({ row, template, reason }), group);
|
|
135
|
+
};
|
|
136
|
+
return flow(
|
|
137
|
+
flatMap(credentialReferencesOf),
|
|
138
|
+
duplicateGroups('credentialReference'),
|
|
139
|
+
flatMap(toErrors),
|
|
140
|
+
)(rows);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
module.exports = {
|
|
144
|
+
authValuesKey,
|
|
145
|
+
findDuplicateAuthValues,
|
|
146
|
+
findDuplicateCredentialReferences,
|
|
147
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Velocity Team
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const {
|
|
18
|
+
difference,
|
|
19
|
+
filter,
|
|
20
|
+
flatMap,
|
|
21
|
+
flow,
|
|
22
|
+
fromPairs,
|
|
23
|
+
get,
|
|
24
|
+
getOr,
|
|
25
|
+
groupBy,
|
|
26
|
+
has,
|
|
27
|
+
isEmpty,
|
|
28
|
+
join,
|
|
29
|
+
map,
|
|
30
|
+
size,
|
|
31
|
+
uniq,
|
|
32
|
+
uniqBy,
|
|
33
|
+
} = require('lodash/fp');
|
|
34
|
+
const { authValuesKey } = require('./find-duplicates');
|
|
35
|
+
|
|
36
|
+
// Rows whose depot didn't render have no userReference to compare.
|
|
37
|
+
const isLookedUp = ({ authValues, depot }) =>
|
|
38
|
+
!isEmpty(authValues) && depot != null;
|
|
39
|
+
|
|
40
|
+
const depotAuthValuesKey = flow(get('authValues'), authValuesKey);
|
|
41
|
+
|
|
42
|
+
// The credential types a returning holder's depot will receive: the row's
|
|
43
|
+
// credentials' types or, in a depot-only load, the spec's
|
|
44
|
+
// expectedCredentialTypes, which a later load adds.
|
|
45
|
+
const incomingTypes = (row, { depotOnly, specExpectedTypes }) =>
|
|
46
|
+
depotOnly ? specExpectedTypes : flow(map('type'), uniq)(row.credentials);
|
|
47
|
+
|
|
48
|
+
// The depot must expect each of them, when it expects any.
|
|
49
|
+
const unexpectedTypeErrors = (row, depot, context) => {
|
|
50
|
+
const expectedTypes = depot.expectedCredentialTypes;
|
|
51
|
+
const types = difference(incomingTypes(row, context), expectedTypes);
|
|
52
|
+
return isEmpty(expectedTypes) || isEmpty(types)
|
|
53
|
+
? []
|
|
54
|
+
: [
|
|
55
|
+
`the auth values belong to depot ${depot.id}, which doesn't expect credentials of type ${join(', ', types)}`,
|
|
56
|
+
];
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// A depot-only load adds no credentials, so a returning holder's depot must
|
|
60
|
+
// wait for them, as the load's new depots do. A depot's deferredCredentials
|
|
61
|
+
// can't be changed, and a depot that doesn't defer would give the holder an
|
|
62
|
+
// empty offer.
|
|
63
|
+
const undeferredErrors = (depot, { depotOnly }) =>
|
|
64
|
+
depotOnly && depot.deferredCredentials !== true
|
|
65
|
+
? [
|
|
66
|
+
`the auth values belong to depot ${depot.id}, which doesn't defer credentials, so a depot-only load can't reuse it`,
|
|
67
|
+
]
|
|
68
|
+
: [];
|
|
69
|
+
|
|
70
|
+
const checkReturningHolder = (row, depot, context) => {
|
|
71
|
+
const errors = [
|
|
72
|
+
...unexpectedTypeErrors(row, depot, context),
|
|
73
|
+
...undeferredErrors(depot, context),
|
|
74
|
+
];
|
|
75
|
+
return isEmpty(errors) ? { existingDepot: depot, errors } : { errors };
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const checkMatch = (row, depots, context) => {
|
|
79
|
+
if (isEmpty(depots)) {
|
|
80
|
+
return { errors: [] };
|
|
81
|
+
}
|
|
82
|
+
if (size(depots) > 1) {
|
|
83
|
+
const ids = join(', ', map('id', depots));
|
|
84
|
+
return {
|
|
85
|
+
errors: [`the auth values belong to ${size(depots)} depots: ${ids}`],
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
const [depot] = depots;
|
|
89
|
+
if (depot.userReference !== row.depot.userReference) {
|
|
90
|
+
return {
|
|
91
|
+
errors: [
|
|
92
|
+
`the auth values belong to depot ${depot.id}, whose userReference is "${depot.userReference}"`,
|
|
93
|
+
],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
// A depot that an earlier run of this batch created isn't a reuse.
|
|
97
|
+
if (!context.reuse && !context.createdDepotIds.has(depot.id)) {
|
|
98
|
+
return {
|
|
99
|
+
errors: [
|
|
100
|
+
`the auth values belong to depot ${depot.id}, and --no-reuse is set`,
|
|
101
|
+
],
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
return checkReturningHolder(row, depot, context);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// Step 4: the rows against the depots that already hold their auth values.
|
|
108
|
+
// createdDepotIds are the depots that the batch's state file records.
|
|
109
|
+
const matchExistingDepots = async (
|
|
110
|
+
rows,
|
|
111
|
+
{ spec, authColumns, hub, reuse, createdDepotIds },
|
|
112
|
+
) => {
|
|
113
|
+
const lookedUpRows = filter(isLookedUp, rows);
|
|
114
|
+
if (isEmpty(lookedUpRows)) {
|
|
115
|
+
return { existingDepots: {}, errors: [] };
|
|
116
|
+
}
|
|
117
|
+
const tuples = flow(map('authValues'), uniqBy(authValuesKey))(lookedUpRows);
|
|
118
|
+
const depots = await hub.findDepotsByAuthValues(spec, tuples);
|
|
119
|
+
const depotsByKey = groupBy(depotAuthValuesKey, depots);
|
|
120
|
+
const matchContext = {
|
|
121
|
+
reuse,
|
|
122
|
+
createdDepotIds,
|
|
123
|
+
depotOnly: !has('credentials', spec),
|
|
124
|
+
specExpectedTypes: getOr([], ['depot', 'expectedCredentialTypes'], spec),
|
|
125
|
+
};
|
|
126
|
+
const matchRow = (row) => {
|
|
127
|
+
const rowDepots = getOr([], [authValuesKey(row.authValues)], depotsByKey);
|
|
128
|
+
return { row: row.row, ...checkMatch(row, rowDepots, matchContext) };
|
|
129
|
+
};
|
|
130
|
+
const matches = map(matchRow, lookedUpRows);
|
|
131
|
+
const existingDepots = flow(
|
|
132
|
+
filter('existingDepot'),
|
|
133
|
+
map(({ row, existingDepot }) => [row, existingDepot]),
|
|
134
|
+
fromPairs,
|
|
135
|
+
)(matches);
|
|
136
|
+
const column = join(', ', map('column', authColumns));
|
|
137
|
+
const toErrors = ({ row, errors: reasons }) =>
|
|
138
|
+
map((reason) => ({ row, column, reason }), reasons);
|
|
139
|
+
return { existingDepots, errors: flatMap(toErrors, matches) };
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
module.exports = { matchExistingDepots };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Velocity Team
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// The reason given for a column that a row doesn't have. When the CSV's header
|
|
18
|
+
// doesn't have the column either, the rows' errors become one batch error.
|
|
19
|
+
const MISSING = 'is missing';
|
|
20
|
+
|
|
21
|
+
module.exports = { MISSING };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Velocity Team
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const {
|
|
18
|
+
filter,
|
|
19
|
+
flatMap,
|
|
20
|
+
flow,
|
|
21
|
+
get,
|
|
22
|
+
isEmpty,
|
|
23
|
+
map,
|
|
24
|
+
pick,
|
|
25
|
+
size,
|
|
26
|
+
} = require('lodash/fp');
|
|
27
|
+
const { loadSpec } = require('../spec/load-spec');
|
|
28
|
+
const { loadCsv } = require('../csv/load-csv');
|
|
29
|
+
const { checkService } = require('./check-service');
|
|
30
|
+
const { checkRows } = require('./check-rows');
|
|
31
|
+
const { checkCredentialSubjects } = require('./check-credential-subjects');
|
|
32
|
+
const { matchExistingDepots } = require('./match-existing-depots');
|
|
33
|
+
|
|
34
|
+
// What a load needs of each row: the depot to create, or the returning
|
|
35
|
+
// holder's depot to reuse, and the credentials.
|
|
36
|
+
const toBatchRow = (existingDepots) => (row) => ({
|
|
37
|
+
row: row.row,
|
|
38
|
+
depot: row.depot,
|
|
39
|
+
authValues: row.authValues,
|
|
40
|
+
existingDepot: get([row.row], existingDepots),
|
|
41
|
+
credentials: map(pick(['template', 'type', 'credential']), row.credentials),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Validates a batch as the README's steps describe, collecting every error.
|
|
45
|
+
const validateBatch = async ({ specFile, csvFile, vars, options }, clients) => {
|
|
46
|
+
const { spec, templates, errors: specErrors } = await loadSpec(specFile);
|
|
47
|
+
if (!isEmpty(specErrors)) {
|
|
48
|
+
return { errors: specErrors };
|
|
49
|
+
}
|
|
50
|
+
const csv = await loadCsv(csvFile);
|
|
51
|
+
if (!isEmpty(csv.errors)) {
|
|
52
|
+
return { errors: csv.errors };
|
|
53
|
+
}
|
|
54
|
+
const service = await clients.hub.getIssuerService(spec);
|
|
55
|
+
const { authColumns, errors: serviceErrors } = checkService({
|
|
56
|
+
spec,
|
|
57
|
+
templates,
|
|
58
|
+
service,
|
|
59
|
+
});
|
|
60
|
+
const { rows, errors: rowErrors } = checkRows({
|
|
61
|
+
spec,
|
|
62
|
+
templates,
|
|
63
|
+
authColumns,
|
|
64
|
+
csv,
|
|
65
|
+
vars,
|
|
66
|
+
options,
|
|
67
|
+
});
|
|
68
|
+
const subjectErrors = await checkCredentialSubjects(
|
|
69
|
+
rows,
|
|
70
|
+
templates,
|
|
71
|
+
clients.registrar,
|
|
72
|
+
);
|
|
73
|
+
const { existingDepots, errors: depotErrors } = await matchExistingDepots(
|
|
74
|
+
rows,
|
|
75
|
+
{
|
|
76
|
+
spec,
|
|
77
|
+
authColumns,
|
|
78
|
+
hub: clients.hub,
|
|
79
|
+
reuse: options.reuse,
|
|
80
|
+
createdDepotIds: options.createdDepotIds,
|
|
81
|
+
},
|
|
82
|
+
);
|
|
83
|
+
const batchRows = map(toBatchRow(existingDepots), rows);
|
|
84
|
+
return {
|
|
85
|
+
errors: [...serviceErrors, ...rowErrors, ...subjectErrors, ...depotErrors],
|
|
86
|
+
batch: { spec, service, templates, rows: batchRows },
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Counts what a load of a batch would create and reuse.
|
|
92
|
+
* @param {{ rows: Array<object> }} batch the validated batch
|
|
93
|
+
* @returns {{ rows: number, newDepots: number, existingDepots: number, credentials: number }}
|
|
94
|
+
*/
|
|
95
|
+
const summarizeBatch = ({ rows }) => {
|
|
96
|
+
const existingDepots = flow(filter('existingDepot'), size)(rows);
|
|
97
|
+
return {
|
|
98
|
+
rows: size(rows),
|
|
99
|
+
newDepots: size(rows) - existingDepots,
|
|
100
|
+
existingDepots,
|
|
101
|
+
credentials: flow(flatMap('credentials'), size)(rows),
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
module.exports = { summarizeBatch, validateBatch };
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Velocity Team
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const { parsePhoneNumberFromString } = require('libphonenumber-js');
|
|
18
|
+
const {
|
|
19
|
+
flow,
|
|
20
|
+
get,
|
|
21
|
+
has,
|
|
22
|
+
isEmpty,
|
|
23
|
+
isString,
|
|
24
|
+
map,
|
|
25
|
+
matches,
|
|
26
|
+
overSome,
|
|
27
|
+
pick,
|
|
28
|
+
reject,
|
|
29
|
+
startsWith,
|
|
30
|
+
trim,
|
|
31
|
+
} = require('lodash/fp');
|
|
32
|
+
const { isValidAuthValue } = require('@verii/credentialing-hub-operator-api');
|
|
33
|
+
const { convertValue } = require('./convert-value');
|
|
34
|
+
const { MISSING } = require('../validation/reasons');
|
|
35
|
+
|
|
36
|
+
const isPhoneRule = overSome([
|
|
37
|
+
matches({ type: 'PhoneV1.0' }),
|
|
38
|
+
matches({ valueFormat: 'e164' }),
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
const isEmailRule = overSome([
|
|
42
|
+
matches({ type: 'EmailV1.0' }),
|
|
43
|
+
matches({ valueFormat: 'email' }),
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
// Phone credentials hold E.164 numbers, such as +15556192191. A number written
|
|
47
|
+
// without a country code takes the default country. Only the number's length
|
|
48
|
+
// is checked, so test ranges such as 555 numbers are accepted.
|
|
49
|
+
const normalizePhone = (text, defaultCountry) => {
|
|
50
|
+
const phoneNumber = parsePhoneNumberFromString(text, { defaultCountry });
|
|
51
|
+
if (phoneNumber?.isPossible()) {
|
|
52
|
+
return { value: phoneNumber.number };
|
|
53
|
+
}
|
|
54
|
+
const hint =
|
|
55
|
+
defaultCountry == null && !startsWith('+', text)
|
|
56
|
+
? '. It has no country code, so pass --default-country'
|
|
57
|
+
: '';
|
|
58
|
+
return { reason: `"${text}" is not a phone number${hint}` };
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// An email rule's value gets the Hub's check of an email valueFormat.
|
|
62
|
+
const checkEmail = (rule, text) =>
|
|
63
|
+
isValidAuthValue({ ...rule, valueFormat: 'email' }, text)
|
|
64
|
+
? { value: text }
|
|
65
|
+
: { reason: `"${text}" is not an email address` };
|
|
66
|
+
|
|
67
|
+
const formatAuthValue = (rule, text, { defaultCountry }) => {
|
|
68
|
+
if (isPhoneRule(rule)) {
|
|
69
|
+
return normalizePhone(text, defaultCountry);
|
|
70
|
+
}
|
|
71
|
+
if (isEmailRule(rule)) {
|
|
72
|
+
return checkEmail(rule, text);
|
|
73
|
+
}
|
|
74
|
+
return { value: text };
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Converts a CSV cell to the value a depot's `authValues` holds for a rule.
|
|
79
|
+
* The cell is trimmed and converted to the rule's `valueType`. A phone rule's
|
|
80
|
+
* value is normalized to E.164, and an email rule's value must be an email
|
|
81
|
+
* address.
|
|
82
|
+
* @param {object} rule the auth rule
|
|
83
|
+
* @param {string} [text] the cell, missing when the row has no such cell
|
|
84
|
+
* @param {{ defaultCountry?: string }} options
|
|
85
|
+
* @returns {{ value: string|number|boolean } | { reason: string }}
|
|
86
|
+
*/
|
|
87
|
+
const toAuthValue = (rule, text, options) => {
|
|
88
|
+
if (text == null) {
|
|
89
|
+
return { reason: MISSING };
|
|
90
|
+
}
|
|
91
|
+
const trimmed = trim(text);
|
|
92
|
+
if (isEmpty(trimmed)) {
|
|
93
|
+
return { reason: 'is blank' };
|
|
94
|
+
}
|
|
95
|
+
const converted = convertValue(rule.valueType, trimmed);
|
|
96
|
+
if (has('reason', converted) || !isString(converted.value)) {
|
|
97
|
+
return converted;
|
|
98
|
+
}
|
|
99
|
+
return formatAuthValue(rule, converted.value, options);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// Only the row's own values count, so a column named like an inherited
|
|
103
|
+
// property, such as "constructor", is missing when the row doesn't have it.
|
|
104
|
+
const cellOf = (column, data) =>
|
|
105
|
+
has([column], data) ? get([column], data) : undefined;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Builds a row's auth values, one for each rule, in `valueIndex` order.
|
|
109
|
+
* @param {Array<{ column: string, rule: object }>} authColumns each auth rule and the column that holds its value
|
|
110
|
+
* @param {object} data the row's values by column
|
|
111
|
+
* @param {{ defaultCountry?: string }} options
|
|
112
|
+
* @returns {{ authValues?: Array<string|number|boolean>, errors: Array<{ column: string, reason: string }> }}
|
|
113
|
+
* the auth values, which are missing when any of them is invalid, and the errors
|
|
114
|
+
*/
|
|
115
|
+
const buildAuthValues = (authColumns, data, options) => {
|
|
116
|
+
const toColumnResult = ({ column, rule }) => ({
|
|
117
|
+
column,
|
|
118
|
+
...toAuthValue(rule, cellOf(column, data), options),
|
|
119
|
+
});
|
|
120
|
+
const results = map(toColumnResult, authColumns);
|
|
121
|
+
const errors = flow(
|
|
122
|
+
reject(has('value')),
|
|
123
|
+
map(pick(['column', 'reason'])),
|
|
124
|
+
)(results);
|
|
125
|
+
return isEmpty(errors)
|
|
126
|
+
? { authValues: map('value', results), errors }
|
|
127
|
+
: { errors };
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
module.exports = { buildAuthValues };
|