简体   繁体   English

PHP REST api 的安全方式?

[英]Secure way for PHP REST api?

I am developing the PHP based REST api.我正在开发基于PHPREST api。 I have android app which will send some key parameters to server and the server will respond with the data.我有 android 应用程序,它将向服务器发送一些关键参数,服务器将响应数据。 In this case, the email is the key element to get all the relevant data.在这种情况下,email 是获取所有相关数据的关键要素。 If I want to make it secure, I can save password in sharedPreferences and send it at every request.如果我想让它安全,我可以将密码保存在sharedPreferences中并在每次请求时发送。 This might make communication secure, but I understand that sharedPreferences are not secure and putting confidential information in them is not recommended.这可能使通信安全,但我知道sharedPreferences不安全,不建议将机密信息放入其中。 Also, I cannot store a hash of password in sharedPreferences because I am using the password_hash() function in php api which requires password as plain text. Also, I cannot store a hash of password in sharedPreferences because I am using the password_hash() function in php api which requires password as plain text. So, i have to send request as a plain text password only.所以,我只需要以纯文本密码的形式发送请求。 What should I do to make it secure?我应该怎么做才能确保它的安全?

OAuth2 with client_credentials GrantType is the best option in my opinion.在我看来,带有client_credentials GrantType 的 OAuth2 是最好的选择。 For tokens I would use JWT (which is default in most PHP implementations) which gives you the ability to validate it stateless and makes your auth env more scalable.对于令牌,我将使用 JWT (这是大多数 PHP 实现中的默认设置),它使您能够验证它的无状态并使您的身份验证环境更具可扩展性。

You would the authorize your app using a fixed set of client credentials (client_id and optional client_secret) to authorize the user with its username and password against the auth server.您将使用一组固定的客户端凭据(client_id 和可选的 client_secret)授权您的应用程序,以针对身份验证服务器授权用户使用其用户名和密码。 The received token is then stored in sharedPreferences for further authorization.然后将接收到的令牌存储在 sharedPreferences 中以供进一步授权。

There's the normal way things like this are done and that is usually with some form of token authentication.像这样的事情有正常的完成方式,通常是使用某种形式的令牌认证。

However, you probably want to learn more about security except what you're doing is a toy app only you intend using.但是,您可能想了解有关安全性的更多信息,除非您正在做的只是您打算使用的玩具应用程序。 With that being said, PHP is very wonderful at handling sessions, so you can consider using a cookie jar in your app and take advantage of sessions.话虽如此,PHP 在处理会话方面非常出色,因此您可以考虑在您的应用程序中使用 cookie jar 并利用会话。 REST purists will probably hang me for that suggestion, but you know what? REST 纯粹主义者可能会因为这个建议而绞死我,但你知道吗? It works perfectly provided you're gonna be using your API for just your app.只要您将 API 仅用于您的应用程序,它就可以完美运行。

Another quick and easy solution is to do a normal email and password login from your app.另一个快速简便的解决方案是从您的应用程序执行正常的 email 和密码登录。 On success, return a randomly generated token to the user and also store in a table on your database.成功后,将随机生成的令牌返回给用户,并将其存储在数据库的表中。 This token is what you can store in sharedpref that will be sent along with every request.此令牌是您可以存储在 sharedpref 中的内容,它将与每个请求一起发送。 To make things more interesting, you can allow the token expire after say 10 minutes if it hasn't been used.为了让事情变得更有趣,如果令牌没有被使用,您可以让令牌在 10 分钟后过期。 On your app this will mean the user needs to login again, and generate a new token.在您的应用上,这意味着用户需要再次登录,并生成一个新令牌。

If you're going with the token generation model, you wanna make sure the token is not easily guessable.如果您要使用令牌生成 model,您需要确保令牌不容易被猜到。 I usually just use something like this for those kind of random alphanumeric strings:我通常只对那些随机的字母数字字符串使用这样的东西:

$randomLongString = hash('sha384', microtime(). uniqid(). bin2hex(random_bytes(10)));

For the expiry, you can record the timestamp the token table was accessed, store it in a column with the same token.对于到期,您可以记录访问令牌表的时间戳,将其存储在具有相同令牌的列中。 If the old timestamp + (expiry time) > timestamp now, then you know the token is expired.如果旧的时间戳+(到期时间)> 现在的时间戳,那么您知道令牌已过期。 Return a 401. If not update the old timestamp with the current timestamp, then return a 200.返回 401。如果不使用当前时间戳更新旧时间戳,则返回 200。

These are relatively simple but effective solutions that work for single server setups, and relies on the fact that the user is still the same user.这些是适用于单服务器设置的相对简单但有效的解决方案,并且依赖于用户仍然是同一用户这一事实。 Other things you could do is IP checking, device ID verification before you generate the token.您可以做的其他事情是 IP 检查,在生成令牌之前验证设备 ID。 And if any of those things change along the way, you quickly invalidate the token and provide a way for the user to prove they are who they claim they are.如果这些事情中的任何一个在此过程中发生变化,您可以快速使令牌失效,并为用户提供一种方式来证明他们是他们声称的身份。

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

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