简体   繁体   English

在Javascript Web App中保护私有API密钥

[英]Securing Private API keys in Javascript Web App

How can I securely store the keys to my private API in my web based (javascript) application? 如何在基于Web的(javascript)应用程序中将密钥安全地存储到我的私有API中?

I've thoroughly researched this but am yet to find a solid answer or any kind of standard procedure. 我已经彻底研究了这个,但我还没有找到一个可靠的答案或任何标准的程序。

My API implements HMAC authentication, and every member of my site has a public/secret key to authenticate their use of the API and sign their request. 我的API实现了HMAC身份验证,我站点的每个成员都有一个公钥/密钥来验证他们对API的使用并签署​​他们的请求。

On a mobile app, the server would send the public/secret keys over an encrypted connection to be stored on the device after a successful login request. 在移动应用程序上,服务器将通过加密连接发送公钥/密钥,以在成功登录请求后存储在设备上。 Subsequent requests would be signed using these keys. 后续请求将使用这些密钥进行签名。

So where should I store the keys on the client in this case, considering that the source code is visible to anyone who knows how to use a web inspector? 那么在这种情况下我应该将密钥存储在客户端上,考虑到知道如何使用Web检查器的任何人都可以看到源代码? In reality it's not an issue if the authenticated user sees their own keys, as they're unlikely to share them just like they'd be unlikely to share their account username/password with anyone else. 实际上,如果经过身份验证的用户看到自己的密钥,则不会出现问题,因为他们不太可能分享他们,就像他们不太可能与其他任何人分享他们的帐户用户名/密码一样。


Possible Solutions? 可能的解决方案?

Store the public/secret keys in cookies, and retrieve using javascript - Probably not too secure, can the cookies be reliably cleared when the user logs out/their session ends? 将公钥/密钥存储在cookie中,并使用javascript检索 - 可能不太安全,可以在用户注销/会话结束时可靠地清除cookie吗?

Store the public/secret keys in the web-page itself - Only real solution I can think of - Javascript can access the keys through the DOM, and they can only be compromised(seen) if the user leaves their account logged in, and somebody knows where to look for them. 将公钥/密钥存储在网页本身 - 只有我能想到的真正的解决方案--Javascript可以通过DOM访问密钥,只有当用户离开他们的帐户时才会被泄露(见),有人知道在哪里寻找它们。

Note: The web app is not a single-page app, so storing the public/secret keys within the memory is not an option. 注意:Web应用程序不是单页应用程序,因此不能在内存中存储公钥/密钥。


Not sure if i'm on the right track at all (something tells me I'm not), hopefully someone can set me straight on this. 不确定我是否在正确的轨道上(有些东西告诉我,我不是),希望有人可以让我直截了当。

Thanks 谢谢

I've come to the conclusion that what i'm trying to protect against here is CSRF.. 我得出结论,我在这里想要保护的是CSRF ..

I'm using laravel, so my solution was to add the csrf nonce (Session::token()) in the head as seen here . 我正在使用laravel,所以我的解决方案是在头部添加csrf nonce(Session :: token()), 如此处所示

<meta name="token" content="32947fh2834fgkhgfr8724234f">

And send it along in the request header: 并在请求标头中发送它:

$.ajaxSetup({
    headers: { 'session_token': '32947fh2834fgkhgfr8724234f' }
});

When authorising the api request it's a case of checking for the 'session_token' header and validating it's authenticity. 在授权api请求时,检查“session_token”标头并验证其真实性是一种情况。 If the session_token isn't there it'll fall back to the HMAC check. 如果session_token不在那里,它将回退到HMAC检查。

Coupled with ssl this should be enough, Thoughts? 加上ssl这应该够了,思考?

Not sure there's a clear solution at all. 不确定有一个明确的解决方案。 It sounds like you're trying to store a "secret" in a browser? 听起来你正试图在浏览器中存储“秘密”? There will aways be a way to view it, even if you try to obfuscate it. 即使您试图对其进行模糊处理,也无法查看它。

Greeting, 问候,

Using API key and Token on the frontend applications always has a demand for storing key or token securely, unfortunately, the browser(client) has no such storage. 在前端应用程序上使用API​​密钥和令牌始终需要安全地存储密钥或令牌,遗憾的是,浏览器(客户端)没有这样的存储。 This scenario can be overcome by using Access token (if Oauth) instead of API key after the authentication and these access token also can have their own expiration. 在身份验证之后使用Access令牌(如果是Oauth)而不是API密钥可以克服这种情况,并且这些访问令牌也可以拥有自己的过期时间。

if really need an additional security against the stored Key/Token in the client side, In that case, we would have come up with a solution like encrypting our tokens using base64 kind of method with some hashing algorithm and decrypting and validating in the API endpoint would be the solution. 如果真的需要针对客户端存储的密钥/令牌的额外安全性,在这种情况下,我们会想出一个解决方案,比如使用base64类方法加密我们的令牌,使用一些散列算法并在API端点中解密和验证将是解决方案。

The following blog post may be helpful in this case: 在这种情况下,以下博文可能会有所帮助:

https://www.wolfe.id.au/2012/10/20/what-is-hmac-authentication-and-why-is-it-useful/ https://www.wolfe.id.au/2012/10/20/what-is-hmac-authentication-and-why-is-it-useful/

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

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