-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
drop use of authxmanager and authenticatedconnection from core
- Loading branch information
Showing
6 changed files
with
392 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 86 additions & 9 deletions
95
src/main/java/redis/clients/jedis/authentication/JedisAuthXManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,103 @@ | ||
package redis.clients.jedis.authentication; | ||
|
||
import redis.clients.authentication.core.AuthXManager; | ||
import java.lang.ref.WeakReference; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.function.Supplier; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import redis.clients.authentication.core.Token; | ||
import redis.clients.authentication.core.TokenAuthConfig; | ||
import redis.clients.authentication.core.TokenListener; | ||
import redis.clients.authentication.core.TokenManager; | ||
import redis.clients.jedis.Connection; | ||
import redis.clients.jedis.RedisCredentials; | ||
|
||
public class JedisAuthXManager implements Supplier<RedisCredentials> { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(JedisAuthXManager.class); | ||
|
||
public class JedisAuthXManager extends AuthXManager { | ||
private TokenListener listener; | ||
private TokenManager tokenManager; | ||
private List<WeakReference<Connection>> connections = Collections | ||
.synchronizedList(new ArrayList<>()); | ||
private Token currentToken; | ||
private AuthenticationListener listener; | ||
|
||
public interface AuthenticationListener { | ||
public void onAuthenticate(Token token); | ||
} | ||
|
||
public JedisAuthXManager(TokenManager tokenManager) { | ||
super(tokenManager); | ||
this.tokenManager = tokenManager; | ||
} | ||
|
||
public void setListener(TokenListener listener) { | ||
this.listener = listener; | ||
public JedisAuthXManager(TokenAuthConfig tokenAuthConfig) { | ||
this(new TokenManager(tokenAuthConfig.getIdentityProviderConfig().getProvider(), | ||
tokenAuthConfig.getTokenManagerConfig())); | ||
} | ||
|
||
public void start(boolean blockForInitialToken) | ||
throws InterruptedException, ExecutionException, TimeoutException { | ||
|
||
tokenManager.start(new TokenListener() { | ||
@Override | ||
public void onTokenRenewed(Token token) { | ||
currentToken = token; | ||
authenticateConnections(token); | ||
} | ||
|
||
@Override | ||
public void onError(Exception reason) { | ||
JedisAuthXManager.this.onError(reason); | ||
} | ||
}, blockForInitialToken); | ||
} | ||
|
||
@Override | ||
public void authenticateConnections(Token token) { | ||
super.authenticateConnections(token); | ||
RedisCredentials credentialsFromToken = new TokenCredentials(token); | ||
for (WeakReference<Connection> connectionRef : connections) { | ||
Connection connection = connectionRef.get(); | ||
if (connection != null) { | ||
try { | ||
connection.setCredentials(credentialsFromToken); | ||
} catch (Exception e) { | ||
log.error("Failed to authenticate connection!", e); | ||
} | ||
} else { | ||
connections.remove(connectionRef); | ||
} | ||
} | ||
if (listener != null) { | ||
listener.onTokenRenewed(token); | ||
listener.onAuthenticate(token); | ||
} | ||
} | ||
|
||
public void onError(Exception reason) { | ||
throw new JedisAuthenticationException( | ||
"Token request/renewal failed with message:" + reason.getMessage(), reason); | ||
} | ||
|
||
public Connection addConnection(Connection connection) { | ||
connections.add(new WeakReference<>(connection)); | ||
return connection; | ||
} | ||
|
||
public void stop() { | ||
tokenManager.stop(); | ||
} | ||
|
||
public void setListener(AuthenticationListener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
@Override | ||
public RedisCredentials get() { | ||
return new TokenCredentials(this.currentToken); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/redis/clients/jedis/authentication/JedisAuthenticationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package redis.clients.jedis.authentication; | ||
|
||
public class JedisAuthenticationException extends RuntimeException { | ||
|
||
public JedisAuthenticationException(String message) { | ||
super(message); | ||
} | ||
|
||
public JedisAuthenticationException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
Oops, something went wrong.