Instead of a server issuing randomly generated session strings as service access keys (aka "authentication tokens") to a client, inserting them into database to maintain a set of user-key relations, for subsequent validation on every service request, may a server not do the following instead to achieve comparable or better security in the thus distributed system:
- Take the user's primary key (aka "user identifier")
- Take a chosen key expiration timestamp, and, optionally, the IP address of the client, and whatever else may be worth embedding in the key
- Take a secret string as the key verification signature
- Serialize all of the above
- Encrypt the result, symmetrically, with a key only known to the server, or asymmetrically using server's chosen private key
The session key that is the result of the steps above, is then shared with the client, e.g. with a cookie. With a stateless protocol like HTTP, where every request has to prove client authentication using a session key, the procedure described above is done in reverse by the server, with the goal of recovering an authenticated user identifier.
Everything else being equal, a successful attack on the underlying cryptography would be required for an adversary to be able to forge session keys and thus compromise the security of this system. With "everything else" I refer to weaknesses like the adversary obtaining user's session key from the client somehow with the intention to use it themselves.
The advantages of the approach, that I see, are:
- No need to maintain a valid user-key relation set anywhere
- Expiration value embedded in each key ensures that keys cannot be used beyond a certain point in time, by anyone.
- Decryption-based verification by the server may be less costly than database access.
The disadvantages may be:
- Invalidating keys by the server may be considered problematic:
- Even if a key securely embeds e.g. an IP address, where the server may use the latter to validate the key by comparing the embedded address to the address behind the service request, an IP address may be forged
- If key revocation problem is solved by the server maintaining a set of revoked keys, one may ask how this is better than maintaining a set of user-key relations in the first place?
I consider this question closed as it may not ever have a single acceptable answer and does not hence fit the SO guidelines, but since it has remained open and has gotten useful answers, I won't delete it, at least.