存储外部 access/refresh 个令牌
Storing external access/refresh tokens
我创建了一个 Laravel 应用程序,它请求我的用户使用 oAuth(授权代码)访问外部应用程序。很好,我可以将外部应用程序连接到我的用户帐户。但是,外部 API 给了我一个访问和刷新令牌。访问令牌明显过期,刷新令牌没有。
一旦用户授予我访问权限,我需要将这些令牌存储在某个地方。特别是刷新令牌。访问令牌过期后,我需要刷新访问令牌。我的“问题”是,我不太确定将这些令牌存储在哪里。我想到了几个选项:
- 在数据库中存储加密令牌,链接到用户
- 将它们存储在缓存中
- 将它们存储在 Redis 数据库中
- ...
有多种选择,但我正在寻找最安全的一种。将加密的令牌存储在数据库中不是我最喜欢的选择,但却是最持久的选择。可以有意或无意地清除缓存。清除缓存后,我需要用户再次授予我访问他们帐户的权限。
存储这些凭据的最佳方式是什么?
安全
What's the best way of storing these credentials?
There are many options, but I'm looking for the safest one.
无论将其存储在何处,始终以加密格式进行。这样即使泄露了也不能重复使用,除非加密密钥也泄露了。
刷新令牌持久性
Storing the encrypted tokens in the database isn't my favorite choice, but the most persistent one.
您可以使用会话、JWT 令牌或直接进入数据库来完成。让我们看看选项...
Laravel 会话数
如果您在 Laravel 应用程序中使用用户会话,您可以将其加密存储在每个用户的会话中。
Since HTTP driven applications are stateless, sessions provide a way to store information about the user across multiple requests. Laravel ships with a variety of session backends that are accessed through an expressive, unified API. Support for popular backends such as Memcached, Redis, and databases is included out of the box.
Laravel 还支持将会话存储在加密的 cookie 中。
智威汤逊代币
如果您使用的是 JWT 令牌,那么您可能使用的是 JWS 令牌,如果是这样,请将其加密存储在 JWS 声明中,甚至更好,请使用 JWE 令牌。
JWT 令牌中的声明是 JWT 令牌有效负载中的 key/value 对。 JWT Token由header.payload.signature
组成。负载示例:
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
A JSON Web Signature (abbreviated JWS) is an IETF-proposed standard (RFC 7515) for signing arbitrary data.[1] This is used as the basis for a variety of web-based technologies including JSON Web Token.
JSON Web Encryption (JWE) is an IETF standard providing a standardised syntax for the exchange of encrypted data, based on JSON and Base64.[1] It is defined by RFC7516. Along with JSON Web Signature (JWS), it is one of the two possible formats of a JWT (JSON Web Token). JWE forms part of the JavaScript Object Signing and Encryption (JOSE) suite of protocols.
您可以在 https://jwt.io/introduction 了解更多关于 JWT 的信息。
数据库
使用对您来说更方便的数据库,也就是您的应用程序中已有的数据库。不要仅仅为了存储刷新令牌而引入 Redis,但如果您已经在使用 Redis,那么它可以作为替代方案,但我只是将其加密存储在您已经存储用户信息的数据库中。毕竟刷新令牌不是你在每个请求中都执行的操作,因此这里的性能可能不是那么关键。
内存和加密数据库是您最好的选择。内存是不够的,因为在任何类型的内存刷新的情况下你都会丢失它。所以你留下了加密的 DB
我创建了一个 Laravel 应用程序,它请求我的用户使用 oAuth(授权代码)访问外部应用程序。很好,我可以将外部应用程序连接到我的用户帐户。但是,外部 API 给了我一个访问和刷新令牌。访问令牌明显过期,刷新令牌没有。
一旦用户授予我访问权限,我需要将这些令牌存储在某个地方。特别是刷新令牌。访问令牌过期后,我需要刷新访问令牌。我的“问题”是,我不太确定将这些令牌存储在哪里。我想到了几个选项:
- 在数据库中存储加密令牌,链接到用户
- 将它们存储在缓存中
- 将它们存储在 Redis 数据库中
- ...
有多种选择,但我正在寻找最安全的一种。将加密的令牌存储在数据库中不是我最喜欢的选择,但却是最持久的选择。可以有意或无意地清除缓存。清除缓存后,我需要用户再次授予我访问他们帐户的权限。
存储这些凭据的最佳方式是什么?
安全
What's the best way of storing these credentials? There are many options, but I'm looking for the safest one.
无论将其存储在何处,始终以加密格式进行。这样即使泄露了也不能重复使用,除非加密密钥也泄露了。
刷新令牌持久性
Storing the encrypted tokens in the database isn't my favorite choice, but the most persistent one.
您可以使用会话、JWT 令牌或直接进入数据库来完成。让我们看看选项...
Laravel 会话数
如果您在 Laravel 应用程序中使用用户会话,您可以将其加密存储在每个用户的会话中。
Since HTTP driven applications are stateless, sessions provide a way to store information about the user across multiple requests. Laravel ships with a variety of session backends that are accessed through an expressive, unified API. Support for popular backends such as Memcached, Redis, and databases is included out of the box.
Laravel 还支持将会话存储在加密的 cookie 中。
智威汤逊代币
如果您使用的是 JWT 令牌,那么您可能使用的是 JWS 令牌,如果是这样,请将其加密存储在 JWS 声明中,甚至更好,请使用 JWE 令牌。
JWT 令牌中的声明是 JWT 令牌有效负载中的 key/value 对。 JWT Token由header.payload.signature
组成。负载示例:
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
A JSON Web Signature (abbreviated JWS) is an IETF-proposed standard (RFC 7515) for signing arbitrary data.[1] This is used as the basis for a variety of web-based technologies including JSON Web Token.
JSON Web Encryption (JWE) is an IETF standard providing a standardised syntax for the exchange of encrypted data, based on JSON and Base64.[1] It is defined by RFC7516. Along with JSON Web Signature (JWS), it is one of the two possible formats of a JWT (JSON Web Token). JWE forms part of the JavaScript Object Signing and Encryption (JOSE) suite of protocols.
您可以在 https://jwt.io/introduction 了解更多关于 JWT 的信息。
数据库
使用对您来说更方便的数据库,也就是您的应用程序中已有的数据库。不要仅仅为了存储刷新令牌而引入 Redis,但如果您已经在使用 Redis,那么它可以作为替代方案,但我只是将其加密存储在您已经存储用户信息的数据库中。毕竟刷新令牌不是你在每个请求中都执行的操作,因此这里的性能可能不是那么关键。
内存和加密数据库是您最好的选择。内存是不够的,因为在任何类型的内存刷新的情况下你都会丢失它。所以你留下了加密的 DB