I'm trying to build my own PHP based REST Api for my Android app and I'm a litte bit confused by all the different user authentication stuff one can find on the internet. So I want to present my security considerations and would be glad about some feedback.
First, I want to make clear that I don't want to build a public API where 3rd parties could register for an API key, instead I want only be able for users of my app to do all the register/login/sending requests stuff. So my plans are as follows:
When a user wants to register, my app first exchanges generated keys with my server using the Diffie-Hellman method. For this, my server and my app store information about the same prime number and the same base (often called p and g). My app then generates a secret and creates a public key out of p, g and the secret. The public key is send to the server, which answers with sending back a user id in the Java UUID format and the server's public key based on the server's secret, p, and g. At the end, both parties are able to generate a common secret. The whole transaction is done using TSL/SSL and can be seen here. Instead of the whole numeric, pretty long secret, a SHA-512 hash is stored in the database and the app.
After exchanging the key, I can now send the user's account information (userid, password and other stuff) to the server using TSL/SSL. The app's secret is used for HMAC making sure that the app instance (the user) is authorized to send me this stuff. I can check this as I stored the SHA-512 hashed secret and the userid in the database. The password needs to be transmitted to be able to re-login if the user logged out or for wants to change his password. The password is hashed follwowing these instructions. After logging in, a random login token is returned to the user representing his logged in state. This login token is also stored in the database.
Instead of sending the user's credentials with every request, the login token is used as login credentials and the secret generated in the first step is used to do HMAC. So if I get a request, I can first check if the user is authorized to sent me a request by doing the message authentication with HMAC and then I can check if the user is logged in comparing the transmitted login token with the one stored in the database. This transaction may also be done with TSL/SSL but doesn't need to.
Is this a valid concept or does it lack of some crucial points, contains misunderstandings or is there any other kind of problem with it?