Skip to content

Commit

Permalink
✨ Add authDb to the MongoAccount to track the db used during authenti…
Browse files Browse the repository at this point in the history
…cation
  • Loading branch information
ujibang committed Nov 13, 2024
1 parent e7a9b9e commit 4b2a71e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;

import org.bson.BsonDocument;
import org.bson.BsonString;
import org.mindrot.jbcrypt.BCrypt;
import org.restheart.cache.Cache;
import org.restheart.cache.CacheFactory;
Expand Down Expand Up @@ -146,7 +147,7 @@ public void init() {
if (cacheEnabled) {
final int cacheSize = arg(config, "cache-size");
final int cacheTTL = arg(config, "cache-ttl");
this.USERS_CACHE = CacheFactory.createLocalLoadingCache(cacheSize, this.cacheExpirePolicy, cacheTTL, key -> findAccount(accountIdTrasformer(key)));
this.USERS_CACHE = CacheFactory.createLocalLoadingCache(cacheSize, this.cacheExpirePolicy, cacheTTL, key -> findAccount(key));
}

try {
Expand Down Expand Up @@ -354,7 +355,7 @@ private MongoRealmAccount getAccount(final String usersDb, final String id) {
final var cacheKey = new CacheKey(id, usersDb);

if (USERS_CACHE == null) {
return findAccount(this.accountIdTrasformer(cacheKey));
return findAccount(cacheKey);
} else {
final var _account = USERS_CACHE.getLoading(cacheKey);

Expand All @@ -366,18 +367,6 @@ private MongoRealmAccount getAccount(final String usersDb, final String id) {
}
}

/**
* Override this method to trasform the account id. By default it returns
* the id without any transformation. For example, it could be overridden to
* force the id to be lowercase.
*
* @param key the cache key
* @return the trasformed cache key(default is identity)
*/
protected CacheKey accountIdTrasformer(final CacheKey key) {
return key;
}

/**
* if client authenticates passing the real credentials, update the account
* in the auth-token cache, otherwise the client authenticating with the
Expand Down Expand Up @@ -448,7 +437,7 @@ public boolean checkUserCollection() throws IllegalStateException {
return true;
}

public long countAccounts() {
private long countAccounts() {
try {
return mclient.getDatabase(this.getUsersDb()).getCollection(this.getUsersCollection()).estimatedDocumentCount();
} catch (final Throwable t) {
Expand All @@ -457,7 +446,7 @@ public long countAccounts() {
}
}

public void createDefaultAccount() {
private void createDefaultAccount() {
if (this.mclient == null) {
LOGGER.error("Cannot find account: mongo service is not enabled.");
return;
Expand All @@ -472,7 +461,7 @@ public void createDefaultAccount() {
}
}

public MongoRealmAccount findAccount(final CacheKey key) {
private MongoRealmAccount findAccount(final CacheKey key) {
final var coll = mclient.getDatabase(key.db()).getCollection(this.getUsersCollection()).withDocumentClass(BsonDocument.class);

BsonDocument _account;
Expand Down Expand Up @@ -542,9 +531,10 @@ public MongoRealmAccount findAccount(final CacheKey key) {
}
});

return new MongoRealmAccount(key.db(), key.id(), _password.getAsJsonPrimitive().getAsString().toCharArray(), roles,
// used this because password has been removed from account
BsonDocument.parse(account.toString()));
var properties = BsonDocument.parse(account.toString()); // used this because password has been removed from account
properties.put("authDb", new BsonString(key.db()));

return new MongoRealmAccount(key.db(), key.id(), _password.getAsJsonPrimitive().getAsString().toCharArray(), roles, properties);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private Token newToken(Account account, Date expires) {
.withSubject(account.getPrincipal().getName())
.withExpiresAt(expires)
.withIssuer(issuer)
.withArrayClaim("roles", (String[]) account.getRoles().toArray());
.withArrayClaim("roles", account.getRoles().toArray(new String[account.getRoles().size()]));

Builder[] builder = { _builder };

Expand All @@ -246,7 +246,7 @@ private Token newToken(Account account, Date expires) {

var raw = builder[0].sign(algo);

return new Token(raw.toCharArray(), expires, (String[]) account.getRoles().toArray(), properties);
return new Token(raw.toCharArray(), expires, account.getRoles().toArray(new String[account.getRoles().size()]), properties);
}


Expand Down

0 comments on commit 4b2a71e

Please sign in to comment.