I'm working on client implementing the "auth code" OAuth2 flow. I'm trying to find the best way to generate (and check back) the "state" parameter to prevent CSRF attacks to the login flow. I cannot store the state value server-side and attach it to a user session ID as I do not want session affinity. I'm considering the following:
- Generate a proper NONCE and set it as session cookie between the end user browser and the client application. Save a timestamp together with that NONCE.
- Client application side, calculate the "state" as a hash (SHA2) of the nonce + timestamp + a secret salt. I'm considering the OAuth client secret as the secret salt, since it is the only secret distributed to all client application instances.
- When receiving the auth code at the end of the flow, the client app checks the nonce from the cookie with the timestamp, controls timeout, and recalculates the hash using the client secret, to compare this to the state parameter. Code is accepted only if the state match the hash and there is no timeout.
- Does it sounds like a proper way to generate the "state"?
- Am I taking risk of disclosure using my client secret as a salt to the hash operation?
Thanks for your help.