public String generateToken(String user, String userIp) { BasicCipher cipher = AESCipherFactory.getInstance(SUPER_SECURE_KEY); JsonObject jsonToken = new JsonObject(); jsonToken.addProperty(LOGIN_PROPERTY, user); jsonToken.addProperty(IP_PROPERTY, userIp); // TODO Colocar mais coisas no token return cipher.cipher(jsonToken.toString()); }
public String getAuthenticatedUser(String token, String userIp) { BasicCipher cipher = AESCipherFactory.getInstance(SUPER_SECURE_KEY); String decriptedToken; try { decriptedToken = cipher.decipher(token); JsonParser parser = new JsonParser(); JsonObject jsonToken = parser.parse(decriptedToken).getAsJsonObject(); // TODO Mais coisas no token String tokenLogin = jsonToken.get(LOGIN_PROPERTY).getAsString(); String tokenIp = jsonToken.get(IP_PROPERTY).getAsString(); String user = LoginService.USERS.get(tokenLogin); if (user != null && userIp.equals(tokenIp)) { return tokenLogin; } else { return null; } } catch (RuntimeException e) { return null; } }