private async Task ValidateUser(IOwinContext context) { string authHeader = context.Request.Headers["Authorization"]; if (authHeader != null && authHeader.StartsWith("Basic")) { string encodedUsernamePassword = authHeader.Substring("Basic ".Length).Trim(); var encoding = Encoding.GetEncoding("iso-8859-1"); string usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword)); int seperatorIndex = usernamePassword.IndexOf(':'); var username = usernamePassword.Substring(0, seperatorIndex); var password = usernamePassword.Substring(seperatorIndex + 1); if (!await _smsDao.IsAuthorizedUserAsync(username, password)) { throw new AuthorizationException($"User '{username}' is not authorized to access the api."); } UserProfileProvider.SetUserProfile(new UserProfile(username)); } else { throw new AuthorizationException("The authorization header is either empty or is not Basic."); } }
/// <summary> /// Process Records /// </summary> protected override void ProcessRecord() { var networkCredential = Credential.GetNetworkCredential(); var domain = string.IsNullOrWhiteSpace(networkCredential.Domain) ? LocalDomain : networkCredential.Domain; var credentials = new AccessTokenCredential { Domain = domain, Username = networkCredential.UserName, Password = networkCredential.Password }; var httpRequest = new HttpRequestMessage(HttpMethod.Post, "public/accessTokens") { Content = new StringContent(JsonConvert.SerializeObject(credentials), Encoding.UTF8, "application/json") }; try { var httpClient = Session.ApiClient.BuildClient(clusterUri, true); var response = httpClient.SendAsync(httpRequest).Result; var responseContent = response.Content.ReadAsStringAsync().Result; if (response.StatusCode != HttpStatusCode.Created) { var error = JsonConvert.DeserializeObject <Error>(responseContent); StringBuilder sb = new StringBuilder(); sb.AppendLine("Failed to connect to the Cohesity Cluster"); sb.AppendLine(error.Message); throw new Exception(sb.ToString()); } var accessToken = JsonConvert.DeserializeObject <AccessTokenObject>(responseContent); var userProfile = new UserProfile { ClusterUri = clusterUri, AccessToken = accessToken, AllowInvalidServerCertificates = true }; userProfileProvider.SetUserProfile(userProfile); WriteObject($"Connected to the Cohesity Cluster {Server} Successfully"); } catch (AggregateException ex) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Failed to connect to the Cohesity Cluster"); foreach (Exception exInnerException in ex.Flatten().InnerExceptions) { Exception exNestedInnerException = exInnerException; do { if (!string.IsNullOrEmpty(exNestedInnerException.Message)) { sb.AppendLine(exNestedInnerException.Message); } exNestedInnerException = exNestedInnerException.InnerException; } while (exNestedInnerException != null); } throw new Exception(sb.ToString()); } catch (Exception ex) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Failed to connect to the Cohesity Cluster"); sb.AppendLine(ex.Message); throw new Exception(sb.ToString()); } }