private static void ExtractExpiresInAndAudience(string simpleWebToken, out DateTime expiresIn, out string audience) { expiresIn = DateTime.MinValue; audience = null; if (string.IsNullOrWhiteSpace(simpleWebToken)) { TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyToken, new object[0])); } else { IDictionary <string, string> strs = TokenProviderHelper.Decode(simpleWebToken); string item = strs["ExpiresOn"]; if (string.IsNullOrWhiteSpace(item)) { TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyExpiration, new object[0])); } expiresIn = TokenConstants.WrapBaseTime + TimeSpan.FromSeconds(double.Parse(HttpUtility.UrlDecode(item.Trim()), CultureInfo.InvariantCulture)); audience = strs["Audience"]; if (string.IsNullOrWhiteSpace(item)) { TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyAudience, new object[0])); return; } } }
private static string ExtractAudience(string token) { string str = null; if (token != null) { string[] strArrays = token.Split(new char[] { '&' }); for (int i = 0; i < (int)strArrays.Length; i++) { string str1 = strArrays[i]; string[] strArrays1 = str1.Split(new char[] { '=' }); if ((int)strArrays1.Length != 2) { string tokenProviderInvalidTokenParameter = Resources.TokenProviderInvalidTokenParameter; object[] objArray = new object[] { str1 }; TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(tokenProviderInvalidTokenParameter, objArray)); } else if (strArrays1[0].Trim() == "Audience") { str = HttpUtility.UrlDecode(strArrays1[1].Trim()); } } } if (string.IsNullOrEmpty(str)) { TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyAudience, new object[0])); } return(str); }
protected override IAsyncResult OnBeginGetToken(string appliesTo, string action, TimeSpan timeout, AsyncCallback callback, object state) { base.ValidateAction(action); Uri stsUri = TokenProviderHelper.GetStsUri(this.stsUri, appliesTo); return(TokenProviderHelper.BeginGetAccessTokenByAssertion(stsUri, appliesTo, SharedSecretTokenProvider.ComputeSimpleWebTokenString(this.issuerName, this.issuerSecret), "SWT", timeout, callback, state)); }
protected override IAsyncResult OnBeginGetToken(string appliesTo, string action, TimeSpan timeout, AsyncCallback callback, object state) { base.ValidateAction(action); Uri stsUri = TokenProviderHelper.GetStsUri(this.stsUri, appliesTo); TokenProviderHelper.TokenResult <SecurityToken> accessTokenByAssertion = TokenProviderHelper.GetAccessTokenByAssertion(stsUri, appliesTo, this.SamlToken, "SAML", timeout); return(new CompletedAsyncResult <TokenProviderHelper.TokenResult <SecurityToken> >(accessTokenByAssertion, callback, state)); }
protected override IAsyncResult OnBeginGetWebToken(string appliesTo, string action, TimeSpan timeout, AsyncCallback callback, object state) { base.ValidateAction(action); Uri stsUri = TokenProviderHelper.GetStsUri(this.stsUri, appliesTo); TokenProviderHelper.TokenResult <string> httpAuthAccessTokenByAssertion = TokenProviderHelper.GetHttpAuthAccessTokenByAssertion(stsUri, appliesTo, this.SimpleWebToken, "SWT", timeout); return(new CompletedAsyncResult <TokenProviderHelper.TokenResult <string> >(httpAuthAccessTokenByAssertion, callback, state)); }
public static string GetWindowsAccessTokenCore(IEnumerator <Uri> stsUris, Func <Uri, Uri> uriBuilder, string requestToken, TimeSpan timeout, out DateTime expiresIn, out string audience) { byte[] bytes = Encoding.UTF8.GetBytes(requestToken); string end = null; expiresIn = DateTime.MinValue; audience = null; bool flag = stsUris.MoveNext(); while (flag) { Uri uri = uriBuilder(stsUris.Current); try { HttpWebRequest servicePointMaxIdleTimeMilliSeconds = WebRequest.Create(uri) as HttpWebRequest; servicePointMaxIdleTimeMilliSeconds.ServicePoint.MaxIdleTime = Constants.ServicePointMaxIdleTimeMilliSeconds; servicePointMaxIdleTimeMilliSeconds.AllowAutoRedirect = true; servicePointMaxIdleTimeMilliSeconds.MaximumAutomaticRedirections = 1; servicePointMaxIdleTimeMilliSeconds.Method = "POST"; servicePointMaxIdleTimeMilliSeconds.ContentType = "application/x-www-form-urlencoded"; servicePointMaxIdleTimeMilliSeconds.ContentLength = (long)((int)bytes.Length); servicePointMaxIdleTimeMilliSeconds.Timeout = TokenProviderHelper.ConvertToInt32(timeout); servicePointMaxIdleTimeMilliSeconds.UseDefaultCredentials = true; AuthenticationManager.CustomTargetNameDictionary[uri.AbsoluteUri] = "HTTP\\"; using (Stream requestStream = servicePointMaxIdleTimeMilliSeconds.GetRequestStream()) { requestStream.Write(bytes, 0, (int)bytes.Length); } using (HttpWebResponse response = servicePointMaxIdleTimeMilliSeconds.GetResponse() as HttpWebResponse) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8)) { end = streamReader.ReadToEnd(); TokenProviderHelper.ExtractExpiresInAndAudience(end, out expiresIn, out audience); } } } flag = false; } catch (ArgumentException argumentException) { TokenProviderHelper.ThrowException(uri, argumentException); } catch (WebException webException1) { WebException webException = webException1; flag = stsUris.MoveNext(); if (!flag) { TokenProviderHelper.ThrowException(uri, webException); } } } return(end); }
internal static Uri GetStsUri(Uri stsUri, string appliesTo) { if (stsUri != null) { return(new Uri(stsUri, "WRAPv0.9/")); } string str = TokenProviderHelper.ExtractSolutionFromHostname((new Uri(appliesTo)).Host); return(ServiceBusEnvironment.CreateAccessControlUri(str)); }
internal static TokenProviderHelper.TokenResult <SecurityToken> GetAccessTokenByAssertion(Uri requestUri, string appliesTo, string requestToken, string simpleAuthAssertionFormat, TimeSpan timeout) { string str; string str1; string accessTokenCore = TokenProviderHelper.GetAccessTokenCore(requestUri, appliesTo, requestToken, simpleAuthAssertionFormat, timeout, out str, out str1); DateTime dateTime = TokenProviderHelper.ConvertExpiry(str); SimpleWebSecurityToken simpleWebSecurityToken = new SimpleWebSecurityToken(accessTokenCore, dateTime, str1); return(new TokenProviderHelper.TokenResult <SecurityToken>() { CacheUntil = dateTime, Token = simpleWebSecurityToken }); }
internal static TokenProviderHelper.TokenResult <SecurityToken> EndGetAccessTokenByAssertion(IAsyncResult result) { string str; string str1; string str2 = TokenProviderHelper.EndGetAccessTokenCore(result, out str, out str1); DateTime dateTime = TokenProviderHelper.ConvertExpiry(str); SimpleWebSecurityToken simpleWebSecurityToken = new SimpleWebSecurityToken(str2, dateTime, str1); return(new TokenProviderHelper.TokenResult <SecurityToken>() { CacheUntil = dateTime, Token = simpleWebSecurityToken }); }
protected override IAsyncResult OnBeginGetToken(string appliesTo, string action, TimeSpan timeout, AsyncCallback callback, object state) { DateTime dateTime; string str; string windowsAccessTokenCore = TokenProviderHelper.GetWindowsAccessTokenCore(this.stsUris.GetEnumerator(), this.onBuildUri, this.BuildRequestToken(appliesTo), timeout, out dateTime, out str); SimpleWebSecurityToken simpleWebSecurityToken = new SimpleWebSecurityToken(windowsAccessTokenCore, dateTime, str); TokenProviderHelper.TokenResult <SecurityToken> tokenResult = new TokenProviderHelper.TokenResult <SecurityToken>() { CacheUntil = dateTime, Token = simpleWebSecurityToken }; return(new CompletedAsyncResult <TokenProviderHelper.TokenResult <SecurityToken> >(tokenResult, callback, state)); }
private Exception ConvertException(Exception exception) { if (exception is ArgumentException) { return(TokenProviderHelper.ConvertException(this.requestUri, exception)); } if (exception is WebException) { return(TokenProviderHelper.ConvertWebException(this.requestUri, (WebException)exception)); } if (!(exception is IOException) || !this.requestTimedOut) { return(exception); } return(new TimeoutException(SRClient.OperationRequestTimedOut(base.OriginalTimeout), exception)); }
private static void ExtractAccessToken(string urlParameters, out string token, out string expiresIn, out string audience) { token = null; expiresIn = null; audience = null; if (urlParameters != null) { string[] strArrays = urlParameters.Split(new char[] { '&' }); for (int i = 0; i < (int)strArrays.Length; i++) { string str = strArrays[i]; string[] strArrays1 = str.Split(new char[] { '=' }); if ((int)strArrays1.Length != 2) { string tokenProviderInvalidTokenParameter = Resources.TokenProviderInvalidTokenParameter; object[] objArray = new object[] { str }; TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(tokenProviderInvalidTokenParameter, objArray)); } else { string str1 = strArrays1[0].Trim(); if (str1 == "wrap_access_token") { token = HttpUtility.UrlDecode(strArrays1[1].Trim()); audience = TokenProviderHelper.ExtractAudience(token); } else if (str1 == "wrap_access_token_expires_in") { expiresIn = HttpUtility.UrlDecode(strArrays1[1].Trim()); } } } } if (string.IsNullOrEmpty(token)) { TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyToken, new object[0])); } if (string.IsNullOrEmpty(expiresIn)) { TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyExpiration, new object[0])); } if (string.IsNullOrEmpty(audience)) { TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyAudience, new object[0])); } }
protected override IAsyncResult OnBeginGetWebToken(string appliesTo, string action, TimeSpan timeout, AsyncCallback callback, object state) { DateTime dateTime; string str; string windowsAccessTokenCore = TokenProviderHelper.GetWindowsAccessTokenCore(this.stsUris.GetEnumerator(), this.onBuildUri, this.BuildRequestToken(appliesTo), timeout, out dateTime, out str); CultureInfo invariantCulture = CultureInfo.InvariantCulture; object[] objArray = new object[] { "WRAP", "access_token", windowsAccessTokenCore }; string str1 = string.Format(invariantCulture, "{0} {1}=\"{2}\"", objArray); TokenProviderHelper.TokenResult <string> tokenResult = new TokenProviderHelper.TokenResult <string>() { CacheUntil = dateTime, Token = str1 }; return(new CompletedAsyncResult <TokenProviderHelper.TokenResult <string> >(tokenResult, callback, state)); }
internal static TokenProviderHelper.TokenResult <string> GetHttpAuthAccessTokenByAssertion(Uri requestUri, string appliesTo, string requestToken, string simpleAuthAssertionFormat, TimeSpan timeout) { string str; string str1; string accessTokenCore = TokenProviderHelper.GetAccessTokenCore(requestUri, appliesTo, requestToken, simpleAuthAssertionFormat, timeout, out str, out str1); DateTime dateTime = TokenProviderHelper.ConvertExpiry(str); CultureInfo invariantCulture = CultureInfo.InvariantCulture; object[] objArray = new object[] { "WRAP", "access_token", accessTokenCore }; string str2 = string.Format(invariantCulture, "{0} {1}=\"{2}\"", objArray); return(new TokenProviderHelper.TokenResult <string>() { CacheUntil = dateTime, Token = str2 }); }
private static void ThrowException(Uri requestUri, WebException exception) { throw TokenProviderHelper.ConvertWebException(requestUri, exception); }
protected override SecurityToken OnEndGetToken(IAsyncResult result, out DateTime cacheUntil) { TokenProviderHelper.TokenResult <SecurityToken> tokenResult = TokenProviderHelper.EndGetAccessTokenByAssertion(result); cacheUntil = tokenResult.CacheUntil; return(tokenResult.Token); }
private static string GetAccessTokenCore(Uri requestUri, string appliesTo, string requestToken, string simpleAuthAssertionFormat, TimeSpan timeout, out string expiresIn, out string audience) { StringBuilder stringBuilder = new StringBuilder(); CultureInfo invariantCulture = CultureInfo.InvariantCulture; object[] objArray = new object[] { "wrap_scope", HttpUtility.UrlEncode(appliesTo) }; stringBuilder.AppendFormat(invariantCulture, "{0}={1}", objArray); stringBuilder.Append('&'); CultureInfo cultureInfo = CultureInfo.InvariantCulture; object[] objArray1 = new object[] { "wrap_assertion_format", simpleAuthAssertionFormat }; stringBuilder.AppendFormat(cultureInfo, "{0}={1}", objArray1); stringBuilder.Append('&'); CultureInfo invariantCulture1 = CultureInfo.InvariantCulture; object[] objArray2 = new object[] { "wrap_assertion", HttpUtility.UrlEncode(requestToken) }; stringBuilder.AppendFormat(invariantCulture1, "{0}={1}", objArray2); byte[] bytes = Encoding.UTF8.GetBytes(stringBuilder.ToString()); string str = null; expiresIn = null; audience = null; try { HttpWebRequest servicePointMaxIdleTimeMilliSeconds = WebRequest.Create(requestUri) as HttpWebRequest; servicePointMaxIdleTimeMilliSeconds.ServicePoint.MaxIdleTime = Constants.ServicePointMaxIdleTimeMilliSeconds; servicePointMaxIdleTimeMilliSeconds.AllowAutoRedirect = true; servicePointMaxIdleTimeMilliSeconds.MaximumAutomaticRedirections = 1; servicePointMaxIdleTimeMilliSeconds.Method = "POST"; servicePointMaxIdleTimeMilliSeconds.ContentType = "application/x-www-form-urlencoded"; servicePointMaxIdleTimeMilliSeconds.ContentLength = (long)((int)bytes.Length); try { servicePointMaxIdleTimeMilliSeconds.Timeout = Convert.ToInt32(timeout.TotalMilliseconds, CultureInfo.InvariantCulture); } catch (OverflowException overflowException) { throw new ArgumentException(SRClient.TimeoutExceeded, overflowException); } using (Stream requestStream = servicePointMaxIdleTimeMilliSeconds.GetRequestStream()) { requestStream.Write(bytes, 0, (int)bytes.Length); } using (HttpWebResponse response = (HttpWebResponse)servicePointMaxIdleTimeMilliSeconds.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8)) { TokenProviderHelper.ExtractAccessToken(streamReader.ReadToEnd(), out str, out expiresIn, out audience); } } } } catch (ArgumentException argumentException) { TokenProviderHelper.ThrowException(requestUri, argumentException); } catch (WebException webException) { TokenProviderHelper.ThrowException(requestUri, webException); } return(str); }
protected override IEnumerator <IteratorAsyncResult <TokenProviderHelper.TokenRequestAsyncResult> .AsyncStep> GetAsyncSteps() { try { StringBuilder stringBuilder = new StringBuilder(); CultureInfo invariantCulture = CultureInfo.InvariantCulture; object[] objArray = new object[] { "wrap_scope", HttpUtility.UrlEncode(this.appliesTo) }; stringBuilder.AppendFormat(invariantCulture, "{0}={1}", objArray); stringBuilder.Append('&'); CultureInfo cultureInfo = CultureInfo.InvariantCulture; object[] objArray1 = new object[] { "wrap_assertion_format", this.simpleAuthAssertionFormat }; stringBuilder.AppendFormat(cultureInfo, "{0}={1}", objArray1); stringBuilder.Append('&'); CultureInfo invariantCulture1 = CultureInfo.InvariantCulture; object[] objArray2 = new object[] { "wrap_assertion", HttpUtility.UrlEncode(this.requestToken) }; stringBuilder.AppendFormat(invariantCulture1, "{0}={1}", objArray2); this.body = Encoding.UTF8.GetBytes(stringBuilder.ToString()); this.request = (HttpWebRequest)WebRequest.Create(this.requestUri); this.request.ServicePoint.MaxIdleTime = Constants.ServicePointMaxIdleTimeMilliSeconds; this.request.AllowAutoRedirect = true; this.request.MaximumAutomaticRedirections = 1; this.request.Method = "POST"; this.request.ContentType = "application/x-www-form-urlencoded"; this.request.ContentLength = (long)((int)this.body.Length); try { HttpWebRequest num = this.request; TimeSpan timeSpan = base.RemainingTime(); num.Timeout = Convert.ToInt32(timeSpan.TotalMilliseconds, CultureInfo.InvariantCulture); } catch (OverflowException overflowException) { throw new ArgumentException(SRClient.TimeoutExceeded, overflowException); } } catch (ArgumentException argumentException) { base.Complete(this.ConvertException(argumentException)); goto Label0; } try { this.requestCancelTimer = new IOThreadTimer(TokenProviderHelper.TokenRequestAsyncResult.onCancelTimer, this, true); try { TimeSpan timeSpan1 = base.RemainingTime(); if (timeSpan1 > TimeSpan.Zero) { TokenProviderHelper.TokenRequestAsyncResult tokenRequestAsyncResult = this; IteratorAsyncResult <TokenProviderHelper.TokenRequestAsyncResult> .BeginCall beginCall = (TokenProviderHelper.TokenRequestAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => { IAsyncResult asyncResult = thisPtr.request.BeginGetRequestStream(c, s); thisPtr.requestCancelTimer.Set(timeSpan1); return(asyncResult); }; yield return(tokenRequestAsyncResult.CallAsync(beginCall, (TokenProviderHelper.TokenRequestAsyncResult thisPtr, IAsyncResult r) => thisPtr.requestStream = thisPtr.request.EndGetRequestStream(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue)); if (base.LastAsyncStepException == null) { TokenProviderHelper.TokenRequestAsyncResult tokenRequestAsyncResult1 = this; IteratorAsyncResult <TokenProviderHelper.TokenRequestAsyncResult> .BeginCall beginCall1 = (TokenProviderHelper.TokenRequestAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.requestStream.BeginWrite(thisPtr.body, 0, (int)thisPtr.body.Length, c, s); yield return(tokenRequestAsyncResult1.CallAsync(beginCall1, (TokenProviderHelper.TokenRequestAsyncResult thisPtr, IAsyncResult r) => thisPtr.requestStream.EndWrite(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue)); if (base.LastAsyncStepException == null) { goto Label1; } base.Complete(this.ConvertException(base.LastAsyncStepException)); goto Label0; } else { base.Complete(this.ConvertException(base.LastAsyncStepException)); goto Label0; } } else { base.Complete(new TimeoutException(SRClient.OperationRequestTimedOut(base.OriginalTimeout))); goto Label0; } } finally { if (this.requestStream != null) { this.requestStream.Dispose(); } this.requestCancelTimer.Cancel(); } Label1: TimeSpan timeSpan2 = base.RemainingTime(); if (timeSpan2 > TimeSpan.Zero) { TokenProviderHelper.TokenRequestAsyncResult tokenRequestAsyncResult2 = this; IteratorAsyncResult <TokenProviderHelper.TokenRequestAsyncResult> .BeginCall beginCall2 = (TokenProviderHelper.TokenRequestAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => { IAsyncResult asyncResult = thisPtr.request.BeginGetResponse(c, s); thisPtr.requestCancelTimer.Set(timeSpan2); return(asyncResult); }; yield return(tokenRequestAsyncResult2.CallAsync(beginCall2, (TokenProviderHelper.TokenRequestAsyncResult thisPtr, IAsyncResult r) => thisPtr.response = (HttpWebResponse)thisPtr.request.EndGetResponse(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue)); if (base.LastAsyncStepException == null) { TokenProviderHelper.TokenRequestAsyncResult tokenRequestAsyncResult3 = this; Stream responseStream = this.response.GetResponseStream(); Stream stream = responseStream; tokenRequestAsyncResult3.sourceStream = responseStream; using (stream) { TokenProviderHelper.TokenRequestAsyncResult tokenRequestAsyncResult4 = this; MemoryStream memoryStream = new MemoryStream(); MemoryStream memoryStream1 = memoryStream; tokenRequestAsyncResult4.tmpStream = memoryStream; using (memoryStream1) { TokenProviderHelper.TokenRequestAsyncResult tokenRequestAsyncResult5 = this; IteratorAsyncResult <TokenProviderHelper.TokenRequestAsyncResult> .BeginCall streamReaderAsyncResult = (TokenProviderHelper.TokenRequestAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => new TokenProviderHelper.StreamReaderAsyncResult(thisPtr.sourceStream, thisPtr.tmpStream, t, c, s); yield return(tokenRequestAsyncResult5.CallAsync(streamReaderAsyncResult, (TokenProviderHelper.TokenRequestAsyncResult thisPtr, IAsyncResult r) => AsyncResult <TokenProviderHelper.StreamReaderAsyncResult> .End(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue)); if (base.LastAsyncStepException == null) { try { this.tmpStream.Position = (long)0; using (StreamReader streamReader = new StreamReader(this.tmpStream, Encoding.UTF8)) { string end = streamReader.ReadToEnd(); TokenProviderHelper.ExtractAccessToken(end, out this.accessToken, out this.expiresIn, out this.audience); } } catch (Exception exception1) { Exception exception = exception1; if (Fx.IsFatal(exception)) { throw; } base.Complete(this.ConvertException(exception)); } } else { base.Complete(this.ConvertException(base.LastAsyncStepException)); } } } } else { base.Complete(this.ConvertException(base.LastAsyncStepException)); } } else { base.Complete(new TimeoutException(SRClient.OperationRequestTimedOut(base.OriginalTimeout))); } } finally { if (this.requestCancelTimer != null) { this.requestCancelTimer.Cancel(); } } Label0: yield break; }
private static void ThrowException(Uri requestUri, Exception innerException) { throw TokenProviderHelper.ConvertException(requestUri, innerException); }
internal static IAsyncResult BeginGetAccessTokenByAssertion(Uri requestUri, string appliesTo, string requestToken, string simpleAuthAssertionFormat, TimeSpan timeout, AsyncCallback callback, object state) { return(TokenProviderHelper.BeginGetAccessTokenCore(requestUri, appliesTo, requestToken, simpleAuthAssertionFormat, timeout, callback, state)); }