Skip to content

Commit

Permalink
add redis transactions to token requests
Browse files Browse the repository at this point in the history
  • Loading branch information
w-ensink committed Dec 16, 2024
1 parent de67879 commit 3003d93
Showing 1 changed file with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package foundation.privacybydesign.sms.tokens;

import java.awt.Panel;
import java.util.List;

import org.slf4j.Logger;
Expand All @@ -8,6 +9,8 @@
import foundation.privacybydesign.sms.redis.Redis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.params.ScanParams;
import redis.clients.jedis.resps.ScanResult;

Expand All @@ -16,7 +19,7 @@
* Useful for when the sms issuer needs to be stateless.
*/
class RedisTokenRequestRepository implements TokenRequestRepository {
private static Logger logger = LoggerFactory.getLogger(RedisTokenRequestRepository.class);
private static Logger LOG = LoggerFactory.getLogger(RedisTokenRequestRepository.class);
private static String namespace = "request";
JedisSentinelPool pool;

Expand All @@ -28,9 +31,24 @@ class RedisTokenRequestRepository implements TokenRequestRepository {
public void store(String phone, TokenRequest request) {
final String key = Redis.createKey(namespace, phone);
try (var jedis = pool.getResource()) {
jedis.hset(key, "token", request.token);
jedis.hset(key, "tries", Integer.toString(request.tries));
jedis.hset(key, "created", String.valueOf(request.created));
jedis.watch(key);

Transaction transaction = jedis.multi();
transaction.hset(key, "token", request.token);
transaction.hset(key, "tries", Integer.toString(request.tries));
transaction.hset(key, "created", String.valueOf(request.created));

final List<Object> result = transaction.exec();

if (result == null) {
LOG.error("failed to set token: redis transaction exec() result is null");
return;
}
for (var r : result) {
if (r instanceof Exception) {
LOG.error("failed to set token: " + ((Exception) r).getMessage());
}
}
}
}

Expand Down Expand Up @@ -71,7 +89,7 @@ private void removeIfExpired(Jedis jedis, String key) {
jedis.del(key);
}
} catch (NumberFormatException e) {
logger.error("Failed to parse " + key + " creation into long: " + e.getMessage());
LOG.error("Failed to parse " + key + " creation into long: " + e.getMessage());
}
}
}
Expand All @@ -81,12 +99,35 @@ public TokenRequest retrieve(String phone) {
try (var jedis = pool.getResource()) {
final String key = Redis.createKey(namespace, phone);
try {
String token = jedis.hget(key, "token");
int tries = Integer.parseInt(jedis.hget(key, "tries"));
long created = Long.parseLong(jedis.hget(key, "created"));

jedis.watch(key);
Transaction transaction = jedis.multi();

final Response<String> tokenRes = transaction.hget(key, "token");
final Response<String> triesRes = transaction.hget(key, "tries");
final Response<String> createdRes = transaction.hget(key, "created");

final List<Object> execRes = transaction.exec();

if (execRes == null) {
LOG.error("error while getting token request from redis: exec result is null");
return null;
}

for (var r : execRes) {
if (r instanceof Exception) {
LOG.error("error while getting token request from redis: " + ((Exception) r).getMessage());
return null;
}
}

final String token = tokenRes.get();
final int tries = Integer.parseInt(triesRes.get());
final long created = Long.parseLong(createdRes.get());

return new TokenRequest(token, tries, created);
} catch (NumberFormatException e) {
logger.error("failed to parse for " + key + ": " + e.getMessage());
LOG.error("failed to parse for " + key + ": " + e.getMessage());
return null;
}
}
Expand Down

0 comments on commit 3003d93

Please sign in to comment.