Skip to content

Commit

Permalink
don't consume token
Browse files Browse the repository at this point in the history
  • Loading branch information
eike-hass committed May 13, 2024
1 parent 220178c commit 1872480
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
14 changes: 8 additions & 6 deletions oid4vc/TangleLabs/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import pkg from 'memory-cache-node';
const { MemoryCache } = pkg;
import type { MemoryCache as IMemoryCache } from 'memory-cache-node';

const itemsExpirationCheckIntervalInSecs = 10 * 60;
const itemsExpirationCheckIntervalInSecs = 5 * 60; // 5 Minutes
const defaultTimeToLiveInSecs = 10 * 60; // 10 Minutes
const maxItemCount = 1000000;

export class Cache<K,V> {
Expand All @@ -17,17 +18,18 @@ export class Cache<K,V> {
return new Cache<K,V>(new MemoryCache<K,V>(itemsExpirationCheckIntervalInSecs, maxItemCount));
}

async storeItem(key: K, item: V) {
return this.memoryCache.storePermanentItem(key, item);
async storeItem(key: K, item: V, TTL: number = defaultTimeToLiveInSecs) {
console.debug(`storing key=${key}, TTL=${TTL} with value=`, item);
return this.memoryCache.storeExpiringItem(key, item, TTL);
}

async hasItem(key: K) {
return this.memoryCache.hasItem(key);
}

async consumeItem(key: K) {
const item = this.memoryCache.retrieveItemValue(key);
this.memoryCache.removeItem(key);
async retrieveItem(key: K) {
const item = this.memoryCache.retrieveItemValue(key);
console.debug(`retrieving key=${key} with value=`, item);
return item;
}

Expand Down
12 changes: 7 additions & 5 deletions oid4vc/TangleLabs/src/httpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ export const createServer = (

app.route("/api/offer/:id").get(
asyncHandler(async (req, res) => {
console.debug(req);
console.debug(req.params);
const offer_id = req.params.id;

const offer = await tokenCache.consumeItem(offer_id);
// TODO: consider consuming the token
const offer = await tokenCache.retrieveItem(offer_id);

if (!offer) {
res.status(500).send();
Expand All @@ -47,10 +48,11 @@ export const createServer = (

app.route("/api/credential-offer/:id").get(
asyncHandler(async (req, res) => {
console.debug(req);
console.debug(req.params);
const offer_id = req.params.id;

const offer = await credentialCache.consumeItem(offer_id);

// TODO: consider consuming the token
const offer = await credentialCache.retrieveItem(offer_id);

if (!offer) {
res.status(500).send();
Expand Down

0 comments on commit 1872480

Please sign in to comment.