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.

My question:

  1. Does it sounds like a proper way to generate the "state"?
  2. Am I taking risk of disclosure using my client secret as a salt to the hash operation?

Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.