public Task Handle(HttpContext httpContext) { string route = httpContext.Request.Path.ToString(); CheckinMessage checkinMessage = GetCheckinMessageFromRequest(httpContext.Request); // If the route is not incldued in the list of valid routes, return a default page if (!State.Profile.Server.URLs.Contains(route) || !checkinMessage.IsValid) { return(DefaultResponse(httpContext)); } FactionMessage factionMessage = new FactionMessage(); if (!String.IsNullOrEmpty(checkinMessage.StagingName)) { factionMessage = _factionAPIHandler.HandleStage(checkinMessage.StagingName, checkinMessage.StagingId, checkinMessage.Message, httpContext.Connection.RemoteIpAddress.ToString()); } else { factionMessage = _factionAPIHandler.HandleBeacon(checkinMessage.AgentName, checkinMessage.Message, httpContext.Connection.RemoteIpAddress.ToString()); } // Convert Faction Message to response as defined by the profile string responseContent = _responseHandler.ProcessFactionMessage(httpContext, factionMessage); if (String.IsNullOrEmpty(responseContent)) { return(DefaultResponse(httpContext)); } return(httpContext.Response.WriteAsync(responseContent)); }
public IHttpActionResult Accept(Guid id) { try { var registration = _registrationRepository.GetById(id); var visitor = _visitorRepository.GetById(registration.Visitor.Id); var eventItem = _eventRepository.GetById(registration.Event.Id); var scan = Scan.Create(DateTime.Now, "Alsnog geaccepteerd", registration); registration.Scans.Add(scan); var body = new CheckinBody() { Name = visitor.Name, City = visitor.City, Email = visitor.Email, NumberOfVisitors = registration.NumberOfVisitors, Postcode = visitor.Postcode, TimeSlot = eventItem.TimeRange.ToString() }; var message = new CheckinMessage() { Status = 200, Description = "Alsnog geaccepteerd" }; var response = new CheckinResponse() { Data = body, Message = message }; registration.Visited = true; _registrationRepository.Update(registration); return(Ok(response)); } catch (Exception) { throw; } }
public void Checkin(CheckinMessage message) { throw new NotImplementedException(); }
public CheckinMessage GetCheckinMessageFromRequest(HttpRequest httpRequest) { // This flag is used to flag this as a valid agent message or not. If we can't // get the info we want out of the request, we assume its bogus and send a // default response instead CheckinMessage checkinMessage = new CheckinMessage(); checkinMessage.IsValid = true; string _bodyContent = ""; // Check cookies against the profile to see if it matches what we're expecting foreach (KeyValuePair <string, string> cookie in State.Profile.Agent.Cookies) { if (httpRequest.Cookies.ContainsKey(cookie.Key)) { if (httpRequest.Cookies[cookie.Key] != cookie.Value) { Logger.LogError($"Request contains the {cookie.Key} cookie, but value {httpRequest.Cookies[cookie.Key]} does not match profile value of {cookie.Value}"); checkinMessage.IsValid = false; } } else { Logger.LogError($"Request does not contain the {cookie.Key} cookie that is specified in the profile"); checkinMessage.IsValid = false; } } // Check headers against the profile to see if it matches what we're expecting foreach (KeyValuePair <string, string> header in State.Profile.Agent.Headers) { if (httpRequest.Headers.ContainsKey(header.Key)) { if (httpRequest.Headers[header.Key] != header.Value) { Logger.LogError($"Request contains the {header.Key} header, but value {httpRequest.Headers[header.Key]} does not match profile value of {header.Value}"); checkinMessage.IsValid = false; } } else { Logger.LogError($"Request does not contain the {header.Key} header that is specified in the profile"); checkinMessage.IsValid = false; } } string encodedId = ""; // Check if this is staging message and get staging info if (State.Profile.Agent.StagingIdentifier.Location == "Header") { if (httpRequest.Headers.ContainsKey(State.Profile.Agent.StagingIdentifier.Name)) { encodedId = httpRequest.Headers[State.Profile.Agent.StagingIdentifier.Name]; } } else if (State.Profile.Agent.StagingIdentifier.Location == "Cookie") { if (httpRequest.Cookies.ContainsKey(State.Profile.Agent.StagingIdentifier.Name)) { encodedId = httpRequest.Cookies[State.Profile.Agent.StagingIdentifier.Name]; } } if (!String.IsNullOrEmpty(encodedId)) { string decodedId = Encoding.UTF8.GetString(Convert.FromBase64String(encodedId)); checkinMessage.StagingName = decodedId.Split(':')[0]; checkinMessage.StagingId = decodedId.Split(':')[1]; } // Get AgentName if (State.Profile.Agent.CheckinIdentifier.Location == "Cookie") { checkinMessage.AgentName = httpRequest.Cookies[State.Profile.Agent.CheckinIdentifier.Name]; } else if (State.Profile.Agent.CheckinIdentifier.Location == "Header") { checkinMessage.AgentName = httpRequest.Headers[State.Profile.Agent.CheckinIdentifier.Name]; } if (String.IsNullOrEmpty(checkinMessage.AgentName) && (String.IsNullOrEmpty(checkinMessage.StagingName))) { Logger.LogError("Got request that we can't find an AgentName or StagingName in. Sending wwwroot/index.html in response."); checkinMessage.IsValid = false; } // Get Message from Agent if (checkinMessage.IsValid && httpRequest.Method.ToLower() == "post") { // Get text from body of request if (httpRequest.Body != null) { using (Stream receiveStream = httpRequest.Body) { using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8)) { _bodyContent = readStream.ReadToEnd(); } } if (_bodyContent == State.Profile.Agent.MessageConfig.Default) { Logger.LogDebug($"Got blank checkin message from agent {checkinMessage.AgentName}"); checkinMessage.Message = _bodyContent; } else if (_bodyContent.StartsWith(State.Profile.Agent.MessageConfig.Prepend)) { checkinMessage.Message = _bodyContent.Remove(0, State.Profile.Agent.MessageConfig.Prepend.Count()); checkinMessage.Message = checkinMessage.Message.Remove(checkinMessage.Message.Length - State.Profile.Agent.MessageConfig.Append.Count()); } else { Logger.LogError("Request was sent via POST but could not get message out of it. Sending wwwroot/index.html in response."); } } else { Logger.LogError($"Got a request without a body. Not a agent message"); checkinMessage.IsValid = false; } } return(checkinMessage); }
public IHttpActionResult Checkin(Guid id) { try { var registration = _registrationRepository.GetById(id); if (registration == null) { var message = new CheckinMessage() { Status = 602, Description = "Onbekende ticket" }; var response = new CheckinResponse() { Data = null, Message = message }; return(Content(HttpStatusCode.BadRequest, response)); } var visitor = _visitorRepository.GetById(registration.Visitor.Id); var eventItem = _eventRepository.GetById(registration.Event.Id); var currentDate = DateTime.Now; if (registration.Visited) { var scan = Scan.Create(DateTime.Now, "Ticket is al gebruikt", registration); registration.Scans.Add(scan); _registrationRepository.Update(registration); var body = new CheckinBody() { Name = visitor.Name, City = visitor.City, Email = visitor.Email, NumberOfVisitors = registration.NumberOfVisitors, Postcode = visitor.Postcode, TimeSlot = eventItem.TimeRange.ToString() }; var message = new CheckinMessage() { Status = 601, Description = "Ticket is al gebruikt" }; var response = new CheckinResponse() { Data = body, Message = message }; return(Content(HttpStatusCode.BadRequest, response)); } if (currentDate >= eventItem.TimeRange.Start && currentDate <= eventItem.TimeRange.End) { var scan = Scan.Create(DateTime.Now, "Geaccepteerd", registration); registration.Scans.Add(scan); var body = new CheckinBody() { Name = visitor.Name, City = visitor.City, Email = visitor.Email, NumberOfVisitors = registration.NumberOfVisitors, Postcode = visitor.Postcode, TimeSlot = eventItem.TimeRange.ToString() }; var message = new CheckinMessage() { Status = 200, Description = "Geaccepteerd" }; var response = new CheckinResponse() { Data = body, Message = message }; registration.Visited = true; _registrationRepository.Update(registration); return(Ok(response)); } else { var scan = Scan.Create(DateTime.Now, "Ticket valt buiten de toegestane timeslot", registration); registration.Scans.Add(scan); _registrationRepository.Update(registration); var body = new CheckinBody() { Name = visitor.Name, City = visitor.City, Email = visitor.Email, NumberOfVisitors = registration.NumberOfVisitors, Postcode = visitor.Postcode, TimeSlot = eventItem.TimeRange.ToString() }; var message = new CheckinMessage() { Status = 600, Description = "Ticket valt buiten de toegestane timeslot" }; var response = new CheckinResponse() { Data = body, Message = message }; return(Content(HttpStatusCode.BadRequest, response)); } } catch (Exception) { throw; } }