Skip to content

Commit

Permalink
Corrected SqlAuthToken constructor to use seconds since unix epoch; A…
Browse files Browse the repository at this point in the history
…dded missing comments
  • Loading branch information
tkyc committed Nov 3, 2022
1 parent 090496a commit 226d20c
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,18 @@ CallableStatement prepareCall(String sql, int nType, int nConcur, int nHold,
/**
* Deprecated. Time-to-live is no longer supported for the cached Managed Identity tokens.
* This method will always return 0 and is for backwards compatibility only.
*
* @return Method will always return 0.
*/
@Deprecated
int getMsiTokenCacheTtl();

/**
* Deprecated. Time-to-live is no longer supported for the cached Managed Identity tokens.
* This method is a no-op for backwards compatibility only.
*
* @param timeToLive
* - Time-to-live is no longer supported.
*/
@Deprecated
void setMsiTokenCacheTtl(int timeToLive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1216,18 +1216,34 @@ public interface ISQLServerDataSource extends javax.sql.CommonDataSource {
/**
* Deprecated. Time-to-live is no longer supported for the cached Managed Identity tokens.
* This method is a no-op for backwards compatibility only.
*
* @param timeToLive
* - Time-to-live is no longer supported.
*/
@Deprecated
void setMsiTokenCacheTtl(int timeToLive);

/**
* Deprecated. Time-to-live is no longer supported for the cached Managed Identity tokens.
* This method will always return 0 and is for backwards compatibility only.
*
* @return Method will always return 0.
*/
@Deprecated
int getMsiTokenCacheTtl();

/**
* Sets the {@link SQLServerAccessTokenCallback} delegate.
*
* @param accessTokenCallback
* - Access token callback delegate.
*/
void setAccessTokenCallback(SQLServerAccessTokenCallback accessTokenCallback);

/**
* Returns a {@link SQLServerAccessTokenCallback}, the access token callback delegate.
*
* @return Access token callback delegate.
*/
SQLServerAccessTokenCallback getAccessTokenCallback();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface SQLServerAccessTokenCallback {
* - Security token service URL.
* @param spn
* - Service principal name.
*
* @return Returns a {@link SqlAuthenticationToken}.
*/
SqlAuthenticationToken getAccessToken(String stsurl, String spn);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,7 @@
import java.text.MessageFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -5649,7 +5640,9 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw
byte[] accessTokenFromDLL = dllInfo.accessTokenBytes;

String accessToken = new String(accessTokenFromDLL, UTF_16LE);
fedAuthToken = new SqlAuthenticationToken(accessToken, dllInfo.expiresIn);
Date now = new Date();
now.setTime(now.getTime() + (dllInfo.expiresIn * 1000));
fedAuthToken = new SqlAuthenticationToken(accessToken, now);

// Break out of the retry loop in successful case.
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1224,12 +1224,23 @@ public int getMsiTokenCacheTtl() {
return 0;
}

/**
* Sets the {@link SQLServerAccessTokenCallback} delegate.
*
* @param accessTokenCallback
* - Access token callback delegate.
*/
@Override
public void setAccessTokenCallback(SQLServerAccessTokenCallback accessTokenCallback) {
setObjectProperty(connectionProps, SQLServerDriverObjectProperty.ACCESS_TOKEN_CALLBACK.toString(),
accessTokenCallback);
}

/**
* Returns a {@link SQLServerAccessTokenCallback}, the access token callback delegate.
*
* @return Access token callback delegate.
*/
@Override
public SQLServerAccessTokenCallback getAccessTokenCallback() {
return (SQLServerAccessTokenCallback) getObjectProperty(connectionProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public class SQLServerPooledConnection implements PooledConnection, Serializable

// Unique id generator for each PooledConnection instance (used for logging).
static private final AtomicInteger basePooledConnectionID = new AtomicInteger(0);

/** Reentrant lock. **/
private final Lock lock = new ReentrantLock();

/** Connection event listener lock. **/
private final Lock listenersLock = new ReentrantLock();

SQLServerPooledConnection(SQLServerDataSource ds, String user, String password) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,8 @@ public final String getResponseBuffering() throws SQLServerException {

/** This is a per-statement store provider. */
Map<String, SQLServerColumnEncryptionKeyStoreProvider> statementColumnEncryptionKeyStoreProviders = new HashMap<>();

/** Reentrant lock. **/
private final Lock lock = new ReentrantLock();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public final class SQLServerXAConnection extends SQLServerPooledConnection imple

/** logger */
private Logger xaLogger;

/** Reentrant lock **/
private final Lock lock = new ReentrantLock();

SQLServerXAConnection(SQLServerDataSource ds, String user, String pwd) throws java.sql.SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,56 @@
*/
public class SqlAuthenticationToken implements Serializable {

/**
* Always update serialVersionUID when prompted
*/
/** Always update serialVersionUID when prompted **/
private static final long serialVersionUID = -1343105491285383937L;

/** The token expiration date. **/
private final Date expiresOn;

/** The access token string. **/
private final String accessToken;

public SqlAuthenticationToken(String accessToken, long expiresIn) {
this.accessToken = accessToken;

Date now = new Date();
now.setTime(now.getTime() + (expiresIn * 1000));
this.expiresOn = now;
/**
* Contructs a SqlAuthentication token.
*
* @param accessToken
* - The access token string.
* @param expiresOn
* - The expiration date in seconds since the unix epoch.
*/
public SqlAuthenticationToken(String accessToken, long expiresOn) {
this.accessToken = accessToken;
this.expiresOn = new Date(expiresOn);
}

/**
* Contructs a SqlAuthentication token.
*
* @param accessToken
* - The access token string.
* @param expiresOn
* - The expiration date.
*/
public SqlAuthenticationToken(String accessToken, Date expiresOn) {
this.accessToken = accessToken;
this.expiresOn = expiresOn;
}

/**
* Returns the expiration date of the token.
*
* @return The token expiration date.
*/
public Date getExpiresOn() {
return expiresOn;
}

/**
* Returns the access token string.
*
* @return The access token.
*/
public String getAccessToken() {
return accessToken;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ public SqlAuthenticationToken getAccessToken(String spn, String stsurl) {

try {
ExecutorService executorService = Executors.newSingleThreadExecutor();
IClientCredential credential = ClientCredentialFactory.createFromSecret(accessTokenSecret);
IClientCredential credential = ClientCredentialFactory.createFromSecret(applicationKey);
ConfidentialClientApplication clientApplication = ConfidentialClientApplication
.builder(accessTokenClientId, credential).executorService(executorService)
.builder(applicationClientID, credential).executorService(executorService)
.authority(stsurl).build();
CompletableFuture<IAuthenticationResult> future = clientApplication
.acquireToken(ClientCredentialParameters.builder(scopes).build());
Expand Down

0 comments on commit 226d20c

Please sign in to comment.