public void UploadResult(int?id, IFormCollection Form) { TestRun testRun = _context.TestRun.Single(t => t.TestRunID == id); Test test = _context.Test.Single(t => t.TestID == testRun.TestID); var TestReportContents = Form.Single(t => t.Key == "TestReportDetails").Value; TestReportDetails testReportDetails = JsonConvert.DeserializeObject <TestReportDetails>(TestReportContents); var DateTimeForFilename = StringClass.sanitiseDateTimeStringForFilename(testReportDetails.strStartTime); var frameworkLogDirectory = Path.Combine("TestRunnerLogs", DateTimeForFilename); Directory.CreateDirectory(frameworkLogDirectory); var frameworkLogFilePath = Path.Combine("TestRunnerLogs", DateTimeForFilename, Form.First().Key); System.IO.File.WriteAllText(frameworkLogFilePath, Form.First().Value); AddTestRunnerLog(frameworkLogFilePath, Form.First().Key, testRun.TestRunner.Value, DateTimeForFilename); var resultDirectory = Path.Combine(strResultsDirectory, testRun.TestRunID.ToString(), DateTimeForFilename); Directory.CreateDirectory(resultDirectory); string screenshotList = Form.Single(t => t.Key == "ListOfScreenshots").Value; List <ScreenshotDetails> ListOfScreenshotDetails = JsonConvert.DeserializeObject <List <ScreenshotDetails> >(screenshotList); var screenshotFolder = Path.Combine(strScreenshotsDirectory, testRun.TestRunID.ToString(), DateTimeForFilename); Directory.CreateDirectory(screenshotFolder); var ResultID = AddResultOfID(testRun, resultDirectory, screenshotFolder, testReportDetails, test, testRun.TestEnvironmentID.Value); string stepDetailsList = Form.Single(t => t.Key == "ListOfStepDetails").Value; List <StepDetails> ListOfStepDetails = JsonConvert.DeserializeObject <List <StepDetails> >(stepDetailsList); StoreStepDetailsList(ListOfStepDetails, ResultID); StoreScreenshotDetailsList(ListOfScreenshotDetails, screenshotFolder, ResultID); foreach (var item in ListOfScreenshotDetails) { string imageString = Form.Single(t => t.Key == item.strStepID).Value; byte[] imageByteArray = JsonConvert.DeserializeObject <byte[]>(imageString); var screenshotFilePath = Path.Combine(screenshotFolder, item.strStepID); System.IO.File.WriteAllBytes(screenshotFilePath + ".png", imageByteArray); } }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); string appId, kind, username, @params, authRule, year, number, uniqueId, token, user; appId = kind = username = @params = authRule = year = number = uniqueId = token = user = string.Empty; try { _logger.WriteDebug(new LogMessage("GrantResourceOwnerCredentials"), LogCategories); IFormCollection forms = await context.OwinContext.Request.ReadFormAsync(); appId = forms.Single(f => f.Key == "appId").Value.Single(); _logger.WriteDebug(new LogMessage(string.Concat("AppID-> ", appId)), LogCategories); kind = forms.Single(f => f.Key == "kind").Value.Single(); _logger.WriteDebug(new LogMessage(string.Concat("Kind-> ", kind)), LogCategories); username = forms.Single(f => f.Key == "username").Value.Single(); _logger.WriteDebug(new LogMessage(string.Concat("Username-> ", username)), LogCategories); authRule = forms.Single(f => f.Key == "authRule").Value.Single(); _logger.WriteDebug(new LogMessage(string.Concat("AuthRule-> ", authRule)), LogCategories); @params = forms.Single(f => f.Key == "params").Value.SingleOrDefault(); _logger.WriteDebug(new LogMessage(string.Concat("Params-> ", @params)), LogCategories); if (kind == "Protocol") { string[] keys = @params.Split('|'); year = keys.Single(f => f.StartsWith("year=")).Split('=').Last(); number = keys.Single(f => f.StartsWith("number=")).Split('=').Last(); uniqueId = _unitOfWork.Repository <Protocol>().GetByCompositeKey(short.Parse(year), int.Parse(number)).Single().UniqueId.ToString(); } if (kind == "Protocol" && authRule == VALUE_ExternalViewer_AuthenticationRule_Token) { string[] keys = @params.Split('|'); token = keys.Single(f => f.StartsWith("token=")).Split('=').Last(); user = keys.Single(f => f.StartsWith("user="******"The AppId '{appId}' is not valid."), LogCategories); context.SetError("invalid_grant", $"The AppId {appId} is not valid."); context.Rejected(); return; } if (string.IsNullOrEmpty(kind)) { _logger.WriteWarning(new LogMessage($"The Kind '{kind}' is not valid."), LogCategories); context.SetError("invalid_grant", "The Kind is not valid."); context.Rejected(); return; } if (string.IsNullOrEmpty(username) || !ValidIntegrations.Any(f => f.Key == username)) { _logger.WriteWarning(new LogMessage($"The Username '{username}' is not valid."), LogCategories); context.SetError("invalid_grant", $"The Username {username} is not valid."); context.Rejected(); return; } if (string.IsNullOrEmpty(authRule)) { _logger.WriteWarning(new LogMessage($"The AuthRule '{authRule}' is not valid."), LogCategories); context.SetError("invalid_grant", "The AuthRule is not valid."); context.Rejected(); return; } currentIntegration = ValidIntegrations[username]; if (currentIntegration.Key != authenticationId || currentIntegration.Value != authRule) { _logger.WriteWarning(new LogMessage($"{username}: The configuration is not valid."), LogCategories); context.SetError("invalid_grant", "The configuration is not valid."); context.Rejected(); return; } if (kind == "Protocol" && authRule == VALUE_ExternalViewer_AuthenticationRule_OAuth2 && (string.IsNullOrEmpty(year) || string.IsNullOrEmpty(number))) { _logger.WriteWarning(new LogMessage($"The protocols oauth2 params {year}/{number} is not valid."), LogCategories); context.SetError("invalid_grant", "The protocols oauth2 params is not valid."); context.Rejected(); return; } if (kind == "Protocol" && authRule == VALUE_ExternalViewer_AuthenticationRule_Token && (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(token) || string.IsNullOrEmpty(year) || string.IsNullOrEmpty(number))) { _logger.WriteWarning(new LogMessage($"The protocols token params {user}/{token}/{year}/{number} is not valid."), LogCategories); context.SetError("invalid_grant", "The protocols token params is not valid."); context.Rejected(); return; } if (kind == "Protocol" && authRule == VALUE_ExternalViewer_AuthenticationRule_Token) { string topicName = _messageConfiguration.GetConfigurations()["EventTokenSecurity"].TopicName; if (!await _topicService.SubscriptionExists(topicName, token)) { _logger.WriteWarning(new LogMessage($"AppId {appId} has no valid token {authenticationId}"), LogCategories); context.SetError($"AppId {appId} has not validate token {authenticationId}", "The protocols params is not valid."); context.Rejected(); return; } ServiceBusMessage serviceBusMessage = (await _topicService.GetMessagesAsync(topicName, token)).FirstOrDefault(); IEventTokenSecurity tokenSecurityModel = null; if (serviceBusMessage == null || (tokenSecurityModel = (IEventTokenSecurity)serviceBusMessage.Content) == null) { _logger.WriteWarning(new LogMessage($"AppId {appId} has no valid token"), LogCategories); context.SetError($"AppId {appId} has not validate token {token}", "The protocols params is not valid."); context.Rejected(); return; } if (tokenSecurityModel.ContentType.ContentTypeValue.DocumentUnitAuhtorized.UniqueId.ToString() != uniqueId) { _logger.WriteWarning(new LogMessage($"AppId {appId} token is not valid for protocol {uniqueId}-{year}/{number}"), LogCategories); context.SetError($"AppId {appId} token is not valid for protocol {uniqueId}-{year}/{number}", "The protocols params is not valid."); context.Rejected(); return; } Guid secureToken = tokenSecurityModel.ContentType.ContentTypeValue.Token; _logger.WriteDebug(new LogMessage($"SecureToken is {secureToken}"), LogCategories); } ClaimsIdentity identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim(CLAIM_ExternalViewer_OAuth2_Kind, kind, ClaimValueTypes.String, ClaimsIdentity.DefaultIssuer, string.Empty)); identity.AddClaim(new Claim(CLAIM_ExternalViewer_AuthenticationRule, authRule, ClaimValueTypes.String, ClaimsIdentity.DefaultIssuer, string.Empty)); if (kind == "Protocol") { identity.AddClaim(new Claim(CLAIM_ExternalViewer_OAuth2_Year, year, ClaimValueTypes.String, ClaimsIdentity.DefaultIssuer, string.Empty)); identity.AddClaim(new Claim(CLAIM_ExternalViewer_OAuth2_Number, number, ClaimValueTypes.String, ClaimsIdentity.DefaultIssuer, string.Empty)); identity.AddClaim(new Claim(CLAIM_ExternalViewer_OAuth2_UniqueId, uniqueId, ClaimValueTypes.String, ClaimsIdentity.DefaultIssuer, string.Empty)); if (authRule == VALUE_ExternalViewer_AuthenticationRule_Token) { identity.AddClaim(new Claim(CLAIM_ExternalViewer_Token_Hash, token, ClaimValueTypes.String, ClaimsIdentity.DefaultIssuer, string.Empty)); identity.AddClaim(new Claim(CLAIM_ExternalViewer_Token_User, user, ClaimValueTypes.String, ClaimsIdentity.DefaultIssuer, string.Empty)); } } context.Validated(identity); }
public void UploadResult(int? id, IFormCollection Form) { TestRun testRun = _context.TestRun.Single(t => t.TestRunID == id); Test test = _context.Test.Single(t => t.TestID == testRun.TestID); var TestReportContents = Form.Single(t => t.Key == "TestReportDetails").Value; TestReportDetails testReportDetails = JsonConvert.DeserializeObject<TestReportDetails>(TestReportContents); var DateTimeForFilename = StringClass.sanitiseDateTimeStringForFilename(testReportDetails.strStartTime); var frameworkLogDirectory = Path.Combine("TestRunnerLogs", DateTimeForFilename); Directory.CreateDirectory(frameworkLogDirectory); var frameworkLogFilePath = Path.Combine("TestRunnerLogs", DateTimeForFilename, Form.First().Key); System.IO.File.WriteAllText(frameworkLogFilePath, Form.First().Value); AddTestRunnerLog(frameworkLogFilePath, Form.First().Key, testRun.TestRunner.Value, DateTimeForFilename); var resultDirectory = Path.Combine(strResultsDirectory, testRun.TestRunID.ToString(), DateTimeForFilename); Directory.CreateDirectory(resultDirectory); string screenshotList = Form.Single(t => t.Key == "ListOfScreenshots").Value; List<ScreenshotDetails> ListOfScreenshotDetails = JsonConvert.DeserializeObject<List<ScreenshotDetails>>(screenshotList); var screenshotFolder = Path.Combine(strScreenshotsDirectory, testRun.TestRunID.ToString(), DateTimeForFilename); Directory.CreateDirectory(screenshotFolder); var ResultID = AddResultOfID(testRun, resultDirectory, screenshotFolder, testReportDetails, test, testRun.TestEnvironmentID.Value); string stepDetailsList = Form.Single(t => t.Key == "ListOfStepDetails").Value; List<StepDetails> ListOfStepDetails = JsonConvert.DeserializeObject<List<StepDetails>>(stepDetailsList); StoreStepDetailsList(ListOfStepDetails, ResultID); StoreScreenshotDetailsList(ListOfScreenshotDetails, screenshotFolder, ResultID); foreach (var item in ListOfScreenshotDetails) { string imageString = Form.Single(t => t.Key == item.strStepID).Value; byte[] imageByteArray = JsonConvert.DeserializeObject<byte[]>(imageString); var screenshotFilePath = Path.Combine(screenshotFolder, item.strStepID); System.IO.File.WriteAllBytes(screenshotFilePath + ".png", imageByteArray); } }