Skip to content

Commit 1424aaa

Browse files
committed
Move SLC get+refresh route implementation to getFresh().
1 parent 3f11a9a commit 1424aaa

File tree

2 files changed

+56
-43
lines changed

2 files changed

+56
-43
lines changed

lib/http.js

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -135,47 +135,7 @@ export async function addRoutes({app, service} = {}) {
135135
const {config} = req.serviceObject;
136136
const {slcId} = req.params;
137137
const id = `${config.id}${cfg.routes.slcs}/${encodeURIComponent(slcId)}`;
138-
while(true) {
139-
try {
140-
const record = await slcs.get({id});
141-
// treat expired SLC as `NotFoundError`, but allow for auto-refresh
142-
// below; get `now` as a minute into the future to ensure any
143-
// refreshed VC is still valid once returned to the client
144-
const now = new Date();
145-
now.setTime(now.getTime() + 1000 * 60);
146-
// FIXME: support v2 VCs w/`validUntil`
147-
const validUntil = new Date(record.credential.expirationDate);
148-
if(now <= validUntil) {
149-
// SLC not expired
150-
res.json(record.credential);
151-
return;
152-
}
153-
throw new BedrockError(
154-
'Status list credential not found.',
155-
'NotFoundError', {
156-
statusListCredential: id,
157-
httpStatusCode: 404,
158-
public: true
159-
});
160-
} catch(e) {
161-
if(e.name !== 'NotFoundError') {
162-
// unrecoverable error
163-
throw e;
164-
}
165-
try {
166-
// SLC not found, perhaps expired or not published; try
167-
// auto-refresh
168-
console.log('***********AUTO REFRESH***********');
169-
const doc = await slcs.refresh({id, config});
170-
res.json(doc.content);
171-
return;
172-
} catch(refreshError) {
173-
// log refresh error
174-
logger.error(refreshError.message, {error: refreshError});
175-
// if refresh fails, throw original `NotFoundError`
176-
throw e;
177-
}
178-
}
179-
}
138+
const {credential} = await slcs.getFresh({id, config});
139+
res.json(credential);
180140
}));
181141
}

lib/slcs.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import assert from 'assert-plus';
1010
import {createDocumentLoader} from './documentLoader.js';
1111
import {decodeList} from '@digitalbazaar/vc-status-list';
12+
import {logger} from './logger.js';
1213
import {LruCache} from '@digitalbazaar/lru-memoize';
1314

1415
const {util: {BedrockError}} = bedrock;
@@ -104,6 +105,59 @@ export async function get({id} = {}) {
104105
return SLC_CACHE.memoize({key: id, fn});
105106
}
106107

108+
/**
109+
* Gets the published credential for the given status list credential ID,
110+
* refreshing it if it has expired or is not found.
111+
*
112+
* @param {object} options - The options to use.
113+
* @param {string} options.id - The ID of the status list credential.
114+
* @param {object} options.config - The issuer config.
115+
*
116+
* @returns {Promise<object>} Resolves to the stored record.
117+
*/
118+
export async function getFresh({id, config} = {}) {
119+
while(true) {
120+
try {
121+
const record = await get({id});
122+
// treat expired SLC as `NotFoundError`, but allow for auto-refresh
123+
// below; get `now` as a minute into the future to ensure any
124+
// refreshed VC is still valid once returned to the client
125+
const now = new Date();
126+
now.setTime(now.getTime() + 1000 * 60);
127+
// FIXME: support v2 VCs w/`validUntil`
128+
const validUntil = new Date(record.credential.expirationDate);
129+
if(now <= validUntil) {
130+
// SLC not expired
131+
return {credential: record.credential};
132+
}
133+
throw new BedrockError(
134+
'Status list credential not found.',
135+
'NotFoundError', {
136+
statusListCredential: id,
137+
httpStatusCode: 404,
138+
public: true
139+
});
140+
} catch(e) {
141+
if(e.name !== 'NotFoundError') {
142+
// unrecoverable error
143+
throw e;
144+
}
145+
try {
146+
// SLC not found, perhaps expired or not published; try
147+
// auto-refresh
148+
console.log('***********AUTO REFRESH***********');
149+
const doc = await refresh({id, config});
150+
return {credential: doc.content};
151+
} catch(refreshError) {
152+
// log refresh error
153+
logger.error(refreshError.message, {error: refreshError});
154+
// if refresh fails, throw original `NotFoundError`
155+
throw e;
156+
}
157+
}
158+
}
159+
}
160+
107161
/**
108162
* Returns true if a status list credential has been stored and false if not.
109163
*
@@ -135,7 +189,6 @@ export async function refresh({id, config} = {}) {
135189
assert.string(id, 'id');
136190
assert.object(config, 'config');
137191

138-
// FIXME: pass `documentStore` instead?
139192
const [documentLoader, documentStore] = await Promise.all([
140193
createDocumentLoader({config}),
141194
getDocumentStore({config})

0 commit comments

Comments
 (0)