public async Task ConnectAsync(ConnectionInformation connection, CancellationToken token) { httpClient = new HttpClient(messageHandler) { BaseAddress = connection.ServerUri, DefaultRequestHeaders = { Authorization = AuthenticationHeaderFactory.Create( connection.UserName, connection.Password, connection.Authentication), }, }; httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent); IsConnected = true; var versionResponse = await InvokeRequestAsync <IGetVersionRequest, string>(token); sonarQubeVersion = Version.Parse(versionResponse); var credentialResponse = await InvokeRequestAsync <IValidateCredentialsRequest, bool>(token); if (!credentialResponse) { IsConnected = false; throw new InvalidOperationException("Invalid credentials"); } }
public void AuthenticationHeaderHelper_GetAuthToken() { // Invalid input string user = "******"; string password = "******"; using (new AssertIgnoreScope()) { Action action = () => AuthenticationHeaderFactory.GetBasicAuthToken(user, password.ToSecureString()); action.ShouldThrow <ArgumentOutOfRangeException>(); } // ASCII user = "******"; password = "******"; AssertAreEqualUserNameAndPassword(user, password, AuthenticationHeaderFactory.GetBasicAuthToken(user, password.ToSecureString())); // UTF-8 user = "******"; // hello in Russian password = "******"; // hello in Chinese AssertAreEqualUserNameAndPassword(user, password, AuthenticationHeaderFactory.GetBasicAuthToken(user, password.ToSecureString())); // Digits and signs (including ':' in the password) user = "******"; password = "******"; AssertAreEqualUserNameAndPassword(user, password, AuthenticationHeaderFactory.GetBasicAuthToken(user, password.ToSecureString())); }
public async Task ConnectAsync(ConnectionInformation connection, CancellationToken token) { logger.Info($"Connecting to '{connection.ServerUri}'."); logger.Debug($"IsConnected is {IsConnected}."); httpClient = new HttpClient(messageHandler) { BaseAddress = connection.ServerUri, DefaultRequestHeaders = { Authorization = AuthenticationHeaderFactory.Create( connection.UserName, connection.Password, connection.Authentication), }, }; httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent); requestFactory = requestFactorySelector.Select(connection.IsSonarCloud, logger); try { // We have to set IsConnected to true here, otherwise the two calls to InvokeRequestAsync // below will fail. However, at this point we don't really know if the server is running // so the web calls could fail. IsConnected = true; var serverTypeDescription = connection.IsSonarCloud ? "SonarCloud" : "SonarQube"; logger.Debug($"Getting the version of {serverTypeDescription}..."); var versionResponse = await InvokeRequestAsync <IGetVersionRequest, string>(token); ServerInfo = new ServerInfo(Version.Parse(versionResponse), connection.IsSonarCloud ? ServerType.SonarCloud : ServerType.SonarQube); logger.Info($"Connected to {serverTypeDescription} '{ServerInfo.Version}'."); logger.Debug($"Validating the credentials..."); var credentialResponse = await InvokeRequestAsync <IValidateCredentialsRequest, bool>(token); if (!credentialResponse) { throw new InvalidOperationException("Invalid credentials"); } logger.Debug($"Credentials accepted."); } catch { IsConnected = false; ServerInfo = null; throw; } }
public async Task Connect(Uri baseAddress, string username, SecureString password) { httpClient.BaseAddress = baseAddress; httpClient.DefaultRequestHeaders.Authorization = AuthenticationHeaderFactory.Create(username, password); connected = true; sonarQubeVersion = await InvokeRequestAsync <IServerVersionRequest, string>( CancellationToken.None); var result = await InvokeRequestAsync <IAuthValidateRequest, AuthenticationResult>( CancellationToken.None); if (!result.IsValid) { throw new InvalidOperationException("Invalid credentials"); } }
public async Task ConnectAsync(ConnectionInformation connection, CancellationToken token) { logger.Info($"Connecting to '{connection.ServerUri}'."); logger.Debug($"IsConnected is {IsConnected}."); httpClient = new HttpClient(messageHandler) { BaseAddress = connection.ServerUri, DefaultRequestHeaders = { Authorization = AuthenticationHeaderFactory.Create( connection.UserName, connection.Password, connection.Authentication), }, }; httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent); IsConnected = true; logger.Debug($"Getting the version of SonarQube..."); var versionResponse = await InvokeRequestAsync <IGetVersionRequest, string>(token); SonarQubeVersion = Version.Parse(versionResponse); logger.Info($"Connected to SonarQube '{SonarQubeVersion}'."); logger.Debug($"Validating the credentials..."); var credentialResponse = await InvokeRequestAsync <IValidateCredentialsRequest, bool>(token); if (!credentialResponse) { IsConnected = false; SonarQubeVersion = null; throw new InvalidOperationException("Invalid credentials"); } logger.Debug($"Credentials accepted."); }
public SonarQubeClient(ConnectionRequest connection, HttpMessageHandler messageHandler, TimeSpan requestTimeout) { if (connection == null) { throw new ArgumentNullException(nameof(connection)); } if (messageHandler == null) { throw new ArgumentNullException(nameof(messageHandler)); } if (requestTimeout <= TimeSpan.Zero) { throw new ArgumentException("Doesn't expect a zero or negative timeout.", nameof(requestTimeout)); } this.httpClient = new HttpClient(messageHandler) { BaseAddress = connection.ServerUri, Timeout = requestTimeout }; this.httpClient.DefaultRequestHeaders.Authorization = AuthenticationHeaderFactory.Create(connection); }
public async Task <IEnumerable <Image> > GetImagesAsync() { var newImageBag = new ConcurrentBag <Image>(); var imageIds = new ConcurrentBag <string>(); Logger.LogDebug("Start loading images cache"); if (Auth == null) { Logger.LogDebug("Loading auth"); Auth = AuthenticationHeaderFactory.GetAuthenticationHeaderAsync().Result; } ImageSearchResult result = null; var url = $"{ImageSearchConfig.BaseUrl}/{ImageSearchConfig.ImageSearchPath}"; try { result = await RestHttpProxy.GetWebRequestAsync <ImageSearchResult>(new Uri(url), new RequestConfiguration { AuthenticationValue = Auth }); } catch (UnauthorizedException) { Auth = AuthenticationHeaderFactory.GetAuthenticationHeaderAsync().Result; result = await RestHttpProxy.GetWebRequestAsync <ImageSearchResult>(new Uri(url), new RequestConfiguration { AuthenticationValue = Auth }); } Logger.LogDebug($"Getting image ids. Total pages: {result.PageCount}"); this.AddImageIds(imageIds, result); var getImageIdsContainerTasks = new List <Task>(); for (int i = 1; i < result.PageCount; i++) { getImageIdsContainerTasks.Add(Task.Run(async() => { var currentUrl = $"{url}?page={i}"; result = await RestHttpProxy.GetWebRequestAsync <ImageSearchResult>(new Uri(url), new RequestConfiguration { AuthenticationValue = Auth }); AddImageIds(imageIds, result); })); } await Task.WhenAll(getImageIdsContainerTasks); Logger.LogDebug($"Getting image details"); var getImageDetailContainerTasks = new List <Task>(); foreach (var imageId in imageIds) { getImageDetailContainerTasks.Add(Task.Run(async() => { var image = await RestHttpProxy.GetWebRequestAsync <Image>(new Uri($"{ImageSearchConfig.BaseUrl}/{ImageSearchConfig.ImageSearchPath}/{imageId}"), new RequestConfiguration { AuthenticationValue = Auth }); newImageBag.Add(image); })); } await Task.WhenAll(getImageDetailContainerTasks); Logger.LogDebug($"Images cache loaded"); return(newImageBag); }