简体   繁体   English

安全存储访问令牌

[英]Securely storing an access token

What security measures should I put in place to ensure that, were my database to be compromised, long-life access tokens could not be stolen? 我应该采取哪些安全措施来确保我的数据库遭到入侵,长期访问令牌不会被盗?

A long-life access token is as good as a username and password for a particular service, but from talking to others it seems most (myself included) store access tokens in plain text. 长期访问令牌与特定服务的用户名和密码一样好,但是通过与其他人交谈,它似乎最多(包括我自己)以纯文本形式存储访问令牌。 This seems to be to be just as bad as storing a password in plain text. 这似乎与在纯文本中存储密码一样糟糕。 Obviously one cannot salt & hash the token. 显然,人们不能对令牌进行加盐和散列。

Ideally I'd want to encrypt them, but I'm unsure of the best way to do this, especially on an open source project. 理想情况下我想加密它们,但我不确定最好的方法,特别是在开源项目上。

I imagine the answer to this question is similar to one on storing payment info and PCI compliance, but I'd also ask why there isn't more discussion of this? 我想这个问题的答案类似于存储支付信息和PCI合规性的答案,但我也会问为什么没有更多的讨论呢? Perhaps I'm missing something. 也许我错过了一些东西。

Do you just want to verify a token provided by others? 您是否只想验证他人提供的令牌? If so, treat it as you would a password. 如果是这样,请将其视为密码。 Use a byte derivation algorithm like Password Based Key Derivation Function 2 (PBKDF2) (also described in RFC 2898 ) with 10,000 iterations and store the first 20 bytes or so. 使用字节派生算法,如基于密码的密钥派生函数2(PBKDF2) (也在RFC 2898中描述),具有10,000次迭代并存储前20个字节左右。 When the token is received. 收到令牌时。 It is not practically reversible. 它实际上并不可逆。

Do you want to present the token to others for authentication? 您是否要将令牌呈现给其他人进行身份验证? If so, this is a challenge because, if your application can decrypt or otherwise get access to the token, so can an attacker. 如果是这样,这是一个挑战,因为如果您的应用程序可以解密或以其他方式访问令牌,攻击者也可以。 Think Shannon's Maxim, the attacker knows the system, especially for an open source project. 想想Shannon的Maxim,攻击者就知道这个系统,特别是对于一个开源项目。

In this case, the best approach is to encrypt the tokens with a strong algorithm (eg AES256), generate keys using a strong cryptographic standard random number generator and store the key(s) securely in a different location to the data, such as in a permission protected file outside the database in the example above. 在这种情况下,最好的方法是使用强算法(例如AES256)加密令牌,使用强加密标准随机数生成器生成密钥,并将密钥安全地存储在与数据不同的位置,例如上例中数据库外的受权限保护文件。 The latter means that SQL injection attacks will not reveal the keys. 后者意味着SQL注入攻击不会泄露密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM