A more recent innovation is the JWT-JSON Web Token.
Here is the link to the specification: JWT - JSON Web Token
JWT is a method of using Hashed tokens using a Hashing method such as HMAC, which stands for Hash-based message authentication code. Since the token is hashed using a secret key, the server can determine if the token has been changed.
Here is an example of a method for creating a Hashed marker for JWT:
public String createTokenForUser(User user) { byte[] userBytes = toJSON(user); byte[] hash = createHmac(userBytes); final StringBuilder sb = new StringBuilder(170); sb.append(toBase64(userBytes)); sb.append(SEPARATOR); sb.append(toBase64(hash)); return sb.toString(); }
Here is an example of decoding a token to make sure it has not been changed:
public User parseUserFromToken(String token) { final String[] parts = token.split(SEPARATOR_SPLITTER); if (parts.length == 2 && parts[0].length() > 0 && parts[1].length() > 0) { try { final byte[] userBytes = fromBase64(parts[0]); final byte[] hash = fromBase64(parts[1]); boolean validHash = Arrays.equals(createHmac(userBytes), hash); if (validHash) { final User user = fromJSON(userBytes); if (new Date().getTime() < user.getExpires()) { return user; } } } catch (IllegalArgumentException e) {
Here is an article with a more complete example: Idle Authentication
anataliocs
source share