Skip to content

Commit

Permalink
fixed a race condition in AuthenticationGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Duda committed Mar 2, 2021
1 parent a543b53 commit 5324e08
Showing 1 changed file with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.codingchili.core.security;

import io.vertx.core.Future;
import io.vertx.core.*;
import io.vertx.core.json.JsonObject;

import java.util.*;
Expand Down Expand Up @@ -115,38 +115,44 @@ public Future<Void> secrets() {
* @return callback
*/
public Future<Void> tokens() {
Future<Void> future = Future.future();
var all = new ArrayList<Future>();

configurations((settings, config, path, save) -> {
andPathMatchesKeyRegex(settings, path).forEach(dependency -> {
AtomicInteger latch = new AtomicInteger(dependency.getTokens().size());
configurations((settings, config, path, save) ->
andPathMatchesKeyRegex(settings, path)
.forEach(dependency ->
all.add(generateTokens(dependency, config, path)
.setHandler(result -> save.run()))));

return CompositeFuture.all(all).map(v -> null);
}

dependency.getTokens().forEach((key, identifier) -> {
Optional<TokenFactory> factory = getFactory(identifier);
private Future<Void> generateTokens(AuthenticationDependency dependant, JsonObject config, String path) {
var latch = new AtomicInteger(dependant.getTokens().size());
var future = (latch.get() > 0) ? Promise.<Void>promise().future() : Future.<Void>succeededFuture();

if (factory.isPresent()) {
Token token = new Token(getIdentity(config));
dependant.getTokens().forEach((key, identifier) -> {
Optional<TokenFactory> factory = getFactory(identifier);

factory.get().hmac(token).setHandler(done -> {
if (done.succeeded()) {
logger.log(CoreStrings.generatedToken(identifier.getService(), key, path));
if (factory.isPresent()) {
Token token = new Token(getIdentity(config));

config.put(key, json(token));
save.run();
factory.get().hmac(token).setHandler(done -> {
if (done.succeeded()) {
logger.log(CoreStrings.generatedToken(identifier.getService(), key, path));

if (latch.decrementAndGet() == 0) {
future.tryComplete();
}
} else {
future.fail(done.cause());
}
});
config.put(key, json(token));

if (latch.decrementAndGet() == 0) {
future.complete();
}
} else {
logger.onSecurityDependencyMissing(identifier.getService(), identifier.getSecret());
throw new SecurityMissingDependencyException(identifier.getService(), identifier.getSecret());
future.fail(done.cause());
}
});
});
} else {
logger.onSecurityDependencyMissing(identifier.getService(), identifier.getSecret());
throw new SecurityMissingDependencyException(identifier.getService(), identifier.getSecret());
}
});
return future;
}
Expand Down

0 comments on commit 5324e08

Please sign in to comment.