Skip to content

Commit

Permalink
Move SLC get+refresh route implementation to getFresh().
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Feb 19, 2024
1 parent 3f11a9a commit 1424aaa
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 43 deletions.
44 changes: 2 additions & 42 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,47 +135,7 @@ export async function addRoutes({app, service} = {}) {
const {config} = req.serviceObject;
const {slcId} = req.params;
const id = `${config.id}${cfg.routes.slcs}/${encodeURIComponent(slcId)}`;
while(true) {
try {
const record = await slcs.get({id});
// treat expired SLC as `NotFoundError`, but allow for auto-refresh
// below; get `now` as a minute into the future to ensure any
// refreshed VC is still valid once returned to the client
const now = new Date();
now.setTime(now.getTime() + 1000 * 60);
// FIXME: support v2 VCs w/`validUntil`
const validUntil = new Date(record.credential.expirationDate);
if(now <= validUntil) {
// SLC not expired
res.json(record.credential);
return;
}
throw new BedrockError(
'Status list credential not found.',
'NotFoundError', {
statusListCredential: id,
httpStatusCode: 404,
public: true
});
} catch(e) {
if(e.name !== 'NotFoundError') {
// unrecoverable error
throw e;
}
try {
// SLC not found, perhaps expired or not published; try
// auto-refresh
console.log('***********AUTO REFRESH***********');
const doc = await slcs.refresh({id, config});
res.json(doc.content);
return;
} catch(refreshError) {
// log refresh error
logger.error(refreshError.message, {error: refreshError});
// if refresh fails, throw original `NotFoundError`
throw e;
}
}
}
const {credential} = await slcs.getFresh({id, config});
res.json(credential);
}));
}
55 changes: 54 additions & 1 deletion lib/slcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import assert from 'assert-plus';
import {createDocumentLoader} from './documentLoader.js';
import {decodeList} from '@digitalbazaar/vc-status-list';
import {logger} from './logger.js';
import {LruCache} from '@digitalbazaar/lru-memoize';

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

/**
* Gets the published credential for the given status list credential ID,
* refreshing it if it has expired or is not found.
*
* @param {object} options - The options to use.
* @param {string} options.id - The ID of the status list credential.
* @param {object} options.config - The issuer config.
*
* @returns {Promise<object>} Resolves to the stored record.
*/
export async function getFresh({id, config} = {}) {
while(true) {
try {
const record = await get({id});
// treat expired SLC as `NotFoundError`, but allow for auto-refresh
// below; get `now` as a minute into the future to ensure any
// refreshed VC is still valid once returned to the client
const now = new Date();
now.setTime(now.getTime() + 1000 * 60);
// FIXME: support v2 VCs w/`validUntil`
const validUntil = new Date(record.credential.expirationDate);
if(now <= validUntil) {
// SLC not expired
return {credential: record.credential};
}
throw new BedrockError(
'Status list credential not found.',
'NotFoundError', {
statusListCredential: id,
httpStatusCode: 404,
public: true
});
} catch(e) {
if(e.name !== 'NotFoundError') {
// unrecoverable error
throw e;
}
try {
// SLC not found, perhaps expired or not published; try
// auto-refresh
console.log('***********AUTO REFRESH***********');
const doc = await refresh({id, config});
return {credential: doc.content};
} catch(refreshError) {
// log refresh error
logger.error(refreshError.message, {error: refreshError});
// if refresh fails, throw original `NotFoundError`
throw e;
}
}
}
}

/**
* Returns true if a status list credential has been stored and false if not.
*
Expand Down Expand Up @@ -135,7 +189,6 @@ export async function refresh({id, config} = {}) {
assert.string(id, 'id');
assert.object(config, 'config');

// FIXME: pass `documentStore` instead?
const [documentLoader, documentStore] = await Promise.all([
createDocumentLoader({config}),
getDocumentStore({config})
Expand Down

0 comments on commit 1424aaa

Please sign in to comment.