in wrapper/src/main/java/software/amazon/jdbc/plugin/federatedauth/OktaAuthPlugin.java [147:199]
private Connection connectInternal(final HostSpec hostSpec, final Properties props,
final JdbcCallable<Connection, SQLException> connectFunc) throws SQLException {
this.samlUtils.checkIdpCredentialsWithFallback(IDP_USERNAME, IDP_PASSWORD, props);
final String host = IamAuthUtils.getIamHost(IAM_HOST.getString(props), hostSpec);
final int port = IamAuthUtils.getIamPort(
IAM_DEFAULT_PORT.getInteger(props),
hostSpec,
this.pluginService.getDialect().getDefaultPort());
final Region region = regionUtils.getRegion(host, props, IAM_REGION.name);
if (region == null) {
throw new SQLException(
Messages.get("OktaAuthPlugin.unableToDetermineRegion", new Object[]{ IAM_REGION.name }));
}
final String cacheKey = IamAuthUtils.getCacheKey(
DB_USER.getString(props),
host,
port,
region);
final TokenInfo tokenInfo = OktaAuthCacheHolder.tokenCache.get(cacheKey);
final boolean isCachedToken = tokenInfo != null && !tokenInfo.isExpired();
if (isCachedToken) {
LOGGER.finest(
() -> Messages.get(
"AuthenticationToken.useCachedToken",
new Object[] {tokenInfo.getToken()}));
PropertyDefinition.PASSWORD.set(props, tokenInfo.getToken());
} else {
updateAuthenticationToken(hostSpec, props, region, cacheKey, host);
}
PropertyDefinition.USER.set(props, DB_USER.getString(props));
try {
return connectFunc.call();
} catch (final SQLException exception) {
updateAuthenticationToken(hostSpec, props, region, cacheKey, host);
return connectFunc.call();
} catch (final Exception exception) {
LOGGER.warning(
() -> Messages.get(
"SamlAuthPlugin.unhandledException",
new Object[] {exception}));
throw new SQLException(exception);
}
}