From e6997015616beecfb1d1cf325c1b0de145d97349 Mon Sep 17 00:00:00 2001 From: Kun Huang Date: Wed, 22 May 2024 21:57:58 +0800 Subject: [PATCH 1/3] Update SqlAuthenticationToken.java --- .../com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java b/src/main/java/com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java index c57cd2268..5eec7ca03 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java @@ -33,7 +33,7 @@ public class SqlAuthenticationToken implements Serializable { */ public SqlAuthenticationToken(String accessToken, long expiresOn) { this.accessToken = accessToken; - this.expiresOn = new Date(expiresOn); + this.expiresOn = new Date(expiresOn * 1000); } /** From 85791a66f20d72dc1f59cffe51246af2f98abbdd Mon Sep 17 00:00:00 2001 From: Terry Chow Date: Wed, 22 May 2024 12:05:49 -0700 Subject: [PATCH 2/3] Pass in milliseconds for token object --- .../microsoft/sqlserver/jdbc/SQLServerSecurityUtility.java | 4 ++-- .../com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSecurityUtility.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSecurityUtility.java index 734bcf487..b49abe9bc 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSecurityUtility.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSecurityUtility.java @@ -387,7 +387,7 @@ static SqlAuthenticationToken getManagedIdentityCredAuthToken(String resource, } else { AccessToken accessToken = accessTokenOptional.get(); sqlFedAuthToken = new SqlAuthenticationToken(accessToken.getToken(), - accessToken.getExpiresAt().toEpochSecond()); + accessToken.getExpiresAt().toInstant().toEpochMilli()); } if (logger.isLoggable(java.util.logging.Level.FINEST)) { @@ -471,7 +471,7 @@ static SqlAuthenticationToken getDefaultAzureCredAuthToken(String resource, } else { AccessToken accessToken = accessTokenOptional.get(); sqlFedAuthToken = new SqlAuthenticationToken(accessToken.getToken(), - accessToken.getExpiresAt().toEpochSecond()); + accessToken.getExpiresAt().toInstant().toEpochMilli()); } return sqlFedAuthToken; diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java b/src/main/java/com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java index 5eec7ca03..71f2e2d95 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java @@ -29,11 +29,11 @@ public class SqlAuthenticationToken implements Serializable { * @param accessToken * The access token string. * @param expiresOn - * The expiration date in seconds since the unix epoch. + * The expiration date in milliseconds since the unix epoch. */ public SqlAuthenticationToken(String accessToken, long expiresOn) { this.accessToken = accessToken; - this.expiresOn = new Date(expiresOn * 1000); + this.expiresOn = new Date(expiresOn); } /** From 6b8d30802e7764f92c16605f7d4677826722a971 Mon Sep 17 00:00:00 2001 From: Jeff Wasty Date: Wed, 22 May 2024 12:08:33 -0700 Subject: [PATCH 3/3] Minor cleanup --- .../com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java | 4 ++-- src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java b/src/main/java/com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java index 71f2e2d95..5cba25cca 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SqlAuthenticationToken.java @@ -24,7 +24,7 @@ public class SqlAuthenticationToken implements Serializable { private final String accessToken; /** - * Contructs a SqlAuthentication token. + * Constructs a SqlAuthentication token. * * @param accessToken * The access token string. @@ -37,7 +37,7 @@ public SqlAuthenticationToken(String accessToken, long expiresOn) { } /** - * Contructs a SqlAuthentication token. + * Constructs a SqlAuthentication token. * * @param accessToken * The access token string. diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java b/src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java index b17e86c0d..928a59490 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java @@ -138,8 +138,8 @@ public SqlAuthenticationToken getAccessToken(String spn, String stsurl) { if (expireTokenToggle) { Date now = new Date(); - long minutesToExpireWithin = TEST_TOKEN_EXPIRY_SECONDS * 1000; // Expire within 2 minutes - return new SqlAuthenticationToken(accessToken, now.getTime() + minutesToExpireWithin); + long millisecondsToExpireWithin = TEST_TOKEN_EXPIRY_SECONDS * 1000; // Expire within 2 minutes + return new SqlAuthenticationToken(accessToken, now.getTime() + millisecondsToExpireWithin); } else { return new SqlAuthenticationToken(accessToken, expiresOn); }