快速背景:
与AngularJS服务器对话的完整Javascript SPA REstful客户端。我正在尝试为API服务器进行最好的身份验证。客户端将有角色,我不担心用户是否能够看到客户端的区域,因为服务器应该是密封的。
身份验证流程:
问题:
如有任何建议或例子,将不胜感激。
发布于 2014-02-04 20:28:44
我目前正在使用angularjs+node作为REST进行类似的情况,使用HMAC进行身份验证。
不过,我正在做这方面的工作,所以我的调子在任何时候都可能发生变化。不过,这是我有的东西。任何愿意在这方面戳洞的人,我也欢迎这一点:
- The key is stored in nodejs memory and regenerates every six hours, keeping record of the last key generated. For 10 seconds after the key changes, the server actually generates two HMACs; one with the new key, one with the old key. That way requests that are made while the key changed are still valid. If the key changed, the server sends the new one back to the client so its can flash it in LocalStorage. The key is a SHA256 of a UUID generated with node-uuid, hashed with crypto. And after typing this out, i realize this may not scale well, but anyway ...
- `Auth-Signature`: HMAC of `username`+`time`+`request.body` (in my case `request.body` is a `JSON.stringify()`'d representation of the request vars) signed with the locally stored key
- `Auth-Username`: the `username`
- `X-Microtime`: A unix timestamp of when the client generated its HMAC
X-Microtime头,如果X-Microtime和now之间的间隔大于10秒,则删除请求作为潜在的重放攻击,并返回401。Auth-Username+X-Microtime+req.body使用节点内存中的6小时私钥。Auth-Username上任何特定于用户的内容,那么我们就有了API头。显然,所有这些通信都是在HTTPS上进行的。
编辑:
在每次成功请求之后,必须将密钥返回给客户端,以便使客户端与动态密钥保持最新状态。这是有问题的,因为它所做的事情与cookie所做的基本相同。
您可以使密钥保持静态且永不更改,但这似乎不太安全,因为密钥永远不会过期。您还可以为每个用户分配一个键,在登录时返回给客户端,但是无论如何,您仍然必须对每个请求进行用户查找,最好在此时使用基本的auth。
编辑#2
所以,在做了一些自己的测试之后,我决定使用一个后端代理来使用我的REST,它仍然使用HMAC。
这使我们能够将私有比特保留在前端之外,同时仍然允许我们构建一个API,该API可以在不对每个请求进行DB调用的情况下快速进行身份验证,并且保持状态无状态。它还允许我们的API为其他接口服务,比如本地移动应用程序。移动应用程序将与私钥捆绑在一起,并为每个请求运行HMAC序列。
https://stackoverflow.com/questions/21540251
复制相似问题