在Node中创建唯一的API密钥以存储在Redis中

时间:2021-04-21 04:25:29

I'm building an API in Node and looking to store the API key/access token in Redis.

我正在Node中构建一个API,并希望在Redis中存储API密钥/访问令牌。

How best can I generate a unique API key/access token to store in Redis as the key and an email address as the value?

我如何才能最好地生成一个唯一的API密钥/访问令牌,以将Redis存储为密钥,将电子邮件地址存储为值?

Does Redis have an inbuilt function for generating unique ids with certain length and character set or how best should I tackle this?

Redis是否具有内置函数来生成具有特定长度和字符集的唯一ID,或者我应该如何最好地解决这个问题?

Thank you!

3 个解决方案

#1


1  

The INCR command of Redis allows you to increment a counter and so generate unique numeric IDs. As far as I know, there is not other built-in feature in redis to generate a unique ID.

Redis的INCR命令允许您递增计数器,从而生成唯一的数字ID。据我所知,redis中没有其他内置功能可以生成唯一ID。

If you need a more complex ID, you will have to generate it in node, like said Brendan in the comments. You could combine redis INCR to generate numeric values and then provide it to a library like hashids.

如果您需要更复杂的ID,则必须在节点中生成它,就像评论中的Brendan所说的那样。您可以将redis INCR组合起来生成数值,然后将其提供给像hashids这样的库。

#2


1  

Like Darryl West said, you should definitely generate the tokens in node. However, if you don't want to take a dependency on uuid then you can always use node's built-in crypto to create random bytes and convert to a string.

就像Darryl West所说,你应该在节点中生成令牌。但是,如果您不想依赖于uuid,那么您始终可以使用node的内置加密来创建随机字节并转换为字符串。

var crypto = require('crypto')

function generateToken() {
  return crypto.randomBytes(48).toString('base64');
}

See crypto.randomBytes(size[, callback]) for more info.

有关详细信息,请参阅crypto.randomBytes(size [,callback])。

#3


0  

A good solution for unique Ids in redis, especially with multiple redis instances is to use a combination of a domain name and uuid (see Brendan's response). So, for an application that inserts new users with a domain name of 'User' you would create an id like this:

redis中唯一ID的一个很好的解决方案,特别是对于多个redis实例,是使用域名和uuid的组合(参见Brendan的回复)。因此,对于插入域名为“User”的新用户的应用程序,您将创建如下ID:

var uuid = require('node-uuid');

var createUniqueId = function() {
    return 'User:' + uuid.v4();
};

Other examples of this approach are described here for leveldb but applicable to redis.

此处针对leveldb描述了此方法的其他示例,但适用于redis。

#1


1  

The INCR command of Redis allows you to increment a counter and so generate unique numeric IDs. As far as I know, there is not other built-in feature in redis to generate a unique ID.

Redis的INCR命令允许您递增计数器,从而生成唯一的数字ID。据我所知,redis中没有其他内置功能可以生成唯一ID。

If you need a more complex ID, you will have to generate it in node, like said Brendan in the comments. You could combine redis INCR to generate numeric values and then provide it to a library like hashids.

如果您需要更复杂的ID,则必须在节点中生成它,就像评论中的Brendan所说的那样。您可以将redis INCR组合起来生成数值,然后将其提供给像hashids这样的库。

#2


1  

Like Darryl West said, you should definitely generate the tokens in node. However, if you don't want to take a dependency on uuid then you can always use node's built-in crypto to create random bytes and convert to a string.

就像Darryl West所说,你应该在节点中生成令牌。但是,如果您不想依赖于uuid,那么您始终可以使用node的内置加密来创建随机字节并转换为字符串。

var crypto = require('crypto')

function generateToken() {
  return crypto.randomBytes(48).toString('base64');
}

See crypto.randomBytes(size[, callback]) for more info.

有关详细信息,请参阅crypto.randomBytes(size [,callback])。

#3


0  

A good solution for unique Ids in redis, especially with multiple redis instances is to use a combination of a domain name and uuid (see Brendan's response). So, for an application that inserts new users with a domain name of 'User' you would create an id like this:

redis中唯一ID的一个很好的解决方案,特别是对于多个redis实例,是使用域名和uuid的组合(参见Brendan的回复)。因此,对于插入域名为“User”的新用户的应用程序,您将创建如下ID:

var uuid = require('node-uuid');

var createUniqueId = function() {
    return 'User:' + uuid.v4();
};

Other examples of this approach are described here for leveldb but applicable to redis.

此处针对leveldb描述了此方法的其他示例,但适用于redis。