Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduces a Custom Cache #199

Closed
wants to merge 1 commit into from
Closed

Introduces a Custom Cache #199

wants to merge 1 commit into from

Conversation

davidpatrick
Copy link
Contributor

By submitting a PR to this repository, you agree to the terms within the Auth0 Code of Conduct. Please see the contributing guidelines for how to create and submit a high-quality PR for this repo.

Description

There has been discussion around the need for a more flexible cache (issues/prs listed in references below). This PR introduces the ability for a developer to override the built-in cache and use their own Custom Cache. This replaces #141 as discussed here #141 (comment)

References

Testing

Added tests to cover the use of a custom cache. Additionally added a mock customCache for an idea of what a custom cache implementation might look like

Checklist

  • I have added documentation for new/changed functionality in this PR or in auth0.com/docs
  • All active GitHub checks for tests, formatting, and security are passing
  • The correct base branch is being used, if not master

@davidpatrick davidpatrick added this to the v1-next milestone Nov 19, 2020
@davidpatrick davidpatrick requested a review from a team as a code owner November 19, 2020 01:07
@adamjmcgrath adamjmcgrath self-assigned this Nov 19, 2020
const logger = debug('jwks');
const getSigningKey = client.getSigningKey;

logger(`Configured caching of signing keys. Max: ${cacheMaxEntries} / Age: ${cacheMaxAge}`);

if (customCache) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cache will need to fallback to getSigningKey when you get a miss (and populate the cache when getSigningKey returns something).

Otherwise your example:

client = new JwksClient({
    jwksUri: `${jwksHost}/.well-known/jwks.json`,
    cache: true,
    customCache: new CustomCache()
  });

Will never resolve anything

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally a custom cache should replace memoizer (the default in-memory cache) rather than sit in front of it.

Copy link
Contributor

@adamjmcgrath adamjmcgrath Nov 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps using something like cache-manager, eg

import cacheManager from 'cache-manager';

export default function(client, { cacheMaxEntries = 5, cacheMaxAge = ms('10m'), store = 'memory' } = options) {
  const memoryCache = cacheManager.caching({ store, max: cacheMaxEntries, ttl: cacheMaxAge });
  return (kid, cb) => memoryCache.wrap(kid, (cacheCallback) => {
    client.getSigningKey(kid, cacheCallback);
  }, {}, cb);
}

You'd ideally need to provide an lru cache as your custom cache, then the cache will drop the least recently used when size > cacheMaxEntries or cache item age > cacheMaxAge. eg https://github.com/BryanDonovan/node-cache-manager#custom-stores

@davidpatrick
Copy link
Contributor Author

Thanks for the review @adamjmcgrath. I will look at replacing the lru-memoizer with the underlying lru-cache directly and allowing replacement of that implementation

@davidpatrick
Copy link
Contributor Author

As mentioned in #202

This PR is to replace #199 and addresses #201. The reason for closing #199, is that a custom cache looked to be the right solution until we started implementing it from the user standpoint. As it would require an async cache and ended up being a lot of heavy lifting for the user in order to address the issues referenced in #201.

@davidpatrick davidpatrick removed this from the v1-next milestone Dec 8, 2020
@davidpatrick davidpatrick deleted the custom-cache branch March 1, 2021 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to load from file ?
2 participants