javascript - Encrypting, then storing access token in localStorage - Stack Overflow

My API returns an access token after a user signs in.All future requests must include this token in the

My API returns an access token after a user signs in.

All future requests must include this token in the Authentication header.

I'd like users to remain logged in even if they close and re-open the browser.

Can I safely store an encrypted version of this access token in localStorage, retrieve it, unencrypt it in my React client code and send it off to the API?

My API returns an access token after a user signs in.

All future requests must include this token in the Authentication header.

I'd like users to remain logged in even if they close and re-open the browser.

Can I safely store an encrypted version of this access token in localStorage, retrieve it, unencrypt it in my React client code and send it off to the API?

Share asked Mar 7, 2019 at 2:42 slindsey3000slindsey3000 4,3015 gold badges42 silver badges58 bronze badges 4
  • 1 You can, but if you control the API you may want to consider using cookie based storage and preventing access via CORS. This prevents XSS attacks from obtaining access to encrypted authentication data. – coreyward Commented Mar 7, 2019 at 3:00
  • 1 nccgroup.trust/us/about-us/newsroom-and-events/blog/2011/august/… – Victor Commented Mar 7, 2019 at 3:27
  • 1 security.stackexchange./questions/173620/… – Victor Commented Mar 7, 2019 at 3:27
  • 1 I would remend you to move away from local storage to keep sensitive information, it is not safe. At work, we have a bunch of single-page applications using cookies to keep the access token of the user, which is a much safer solution. You can read more about why we should be using cookies instead of local storage to keep sensitive information in this link: dev.to/rdegges/please-stop-using-local-storage-1i04 – Nícolas Iensen Commented Mar 7, 2019 at 6:06
Add a ment  | 

1 Answer 1

Reset to default 5

Sure, you could use something like this https://www.npmjs./package/aes-js or another library if you like at https://www.npmjs./search?q=keywords:encrypt.

I think the real question is why? The user can always see the unencrypted value in the network request of the dev tools. If the access token is tied to a username and password that the user owns why can't they see it? It's really just another way to write their username and password. If it is associated with an username that belongs to your app (such as your server uses that username to login to another server for all requests, not just for this one user) then you shouldn't pass it to the browser ever because it is easy to steal.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744861322a4597720.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信