简体   繁体   English

带有用于用户身份验证的 RESTful API 的本地存储。 这是一种安全的方法吗?

[英]Local Storage with RESTful API for user authentication. Is this a safe method?

I would really like some opinions on whether the following is a safe method as user authentication, and if not, please point out it's shortcomings.我很想知道以下作为用户身份验证是否安全的方法,如果不是,请指出它的缺点。

  • React front end React前端
  • PHP / MySQL based RESTful API on remote server远程服务器上基于PHP / MySQL的 RESTful API

1) user signs up, data is posted to the API, user is emailed a single use activation link to ensure email is valid before they can access their account. 1) 用户注册,数据发布到 API,用户通过电子邮件发送一次性激活链接以确保电子邮件有效,然后他们才能访问他们的帐户。

2) user signs in, API validates the data in all of the usual ways and then sends back a JSON object containing their user ID and an access token. 2) 用户登录,API 以所有常用方式验证数据,然后发回包含用户 ID 和访问令牌的 JSON 对象。

3) user ID and access token are set in localStorage on the users device upon receiving the data. 3) 收到数据后,在用户设备的localStorage中设置用户ID 和访问令牌。 React then grabs that data from localStorage and uses it to set and control the state in Redux stores providing an App wide Auth state. React 然后从localStorage该数据并使用它来设置和控制Redux商店中的状态,提供应用程序范围的 Auth 状态。

4) user ID and access token are sent along with every future request made to the API. 4) 用户 ID 和访问令牌与以后向 API 发出的每个请求一起发送。 In the instances where a user isn't logged in, ie - they don't supply a valid user id with matching token, they are automatically prevented from requesting anything that requires authentication at the very first entry point of the API.在用户未登录的情况下,即 - 他们没有提供具有匹配令牌的有效用户 ID,他们将被自动阻止在 API 的第一个入口点请求任何需要身份验证的内容。 Suitable responses are sent back which in turn update the front end state to reflect a non-logged in user.合适的响应被发回,进而更新前端状态以反映未登录的用户。

5) When the user logs out the access Token is deleted from localStorage . 5) 当用户注销时,访问令牌从localStorage删除。

A bit more detail about some the inner workings :关于一些内部工作的更多细节:

  • All tokens are generated server side and stored in the DB, they are random and unique bin2hex(random_bytes(32)) and only valid when supplied with the matching user ID.所有令牌都在服务器端生成并存储在数据库中,它们是随机且唯一的bin2hex(random_bytes(32))并且仅在提供匹配的用户 ID 时有效。 So changing the user ID in a request will result in a failed auth response, as will supplying a valid user ID with a mismatching or expired token.因此,更改请求中的用户 ID 将导致身份验证响应失败,提供具有不匹配或过期令牌的有效用户 ID 也是如此。

  • Tokens are single use and a new token is generated, stored and then sent back in the response from every authenticated request along with the corresponding user ID.令牌是一次性使用的,并且会生成、存储一个新令牌,然后在来自每个经过身份验证的请求的响应中连同相应的用户 ID 一起发回。 This may be overkill and put a lot of extra strain on the server.这可能是矫枉过正,会给服务器带来很多额外的压力。 Would really like your opinions on this aspect especially please.真的很想请您在这方面发表意见。

  • Tokens are set to expire in 2hrs regardless.无论如何,令牌都将在 2 小时内到期。 So if the user leaves themselves logged in, they will be automatically logged out after 2hrs of inactivity.因此,如果用户让自己保持登录状态,他们将在 2 小时不活动后自动注销。

  • User ID and Token are sent as part of the JSON body of every request (not in the headers).用户 ID 和令牌作为每个请求的JSON正文的一部分发送(不在标头中)。 Is this a cause for concern?这是一个令人担忧的原因吗?

  • At no point (other than sign up and login) is the users password transmitted or stored in localStorage or used by the React frontend.在任何时候(注册和登录除外),用户密码都不会传输或存储在localStorage或被React前端使用。 A matching user ID and Token is all that is required to validate the user after the initial authorization.在初始授权后验证用户只需要匹配的用户 ID 和令牌。

  • All connections are made via HTTPS .所有连接均通过HTTPS

Can you spot any glaring security risks in this approach?在这种方法中,您能发现任何明显的安全风险吗? Am I missing the elephant in the room here?我错过了房间里的大象吗?

Obviously the user ID and matching token is as good as supplying the users email and password on every request as far as auth goes, but I can't use PHP sessions or cookies* as the API is hosted on a different domain.显然,就身份验证而言,用户 ID 和匹配的令牌与在每个请求中提供用户电子邮件和密码一样好,但我不能使用 PHP 会话或 cookie*,因为 API 托管在不同的域上。 This is the best workaround I could come up with without having to go down the JWT or Oauth route.这是我能想出的最好的解决方法,而不必走JWTOauth路线。

How flawed it could be in regards to how I am checking and validating data on the API can't be practically addressed here, but assuming that it is all being done correctly is this method secure enough in principle?在我如何检查和验证 API 上的数据方面可能有多大缺陷在这里无法实际解决,但假设一切都正确完成,这种方法原则上是否足够安全?

I look forward to and thank you in advance for your opinions :)我期待并提前感谢您的意见:)

*without a ton of workarounds which would ultimately be superflous as this App can only be used with modern browsers which all support localStorage . *没有大量的变通方法,这些变通方法最终将是多余的,因为该应用程序只能与都支持localStorage现代浏览器一起使用。

In my opinion is not save to store the token in the local store,在我看来不是 save 将令牌存储在本地存储中,

As said in https://auth0.com/docs/security/store-tokenshttps://auth0.com/docs/security/store-tokens所述

Since Browser local storage (or session storage) is not secure.由于浏览器本地存储(或会话存储)不安全。 Any data stored there may be vulnerable to cross-site scripting.存储在那里的任何数据都可能容易受到跨站点脚本的攻击。 If an attacker steals a token, they can gain access to and make requests to your API.如果攻击者窃取了令牌,他们可以访问您的 API 并向其发出请求。 Treat tokens like credit card numbers or passwords: don't store them in local storage.将令牌视为信用卡号或密码:不要将它们存储在本地存储中。

A better option is to use cookies since they are managed by the browser.更好的选择是使用 cookie,因为它们由浏览器管理。

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

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