javascript-solid-server 0.0.124 → 0.0.125
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/package.json +1 -1
- package/src/wac/checker.js +16 -5
- package/test/wac.test.js +24 -0
package/package.json
CHANGED
package/src/wac/checker.js
CHANGED
|
@@ -163,15 +163,26 @@ async function checkAuthorizations(authorizations, targetUrl, agentWebId, requir
|
|
|
163
163
|
c.type === 'PaymentCondition' || c.type === 'https://webacl.org/ns#PaymentCondition'
|
|
164
164
|
);
|
|
165
165
|
if (paymentCondition) {
|
|
166
|
-
|
|
167
|
-
const cost =
|
|
166
|
+
const parsed = parseInt(paymentCondition.amount, 10);
|
|
167
|
+
const cost = Number.isNaN(parsed) ? -1 : parsed;
|
|
168
168
|
const currency = paymentCondition.currency || 'sat';
|
|
169
|
-
|
|
169
|
+
|
|
170
|
+
// Skip invalid amounts
|
|
171
|
+
if (cost < 0) continue;
|
|
172
|
+
|
|
173
|
+
if (agentWebId) {
|
|
170
174
|
try {
|
|
171
175
|
const ledger = await readLedger();
|
|
176
|
+
|
|
177
|
+
// Zero-cost gate: verify they have a ledger entry (have deposited at some point)
|
|
178
|
+
if (cost === 0) {
|
|
179
|
+
const hasEntry = ledger.entries?.some(e => e.url === agentWebId);
|
|
180
|
+
if (hasEntry) return { allowed: true };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Paid access: check balance and deduct
|
|
172
184
|
const balance = getBalance(ledger, agentWebId);
|
|
173
|
-
if (balance >= cost) {
|
|
174
|
-
// Deduct and grant access
|
|
185
|
+
if (cost > 0 && balance >= cost) {
|
|
175
186
|
debit(ledger, agentWebId, cost, currency === 'sats' ? 'sat' : currency);
|
|
176
187
|
const { writeLedger } = await import('../webledger.js');
|
|
177
188
|
await writeLedger(ledger);
|
package/test/wac.test.js
CHANGED
|
@@ -428,6 +428,30 @@ describe('WAC Conditions', () => {
|
|
|
428
428
|
assert.strictEqual(auths[0].conditions[0].type, 'UnknownFutureCondition');
|
|
429
429
|
});
|
|
430
430
|
|
|
431
|
+
it('should parse zero-cost PaymentCondition', async () => {
|
|
432
|
+
const acl = {
|
|
433
|
+
'@context': { 'acl': 'http://www.w3.org/ns/auth/acl#' },
|
|
434
|
+
'@graph': [{
|
|
435
|
+
'@id': '#gate',
|
|
436
|
+
'@type': 'acl:Authorization',
|
|
437
|
+
'acl:agentClass': { '@id': 'acl:AuthenticatedAgent' },
|
|
438
|
+
'acl:accessTo': { '@id': 'https://alice.example/members/' },
|
|
439
|
+
'acl:mode': [{ '@id': 'acl:Read' }],
|
|
440
|
+
'acl:condition': {
|
|
441
|
+
'@type': 'PaymentCondition',
|
|
442
|
+
'amount': '0',
|
|
443
|
+
'currency': 'sats'
|
|
444
|
+
}
|
|
445
|
+
}]
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
const auths = await parseAcl(JSON.stringify(acl), 'https://alice.example/members/.acl');
|
|
449
|
+
const condition = auths[0].conditions[0];
|
|
450
|
+
|
|
451
|
+
assert.strictEqual(condition.type, 'PaymentCondition');
|
|
452
|
+
assert.strictEqual(condition.amount, '0');
|
|
453
|
+
});
|
|
454
|
+
|
|
431
455
|
it('should parse PaymentCondition with all fields', async () => {
|
|
432
456
|
const acl = {
|
|
433
457
|
'@context': { 'acl': 'http://www.w3.org/ns/auth/acl#' },
|