4
My application consists of an API nodejs on backend but I’m also creating the reference implementation of a Javascript client, which is a SPAen made with Backbone.
First: the API accepts only HTTPS requests in the case of a request HTTP reach the server it ignores completely and optionalment can invalidate the password used in that request unsafe.
My server does not save status (no session/cookies) and I use only basic HTTP authenticationen, where I provide two ways to authenticate a request:
1- Send credentials to header: Authentication: base64('Basic ' + nomeDeUsuario:senha)
2- Send a request authenticated with method 1 to GET /usuarios/atual
that returns a token, which is an encrypted string* containing: nomeDeUsuario + '|' + dataDeExpiracaoDoToken
. The customer then sends the header Authentication: 'Token ' + base64(nomeDeUsuario:token)
.
*Encryption made with Openssl’s aes-256-Ctr algorithm. The private key is the user’s password hash.
The method 1 can be used for server-server communication, so it is not suitable for the Javascript client, because for all requests the user would have to enter his credentials, unless such credentials were stored in the browser’s memory, what I don’t know is safe enough. Also, store credentials in local Storage would keep the user logged in indefinitely.
For method 2 The Javascript client only sends an authenticated request with basic authentication and immediately discards this sensitive login information, storing only the token in the Storage location. After a certain time this token will expire and a revalidation will be required, almost emulating a session on the server.
On the server side I check the authenticity of a request made with method 2 simply by getting the password hash of the user and trying to decrypt the token, then I check tokenDescriptografado.split('|')[0] === username
.
Is this a safe approach? Is there a point I’m not taking into account? Given this approach, what kind of attacks would I be subject to?
*This is a crosspost of a question I asked in Information Security
those two questions somehow relate to my
– Renato Gama
You have already considered SSL with Oauth2?
– Bruno Augusto
@Brunoaugusto I will now read about Oauth2. In this case it would be as if I myself were the identity provider!
– Renato Gama