Skip to content

How to gracefully refresh accesstoken instead of using static passwords? #1810

@tago-SE

Description

@tago-SE

How to work with password which expire after some time or that are not present initially?

Microsoft released an example of how to set it up using ioredis, but the problem is that I cannot get it to work as I want.

Global Redis Client

Having only one global redis client and once the access token expires you would need to ensure that it swaps the old instance with a new instance after some period of time, but would such a solution which is suggested in azure-sdk-for-js even work?

  • Will the swap cause the old redis client to drop pending commands?
  • You would need to ensure that the old client is diconnected gracefully.
  • [Issue] The initial client will have incorrect password until it runs out of reconnection attempts and uses the latest changes in the options object. The only way to get around this seem to be to update the redis.options.username/password after a successful auth command.
  • How would this work when running in dev?

Local Redis Client

You create an async factory for providing redis clients with the latest password and ensure that the client is freed after it has been used within a small scope. No need to worry about updating the redis options to match the latest password after the access token expires. However, the issue I ran into with this solution is that it eventually ran out of connections. 'ERR max number of clients reached'. Do you have any recommendations for going this route?

// Example
  const deleteAccount = async (id: string) => {
    const client = await getRedisClientAsync();
    try {
      const key = getAccountKey(id);
      const account = (await loadObjectFromHash(key)) as AdapterAccount;
      if (!account.userId) return null;    
      await client.hdel(key);
      await client.del(getAccountByUserIdKey(account.userId));

    } finally {
      client.quit();
    }
  };

Question
Is there any guidance on implementing access token based redis clients. Or how can I override the default way redis handles connections to use a username/password that is resolved asyncronously after the client has been created? It would be good if these overrides could be added to the constructor so that developers can work with access tokens without having to hack around it like I'm trying to do.

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions