|
16 | 16 | import java.net.DatagramSocket;
|
17 | 17 | import java.net.IDN;
|
18 | 18 | import java.net.InetAddress;
|
19 |
| -import java.net.MalformedURLException; |
20 | 19 | import java.net.SocketException;
|
21 | 20 | import java.net.UnknownHostException;
|
22 | 21 | import java.sql.Blob;
|
|
38 | 37 | import java.sql.Struct;
|
39 | 38 | import java.text.MessageFormat;
|
40 | 39 | import java.util.Arrays;
|
41 |
| -import java.util.Date; |
42 | 40 | import java.util.Enumeration;
|
43 | 41 | import java.util.HashMap;
|
44 | 42 | import java.util.List;
|
45 | 43 | import java.util.Locale;
|
46 | 44 | import java.util.Map;
|
47 | 45 | import java.util.Properties;
|
48 | 46 | import java.util.UUID;
|
49 |
| -import java.util.concurrent.ExecutionException; |
50 | 47 | import java.util.concurrent.Executor;
|
51 |
| -import java.util.concurrent.ExecutorService; |
52 |
| -import java.util.concurrent.Executors; |
53 |
| -import java.util.concurrent.Future; |
54 | 48 | import java.util.concurrent.TimeUnit;
|
55 | 49 | import java.util.concurrent.atomic.AtomicInteger;
|
56 | 50 | import java.util.logging.Level;
|
57 | 51 |
|
58 | 52 | import javax.sql.XAConnection;
|
59 | 53 | import javax.xml.bind.DatatypeConverter;
|
60 | 54 |
|
61 |
| -import com.microsoft.aad.adal4j.AuthenticationContext; |
62 |
| -import com.microsoft.aad.adal4j.AuthenticationException; |
63 |
| -import com.microsoft.aad.adal4j.AuthenticationResult; |
64 |
| - |
65 | 55 | /**
|
66 | 56 | * SQLServerConnection implements a JDBC connection to SQL Server. SQLServerConnections support JDBC connection pooling and may be either physical
|
67 | 57 | * JDBC connections or logical JDBC connections.
|
@@ -146,46 +136,24 @@ class FederatedAuthenticationFeatureExtensionData {
|
146 | 136 | }
|
147 | 137 |
|
148 | 138 | class SqlFedAuthInfo {
|
149 |
| - private String spn; |
150 |
| - private String stsurl; |
| 139 | + String spn; |
| 140 | + String stsurl; |
151 | 141 |
|
152 | 142 | @Override
|
153 | 143 | public String toString() {
|
154 | 144 | return "STSURL: " + stsurl + ", SPN: " + spn;
|
155 | 145 | }
|
156 | 146 | }
|
157 | 147 |
|
158 |
| - final class SqlFedAuthToken { |
159 |
| - private final Date expiresOn; |
160 |
| - private final String accessToken; |
161 |
| - |
162 |
| - SqlFedAuthToken(final String accessToken, |
163 |
| - final long expiresIn) { |
164 |
| - this.accessToken = accessToken; |
165 |
| - |
166 |
| - Date now = new Date(); |
167 |
| - now.setTime(now.getTime() + (expiresIn * 1000)); |
168 |
| - this.expiresOn = now; |
169 |
| - } |
170 |
| - |
171 |
| - SqlFedAuthToken(final String accessToken, |
172 |
| - final Date expiresOn) { |
173 |
| - this.accessToken = accessToken; |
174 |
| - this.expiresOn = expiresOn; |
175 |
| - } |
176 |
| - |
177 |
| - Date getExpiresOnDate() { |
178 |
| - return expiresOn; |
179 |
| - } |
180 |
| - } |
| 148 | + |
181 | 149 |
|
182 |
| - private class ActiveDirectoryAuthentication { |
183 |
| - private static final String jdbcFedauthClientId = "7f98cb04-cd1e-40df-9140-3bf7e2cea4db"; |
184 |
| - private static final String AdalGetAccessTokenFunctionName = "ADALGetAccessToken"; |
185 |
| - private static final int GetAccessTokenSuccess = 0; |
186 |
| - private static final int GetAccessTokenInvalidGrant = 1; |
187 |
| - private static final int GetAccessTokenTansisentError = 2; |
188 |
| - private static final int GetAccessTokenOtherError = 3; |
| 150 | + class ActiveDirectoryAuthentication { |
| 151 | + static final String jdbcFedauthClientId = "7f98cb04-cd1e-40df-9140-3bf7e2cea4db"; |
| 152 | + static final String AdalGetAccessTokenFunctionName = "ADALGetAccessToken"; |
| 153 | + static final int GetAccessTokenSuccess = 0; |
| 154 | + static final int GetAccessTokenInvalidGrant = 1; |
| 155 | + static final int GetAccessTokenTansisentError = 2; |
| 156 | + static final int GetAccessTokenOtherError = 3; |
189 | 157 | }
|
190 | 158 |
|
191 | 159 | /**
|
@@ -3474,39 +3442,10 @@ private SqlFedAuthToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throws SQLSe
|
3474 | 3442 | numberOfAttempts++;
|
3475 | 3443 |
|
3476 | 3444 | if (authenticationString.trim().equalsIgnoreCase(SqlAuthentication.ActiveDirectoryPassword.toString())) {
|
3477 |
| - ExecutorService executorService = Executors.newFixedThreadPool(1); |
3478 |
| - try { |
3479 |
| - AuthenticationContext context = new AuthenticationContext(fedAuthInfo.stsurl, false, executorService); |
3480 |
| - Future<AuthenticationResult> future = context.acquireToken(fedAuthInfo.spn, ActiveDirectoryAuthentication.jdbcFedauthClientId, |
3481 |
| - user, password, null); |
3482 |
| - |
3483 |
| - AuthenticationResult authenticationResult = future.get(); |
3484 |
| - fedAuthToken = new SqlFedAuthToken(authenticationResult.getAccessToken(), authenticationResult.getExpiresOnDate()); |
| 3445 | + fedAuthToken = SQLServerADAL4JUtils.getSqlFedAuthToken(fedAuthInfo, user, password, authenticationString); |
3485 | 3446 |
|
3486 |
| - // Break out of the retry loop in successful case. |
3487 |
| - break; |
3488 |
| - } |
3489 |
| - catch (MalformedURLException | InterruptedException e) { |
3490 |
| - throw new SQLServerException(e.getMessage(), null); |
3491 |
| - } |
3492 |
| - catch (ExecutionException e) { |
3493 |
| - MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ADALExecution")); |
3494 |
| - Object[] msgArgs = {user, authenticationString}; |
3495 |
| - |
3496 |
| - // the cause error message uses \\n\\r which does not give correct format |
3497 |
| - // change it to \r\n to provide correct format |
3498 |
| - String correctedErrorMessage = e.getCause().getMessage().replaceAll("\\\\r\\\\n", "\r\n"); |
3499 |
| - AuthenticationException correctedAuthenticationException = new AuthenticationException(correctedErrorMessage); |
3500 |
| - |
3501 |
| - // SQLServerException is caused by ExecutionException, which is caused by AuthenticationException |
3502 |
| - // to match the exception tree before error message correction |
3503 |
| - ExecutionException correctedExecutionException = new ExecutionException(correctedAuthenticationException); |
3504 |
| - |
3505 |
| - throw new SQLServerException(form.format(msgArgs), null, 0, correctedExecutionException); |
3506 |
| - } |
3507 |
| - finally { |
3508 |
| - executorService.shutdown(); |
3509 |
| - } |
| 3447 | + // Break out of the retry loop in successful case. |
| 3448 | + break; |
3510 | 3449 | }
|
3511 | 3450 | else if (authenticationString.trim().equalsIgnoreCase(SqlAuthentication.ActiveDirectoryIntegrated.toString())) {
|
3512 | 3451 | try {
|
|
0 commit comments