Skip to content
Snippets Groups Projects
Commit ba4c85a2 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

[fix] Authentication service - fix token & challenge management

parent dbd61160
No related branches found
No related tags found
No related merge requests found
......@@ -24,8 +24,7 @@ package org.duniter.elasticsearch.security.challenge;
import org.duniter.core.util.Preconditions;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.Cache;
import org.duniter.core.util.ObjectUtils;
import org.duniter.core.util.StringUtils;
import org.elasticsearch.common.inject.Inject;
......@@ -45,19 +44,19 @@ public class ChallengeMessageStore {
private String prefix;
private long validityDurationInSeconds;
private LoadingCache<String, String> chalengeMessageCache;
private Cache<String, String> store;
@Inject
public ChallengeMessageStore(Settings settings) {
this.prefix = settings.get("duniter4j.auth.challenge.prefix", "duniter4j-challenge-");
this.validityDurationInSeconds = settings.getAsInt("duniter4j.auth.challengeValidityDuration", 10);
this.chalengeMessageCache = initGeneratedMessageCache();
this.store = initGeneratedMessageCache();
}
public boolean validateChallenge(String challenge) {
Preconditions.checkArgument(StringUtils.isNotBlank(challenge));
String storedChallenge = chalengeMessageCache.getIfPresent(challenge);
String storedChallenge = store.getIfPresent(challenge);
// if no value in cache => maybe challenge expired
return ObjectUtils.equals(storedChallenge, challenge);
......@@ -65,8 +64,8 @@ public class ChallengeMessageStore {
public String createNewChallenge() {
String challenge = newChallenge();
chalengeMessageCache.put(challenge, challenge);
return newChallenge();
store.put(challenge, challenge);
return challenge;
}
/* -- internal methods -- */
......@@ -75,16 +74,9 @@ public class ChallengeMessageStore {
return String.valueOf(prefix + System.currentTimeMillis() * System.currentTimeMillis());
}
protected LoadingCache<String, String> initGeneratedMessageCache() {
protected Cache<String, String> initGeneratedMessageCache() {
return CacheBuilder.newBuilder()
.expireAfterWrite(validityDurationInSeconds, TimeUnit.SECONDS)
.build(new CacheLoader<String, String>() {
@Override
public String load(String challenge) throws Exception {
// not used. Filled manually
return null;
}
});
.build();
}
}
......@@ -64,7 +64,7 @@ public class SecurityTokenStore {
public String createNewToken(String challenge, String signature, String pubkey) {
String token = newToken(challenge, signature, pubkey);
tokenCache.put(challenge, challenge);
tokenCache.put(token, token);
return token;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment