// A OAuth handler when accessToken and accessTokenSecrets are known private static HttpMessageHandler OAuthHandlerWithExistingToken(String requestTokenURL, String authorizationTokenURL, String accessTokenURL, String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret) { ServiceProviderDescription serviceDescription = new ServiceProviderDescription(); serviceDescription.AccessTokenEndpoint = new MessageReceivingEndpoint(new Uri(accessTokenURL), HttpDeliveryMethods.PostRequest); serviceDescription.ProtocolVersion = ProtocolVersion.V10a; serviceDescription.RequestTokenEndpoint = new MessageReceivingEndpoint(new Uri(requestTokenURL), HttpDeliveryMethods.PostRequest); serviceDescription.TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() }; serviceDescription.UserAuthorizationEndpoint = new MessageReceivingEndpoint(new Uri(authorizationTokenURL), HttpDeliveryMethods.PostRequest); TokenManager tokenManager = new TokenManager(consumerKey, consumerSecret); // Here we add the known token info tokenManager.AddKnownAccessTokens(accessToken, accessTokenSecret); WebConsumer consumer = new WebConsumer(serviceDescription, tokenManager); DesktopConsumer desktopConsumer = new DesktopConsumer(serviceDescription, tokenManager); return(consumer.CreateAuthorizingHandler(accessToken, CreateSSLHandler())); }
private HttpClient CreateClient() { string accessToken = AccessToken; var innerHandler = new HttpClientHandler(); HttpMessageHandler handler; if (string.IsNullOrWhiteSpace(accessToken)) { var signedMessageHandler = new OAuth1HmacSha1HttpMessageHandler(innerHandler); signedMessageHandler.ConsumerKey = _TokenManager.ConsumerKey; signedMessageHandler.ConsumerSecret = _TokenManager.ConsumerSecret; handler = signedMessageHandler; } else { handler = _WebWorker.CreateAuthorizingHandler(accessToken, innerHandler); } var client = new HttpClient(handler); client.BaseAddress = new Uri(_BaseAddress); client.DefaultRequestHeaders.Accept.Add(JsonMediaTypeHeaderValue); return(client); }
internal DelegatingHandler CreateAuthorizingHandler(string accessToken, HttpMessageHandler innerHandler) { return(_consumer.CreateAuthorizingHandler(accessToken, innerHandler)); }
private static HttpMessageHandler OAuthHandler(string requestTokenURL, string authorizationTokenURL, string accessTokenURL, string consumerKey, string consumerSecret, string user, string passwd, string authUrl) { ServiceProviderDescription serviceDescription = new ServiceProviderDescription(); serviceDescription.AccessTokenEndpoint = new MessageReceivingEndpoint(new Uri(accessTokenURL), HttpDeliveryMethods.PostRequest); serviceDescription.ProtocolVersion = ProtocolVersion.V10a; serviceDescription.RequestTokenEndpoint = new MessageReceivingEndpoint(new Uri(requestTokenURL), HttpDeliveryMethods.PostRequest); serviceDescription.TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() }; serviceDescription.UserAuthorizationEndpoint = new MessageReceivingEndpoint(new Uri(authorizationTokenURL), HttpDeliveryMethods.PostRequest); TokenManager tokenManager = new TokenManager(consumerKey, consumerSecret); WebConsumer consumer = new WebConsumer(serviceDescription, tokenManager); // callback is never called by CLM, but needed to do OAuth based forms login // XXX - Dns.GetHostName() alway seems to return simple, uppercased hostname string callback = "https://" + Dns.GetHostName() + '.' + IPGlobalProperties.GetIPGlobalProperties().DomainName + ":9443/cb"; callback = callback.ToLower(); consumer.PrepareRequestUserAuthorization(new Uri(callback), null, null); OslcClient oslcClient = new OslcClient(); HttpClient client = oslcClient.GetHttpClient(); HttpStatusCode statusCode = HttpStatusCode.Unused; string location = null; HttpResponseMessage resp; try { client.DefaultRequestHeaders.Clear(); resp = client.GetAsync(authorizationTokenURL + "?oauth_token=" + tokenManager.GetRequestToken() + "&oauth_callback=" + Uri.EscapeUriString(callback).Replace("#", "%23").Replace("/", "%2F").Replace(":", "%3A")).Result; statusCode = resp.StatusCode; if (statusCode == HttpStatusCode.Found) { location = resp.Headers.Location.AbsoluteUri; resp.ConsumeContent(); statusCode = FollowRedirects(client, statusCode, location); } string securityCheckUrl = "j_username="******"&j_password="******"application/x-www-form-urlencoded"); mediaTypeValue.CharSet = "utf-8"; content.Headers.ContentType = mediaTypeValue; resp = client.PostAsync(authUrl + "/j_security_check", content).Result; statusCode = resp.StatusCode; string jazzAuthMessage = null; IEnumerable <string> values = new List <string>(); if (resp.Headers.TryGetValues(JAZZ_AUTH_MESSAGE_HEADER, out values)) { jazzAuthMessage = values.Last(); } if (jazzAuthMessage != null && string.Compare(jazzAuthMessage, JAZZ_AUTH_FAILED, true) == 0) { resp.ConsumeContent(); throw new JazzAuthFailedException(user, authUrl); } else if (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.Found) { resp.ConsumeContent(); throw new JazzAuthErrorException(statusCode, authUrl); } else //success { Uri callbackUrl = resp.Headers.Location; resp = client.GetAsync(callbackUrl.AbsoluteUri).Result; callbackUrl = resp.Headers.Location; resp = client.GetAsync(callbackUrl.AbsoluteUri).Result; callbackUrl = resp.Headers.Location; NameValueCollection qscoll = callbackUrl.ParseQueryString(); if (callbackUrl.OriginalString.StartsWith(callback + '?') && qscoll["oauth_verifier"] != null) { DesktopConsumer desktopConsumer = new DesktopConsumer(serviceDescription, tokenManager); AuthorizedTokenResponse authorizedTokenResponse = desktopConsumer.ProcessUserAuthorization(tokenManager.GetRequestToken(), qscoll["oauth_verifier"]); return(consumer.CreateAuthorizingHandler(authorizedTokenResponse.AccessToken, CreateSSLHandler())); } throw new JazzAuthErrorException(statusCode, authUrl); } } catch (JazzAuthFailedException jfe) { throw jfe; } catch (JazzAuthErrorException jee) { throw jee; } catch (Exception e) { Console.WriteLine(e.StackTrace); } // return consumer.CreateAuthorizingHandler(accessToken); return(null); }