Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would go with this:</p> <ol> <li>user initiates action, javascript asks <strong>Server 1</strong> (ajax) for request for file on Server 2</li> <li>Server 1 creates URL using <a href="http://php.net/manual/en/function.hash-hmac.php" rel="nofollow noreferrer">hash_hmac</a> with data: file, user ID, user secret</li> <li>when clicking that URL (server2.com/?file=FILE&amp;user_id=ID&amp;hash=SHA_1_HASH) <strong>server 2</strong> asks server 1 for validation (sends file, user_id and hash)</li> <li>server 1 does the validation, sends response to server 2</li> <li>server 2 pushes file or sends 403 HTTP response</li> </ol> <p>This way, server 2 only needs to consume API of server 1, server 1 has all the logic. </p> <p>Pseudocode for hash and url creation:</p> <pre><code>// getHash($userId, $file) method $user = getUser($userId); $hash = hash_hmac('sha1', $userId . $file, $user-&gt;getSecret()); // getUrl($userId, $file) method return sprintf('http://server2.com/get-file?file=%1&amp;user_id=%2&amp;hash=%3', $userId, $file, $security-&gt;getHash($userId, $file) ); </code></pre> <p>Pseudocode for validation:</p> <pre><code>$hash = $security-&gt;getHash($_GET['id'], $_GET['file']); if ($hash === $_GET['hash']) { // All is good } </code></pre> <p>Edit: <code>getHash()</code> method accepts user ID and file (ID or string, what ever suits your needs). With that data, it produces a hash, using <code>hash_hmac</code> method. For the <code>secret</code> parameter of hash_hmac function, users "secret key" is used. That key would be stored together with users data in the db table. It would be generated with mt_rand or even something stronger as reading /dev/random or using something like <a href="https://stackoverflow.com/a/16478556/691850">https://stackoverflow.com/a/16478556/691850</a>.</p> <p>A word of advice, use <a href="https://tn123.org/mod_xsendfile/" rel="nofollow noreferrer">mod_xsendfile</a> on server 2 (if it is Apache) to push files.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload