|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections; |
| 7 | +using System.Linq; |
| 8 | +using System.Reflection; |
| 9 | + |
| 10 | +namespace Microsoft.Data.SqlClient.ManualTesting.Tests.SQL.Common.SystemDataInternals |
| 11 | +{ |
| 12 | + internal static class FedAuthTokenHelper |
| 13 | + { |
| 14 | + internal static DateTime? GetTokenExpiryDateTime(SqlConnection connection, out string tokenHash) |
| 15 | + { |
| 16 | + try |
| 17 | + { |
| 18 | + object authenticationContextValueObj = GetAuthenticationContextValue(connection); |
| 19 | + |
| 20 | + DateTime expirationTimeProperty = (DateTime)authenticationContextValueObj.GetType().GetProperty("ExpirationTime", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(authenticationContextValueObj, null); |
| 21 | + |
| 22 | + tokenHash = GetTokenHash(authenticationContextValueObj); |
| 23 | + |
| 24 | + return expirationTimeProperty; |
| 25 | + } |
| 26 | + catch (Exception) |
| 27 | + { |
| 28 | + tokenHash = ""; |
| 29 | + return null; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + internal static DateTime? SetTokenExpiryDateTime(SqlConnection connection, int minutesToExpire, out string tokenHash) |
| 34 | + { |
| 35 | + try |
| 36 | + { |
| 37 | + object authenticationContextValueObj = GetAuthenticationContextValue(connection); |
| 38 | + |
| 39 | + DateTime expirationTimeProperty = (DateTime)authenticationContextValueObj.GetType().GetProperty("ExpirationTime", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(authenticationContextValueObj, null); |
| 40 | + |
| 41 | + expirationTimeProperty = DateTime.UtcNow.AddMinutes(minutesToExpire); |
| 42 | + |
| 43 | + FieldInfo expirationTimeInfo = authenticationContextValueObj.GetType().GetField("_expirationTime", BindingFlags.NonPublic | BindingFlags.Instance); |
| 44 | + expirationTimeInfo.SetValue(authenticationContextValueObj, expirationTimeProperty); |
| 45 | + |
| 46 | + tokenHash = GetTokenHash(authenticationContextValueObj); |
| 47 | + |
| 48 | + return expirationTimeProperty; |
| 49 | + } |
| 50 | + catch (Exception) |
| 51 | + { |
| 52 | + tokenHash = ""; |
| 53 | + return null; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + internal static string GetTokenHash(object authenticationContextValueObj) |
| 58 | + { |
| 59 | + try |
| 60 | + { |
| 61 | + Assembly sqlConnectionAssembly = Assembly.GetAssembly(typeof(SqlConnection)); |
| 62 | + |
| 63 | + Type sqlFedAuthTokenType = sqlConnectionAssembly.GetType("Microsoft.Data.SqlClient.SqlFedAuthToken"); |
| 64 | + |
| 65 | + Type[] sqlFedAuthTokenTypeArray = new Type[] { sqlFedAuthTokenType }; |
| 66 | + |
| 67 | + ConstructorInfo sqlFedAuthTokenConstructorInfo = sqlFedAuthTokenType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, CallingConventions.Any, Type.EmptyTypes, null); |
| 68 | + |
| 69 | + Type activeDirectoryAuthenticationTimeoutRetryHelperType = sqlConnectionAssembly.GetType("Microsoft.Data.SqlClient.ActiveDirectoryAuthenticationTimeoutRetryHelper"); |
| 70 | + |
| 71 | + ConstructorInfo activeDirectoryAuthenticationTimeoutRetryHelperConstructorInfo = activeDirectoryAuthenticationTimeoutRetryHelperType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, CallingConventions.Any, Type.EmptyTypes, null); |
| 72 | + |
| 73 | + object activeDirectoryAuthenticationTimeoutRetryHelperObj = activeDirectoryAuthenticationTimeoutRetryHelperConstructorInfo.Invoke(new object[] { }); |
| 74 | + |
| 75 | + MethodInfo tokenHashInfo = activeDirectoryAuthenticationTimeoutRetryHelperObj.GetType().GetMethod("GetTokenHash", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, CallingConventions.Any, sqlFedAuthTokenTypeArray, null); |
| 76 | + |
| 77 | + byte[] tokenBytes = (byte[])authenticationContextValueObj.GetType().GetProperty("AccessToken", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(authenticationContextValueObj, null); |
| 78 | + |
| 79 | + object sqlFedAuthTokenObj = sqlFedAuthTokenConstructorInfo.Invoke(new object[] { }); |
| 80 | + FieldInfo accessTokenInfo = sqlFedAuthTokenObj.GetType().GetField("accessToken", BindingFlags.NonPublic | BindingFlags.Instance); |
| 81 | + accessTokenInfo.SetValue(sqlFedAuthTokenObj, tokenBytes); |
| 82 | + |
| 83 | + string tokenHash = (string)tokenHashInfo.Invoke(activeDirectoryAuthenticationTimeoutRetryHelperObj, new object[] { sqlFedAuthTokenObj }); |
| 84 | + |
| 85 | + return tokenHash; |
| 86 | + } |
| 87 | + catch (Exception) |
| 88 | + { |
| 89 | + return ""; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + internal static object GetAuthenticationContextValue(SqlConnection connection) |
| 94 | + { |
| 95 | + object innerConnectionObj = connection.GetType().GetProperty("InnerConnection", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(connection); |
| 96 | + |
| 97 | + object databaseConnectionPoolObj = innerConnectionObj.GetType().GetProperty("Pool", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(innerConnectionObj); |
| 98 | + |
| 99 | + IEnumerable authenticationContexts = (IEnumerable)databaseConnectionPoolObj.GetType().GetProperty("AuthenticationContexts", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(databaseConnectionPoolObj, null); |
| 100 | + |
| 101 | + object authenticationContextObj = authenticationContexts.Cast<object>().FirstOrDefault(); |
| 102 | + |
| 103 | + object authenticationContextValueObj = authenticationContextObj.GetType().GetProperty("Value").GetValue(authenticationContextObj, null); |
| 104 | + |
| 105 | + return authenticationContextValueObj; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments