title: Authorizing a computer to access a web application
body: <p>I have a web application, and am tasked with adding secure sign-on to bolster security, akin to what Google has added to Google accounts. </p>
  
  <p><strong>Use Case</strong></p>
  
  <p>Essentially, when a user logs in, we want to detect if the user has previously authorized this computer. If the computer has not been authorized, the user is sent a one-time password (via email, SMS, or phone call) that they must enter, where the user may choose to remember this computer. In the web application, we will track authorized devices, allowing users to see when/where they logged in from that device last, and deauthorize any devices if they so choose.</p>
  
  <p>We require a solution that is very light touch (meaning, requiring no client-side software installation), and works with Safari, Chrome, Firefox, and IE 7+ (unfortunately). We will offer x509 security, which provides adequate security, but we still need a solution for customers that can't or won't use x509.</p>
  
  <p>My intention is to store authorization information using cookies (or, potentially, using local storage, degrading to flash cookies, and then normal cookies).</p>
  
  <p><strong>At First Blush</strong></p>
  
  <p><img src="https://i.stack.imgur.com/bqGhw.png" alt="Initial secure sign-on sequence diagram">
  Track two separate values (local data or cookies): a hash representing a secure sign-on token, as well as a device token. Both values are driven (and recorded) by the web application, and dictated to the client. The SSO token is dependent on the device as well as a sequence number. This effectively allows devices to be deauthorized (all SSO tokens become invalid) and mitigates replay (not effectively, though, which is why I'm asking this question) through the use of a sequence number, and uses a nonce.</p>
  
  <p><strong>Problem</strong></p>
  
  <p>With this solution, it's possible for someone to just copy the SSO and device tokens and use in another request. While the sequence number will help me detect such an abuse and thus deauthorize the device, the detection and response can only happen after the valid device and malicious request both attempt access, which is ample time for damage to be done.</p>
  
  <p>I feel like using HMAC would be better. Track the device, the sequence, create a nonce, timestamp, and hash with a private key, then send the hash plus those values as plain text. Server does the same (in addition to validating the device and sequence) and compares. That seems much easier, and much more reliable.... assuming we can securely negotiate, exchange, and store private keys.</p>
  
  <p><strong>Question</strong></p>
  
  <p>So then, how can I securely negotiate a private key for authorized device, and then securely store that key? Is it more possible, at least, if I settle for storing the private key using local storage or flash cookies and just say it's "good enough"? Or, is there something I can do to my original draft to mitigate the vulnerability I describe?</p>