/// <summary> /// Validate the App Deletion field values, and call deletion services /// </summary> /// <param name="request">Values from POST request</param> /// <returns>Http status code and message</returns> public HttpResponseContent ValidateDeletion(ApplicationRequest request) { // Http status code and message HttpResponseContent response; // Validate deletion request values if (request.Title == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Title"); return(response); } else if (!IsValidEmail(request.Email)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Email"); return(response); } using (var _db = new DatabaseContext()) { // Attempt to find application var app = ApplicationService.GetApplication(_db, request.Title, request.Email); if (app == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Application"); return(response); } // Attempt to create an apiKey record var appResponse = ApplicationService.DeleteApplication(_db, app.Id); if (appResponse == null) { response = new HttpResponseContent(HttpStatusCode.InternalServerError, "Unable to delete application."); return(response); } List <object> responses = new List <object>(); responses.Add(appResponse); // Save database changes if (!SaveChanges(_db, responses)) { response = new HttpResponseContent(HttpStatusCode.InternalServerError, "Unable to save database changes"); return(response); } // Successful deletion response = new HttpResponseContent(HttpStatusCode.OK, "Application Deleted from KFC SSO"); return(response); } }
/// <summary> /// Validate the App Deletion request values, and call deletion services /// </summary> /// <param name="request">Values from App Deletion POST request</param> /// <returns>Http status code and content</returns> public HttpResponseContent ValidateDeletion(ApplicationRequest request) { HttpResponseContent response; // Body of http response content // Validate deletion request values if (request.Title == null) { throw new InvalidStringException("Invalid Title: Null"); } else if (!IsValidEmail(request.Email)) { throw new InvalidEmailException("Invalid Email Format"); } // Attempt to find application var app = _applicationService.GetApplication(request.Title, request.Email); if (app == null) { throw new ArgumentException("Application Does Not Exist"); } // Attempt to delete application var appResponse = _applicationService.DeleteApplication(app.Id); if (appResponse == null) { throw new ArgumentException("Application Could Not Be Deleted"); } // Save data store changes SaveChanges(appResponse); string message; // Email application deletion confirmation try { SendAppDeleteEmail(app.Email, app.Title); message = "Application Deleted. An email has been sent confirming your deletion."; } catch { message = "Application Deleted. A confirmation email was unable to send."; } // Return successful deletion response response = new HttpResponseContent(message); return(response); }
public HttpResponseContent ValidateUpdate(ApplicationRequest request) { // Http status code and message HttpResponseContent response; // Attempt to find application var app = _applicationService.GetApplication(request.Title, request.Email); if (app == null) { response = new HttpResponseContent("Invalid Application"); response.Code = HttpStatusCode.BadRequest; return(response); } // Update click count of application record app.ClickCount = request.ClickCount; var appResponse = _applicationService.UpdateApplication(app); // Attempt to save database changes try { SaveChanges(appResponse); } catch { // Error response response = new HttpResponseContent("Unable to save database changes"); response.Code = HttpStatusCode.InternalServerError; return(response); } // Successful publish response = new HttpResponseContent("Updated application from SSO"); response.Code = HttpStatusCode.OK; return(response); }
/// <summary> /// Validate the app registration field values, and call registration services /// </summary> /// <param name="request">Values from POST request</param> /// <returns>Http status code and message</returns> public HttpResponseContent ValidateRegistration(ApplicationRequest request) { // Http status code and message HttpResponseContent response; if (request == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Request."); return(response); } Uri launchUrl = null; Uri deleteUrl = null; // Validate request values if (request.Title == null || !IsValidStringLength(request.Title, titleLength)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Title: Cannot be more than 100 characters in length."); return(response); } else if (request.Email == null || !IsValidEmail(request.Email)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Email"); return(response); } else if (request.LaunchUrl == null || !IsValidUrl(request.LaunchUrl, ref launchUrl) || !IsValidStringLength(request.LaunchUrl, urlLength)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Application Url"); return(response); } else if (request.DeleteUrl == null || !IsValidUrl(request.DeleteUrl, ref deleteUrl) || !IsValidStringLength(request.DeleteUrl, urlLength)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid User Deletion Url"); return(response); } // Create a new application Application app = new Application { Title = request.Title, LaunchUrl = launchUrl.ToString(), Email = request.Email, UserDeletionUrl = request.DeleteUrl, SharedSecretKey = _tokenService.GenerateToken() }; // Create a new ApiKey ApiKey apiKey = new ApiKey { // Generate a unique key Key = _tokenService.GenerateToken(), ApplicationId = app.Id }; using (var _db = new DatabaseContext()) { // Attempt to create an Application record var appResponse = ApplicationService.CreateApplication(_db, app); if (appResponse == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Application Already Exists"); return(response); } // Attempt to create an ApiKey record var keyResponse = ApiKeyService.CreateKey(_db, apiKey); // Keep generating a new key until a unique one is made. while (keyResponse == null) { apiKey.Key = _tokenService.GenerateToken(); keyResponse = ApiKeyService.CreateKey(_db, apiKey); } List <object> responses = new List <object>(); responses.Add(appResponse); responses.Add(keyResponse); // Save database changes if (!SaveChanges(_db, responses)) { response = new HttpResponseContent(HttpStatusCode.InternalServerError, "Unable to save database changes"); return(response); } } // Attempt to send api key to application email //if (SendAppRegistrationApiKeyEmail(app.Email, apiKey.Key)) //{ // // Alert front end that email was sent // string message = "Sent to " + app.Email; // response = new HttpResponseContent(HttpStatusCode.OK, message); //} //else //{ // // Email could not be sent. Send api key to frontend. // response = new HttpResponseContent(HttpStatusCode.OK, apiKey.Key, app.SharedSecretKey); //} // Return success messge response = new HttpResponseContent(HttpStatusCode.OK, apiKey.Key, app.SharedSecretKey); return(response); }
/// <summary> /// Validate the key generation field values, and call key generation services /// </summary> /// <param name="request">Values from POST request</param> /// <returns>Http status code and message</returns> public HttpResponseContent ValidateKeyGeneration(ApplicationRequest request) { // Http status code and message HttpResponseContent response; if (request == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Request."); return(response); } // Validate key generation request values if (request.Title == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Title"); return(response); } else if (request.Email == null || !IsValidEmail(request.Email)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Email"); return(response); } using (var _db = new DatabaseContext()) { // Attempt to find application var app = ApplicationService.GetApplication(_db, request.Title, request.Email); if (app == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Application"); return(response); } // Create a new ApiKey ApiKey apiKey = new ApiKey { Key = _tokenService.GenerateToken(), ApplicationId = app.Id }; // Invalidate old unused api key var keyOld = ApiKeyService.GetKey(_db, app.Id, false); if (keyOld != null) { keyOld.IsUsed = true; keyOld = ApiKeyService.UpdateKey(_db, keyOld); } // Attempt to create an apiKey record var keyResponse = ApiKeyService.CreateKey(_db, apiKey); // Keep generating a new key until a unique one is made. while (keyResponse == null) { apiKey.Key = _tokenService.GenerateToken(); keyResponse = ApiKeyService.CreateKey(_db, apiKey); } List <object> responses = new List <object>(); responses.Add(keyResponse); responses.Add(keyOld); // Save database changes if (!SaveChanges(_db, responses)) { response = new HttpResponseContent(HttpStatusCode.InternalServerError, "Unable to save database changes"); return(response); } string message = apiKey.Key; // TODO: Set up email server to implement email services //string message; //// Attempt to send api key to application email //if (SendNewApiKeyEmail(app.Email, apiKey.Key)) //{ // // Alert front end that email was sent // message = "Sent to " + app.Email; //} //else //{ // // Email could not be sent. Send api key to frontend. // message = apiKey.Key; //} response = new HttpResponseContent(HttpStatusCode.OK, apiKey.Key); return(response); } }
/// <summary> /// Validate the app publish field values, and call publish services /// </summary> /// <param name="request">Values from POST request</param> /// <returns>Http status code and message</returns> public HttpResponseContent ValidatePublish(ApplicationRequest request) { // Http status code and message HttpResponseContent response; if (request == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Request."); return(response); } Uri logoUrl = null; // Validate publish request values if (request.Title == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Title."); return(response); } else if (!IsValidStringLength(request.Description, descriptionLength)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Description: Cannot be more than 2000 characters in length."); return(response); } else if (!IsValidUrl(request.LogoUrl, ref logoUrl)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Logo Url."); return(response); } else if (!IsValidImageExtension(logoUrl, imageType)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Logo Image Extension: Can only be .PNG"); return(response); } else if (!IsValidDimensions(logoUrl, xDimension, yDimension)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Logo Dimensions: Can be no more than 55x55 pixels."); return(response); } using (var _db = new DatabaseContext()) { // Attempt to find api key var apiKey = ApiKeyService.GetKey(_db, request.Key); // Key must exist and be unused. if (apiKey == null || apiKey.IsUsed == true) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Key"); return(response); } // Attempt to get application based on ApplicationId from api key var app = ApplicationService.GetApplication(_db, apiKey.ApplicationId); // Published application title is used to authenticate the app. if (app == null || !request.Title.Equals(app.Title)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Key"); return(response); } // Update values of application record app.Description = request.Description; app.LogoUrl = request.LogoUrl; app.UnderMaintenance = request.UnderMaintenance; var appResponse = ApplicationService.UpdateApplication(_db, app); // Update values of api key record apiKey.IsUsed = true; var keyResponse = ApiKeyService.UpdateKey(_db, apiKey); List <object> responses = new List <object>(); responses.Add(appResponse); responses.Add(keyResponse); // Attempt to save database changes if (!SaveChanges(_db, responses)) { // Error response response = new HttpResponseContent(HttpStatusCode.InternalServerError, "Unable to save database changes"); return(response); } // Successful publish response = new HttpResponseContent(HttpStatusCode.OK, "Published to KFC SSO"); return(response); } }
/// <summary> /// Validate the app registration request values, and call registration services /// </summary> /// <param name="request">Values from Register POST request</param> /// <returns>Http status code and content</returns> public HttpResponseContent ValidateRegistration(ApplicationRequest request) { HttpResponseContent response; // Body of http response content Uri launchUrl = null; Uri deleteUrl = null; Uri healthCheckUrl = null; Uri logoutUrl = null; // Validate request values if (request.Title == null || !IsValidStringLength(request.Title, titleLength)) { throw new InvalidStringException("Invalid Title: Length cannot be greater than " + titleLength + " characters"); } else if (request.Email == null || !IsValidEmail(request.Email)) { throw new InvalidEmailException("Invalid Email Format"); } else if (request.LaunchUrl == null || !IsValidUrl(request.LaunchUrl, ref launchUrl) || !IsValidStringLength(request.LaunchUrl, urlLength)) { throw new InvalidUrlException("Invalid Launch Url Format"); } else if (request.DeleteUrl == null || !IsValidUrl(request.DeleteUrl, ref deleteUrl) || !IsValidStringLength(request.DeleteUrl, urlLength)) { throw new InvalidUrlException("Invalid User Deletion Url Format"); } else if (request.HealthCheckUrl == null || !IsValidUrl(request.HealthCheckUrl, ref healthCheckUrl) || !IsValidStringLength(request.HealthCheckUrl, urlLength)) { throw new InvalidUrlException("Invalid Health Check Url Format"); } else if (request.LogoutUrl == null || !IsValidUrl(request.LogoutUrl, ref logoutUrl) || !IsValidStringLength(request.LogoutUrl, urlLength)) { throw new InvalidUrlException("Invalid Logout Url Format"); } // Create application from request data Application app = new Application { Title = request.Title, LaunchUrl = launchUrl.ToString(), Email = request.Email, UserDeletionUrl = deleteUrl.ToString(), HealthCheckUrl = healthCheckUrl.ToString(), SharedSecretKey = _tokenService.GenerateToken(), LogoutUrl = logoutUrl.ToString() }; // Create a new api key for application ApiKey apiKey = new ApiKey { // Generate a unique key Key = _tokenService.GenerateToken(), ApplicationId = app.Id }; // Add the api key to the application app.ApiKeys.Add(apiKey); // Create an Application entry var appResponse = _applicationService.CreateApplication(app); if (appResponse == null) // Application was not created { throw new ArgumentException("Application Already Exists."); } // Save data store changes SaveChanges(appResponse); string message; // Email the application ID, api key, and shared secret key try { SendAppRegistrationEmail(app.Email, apiKey.Key, app.SharedSecretKey, app.Id); message = "Successful Registration! An email has also been sent containing the following information."; } catch { message = "Successful Registration! Please save the following information. An email containing this information was unable to send."; } // Return success response response = new HttpResponseContent(message, apiKey.Key, app.SharedSecretKey, app.Id); return(response); }
/// <summary> /// Validate the key generation request values, and call key generation services /// </summary> /// <param name="request">Values from Key Generation POST request</param> /// <returns>Http status code and content</returns> public HttpResponseContent ValidateKeyGeneration(ApplicationRequest request) { HttpResponseContent response; // Body of http response content // Validate key generation request values if (request.Title == null) { throw new InvalidStringException("Invalid Title: Null"); } else if (request.Email == null || !IsValidEmail(request.Email)) { throw new InvalidEmailException("Invalid Email Format"); } // Attempt to find application var app = _applicationService.GetApplication(request.Title, request.Email); if (app == null) { throw new ArgumentException("Application Does Not Exist"); } // Create a new ApiKey ApiKey apiKey = new ApiKey { Key = _tokenService.GenerateToken(), ApplicationId = app.Id }; // Invalidate old api keys for (int i = 0; i < app.ApiKeys.Count; i++) { if (!app.ApiKeys.ElementAt(i).IsUsed) { app.ApiKeys.ElementAt(i).IsUsed = true; } } // Add new api key to application app.ApiKeys.Add(apiKey); // Update the application with the changed keys var appResponse = _applicationService.UpdateApplication(app); // Application was not found if (appResponse == null) { throw new ArgumentException("Application Could Not Be Updated"); } // Save data store changes SaveChanges(appResponse); string message; // Email the new api key try { SendNewApiKeyEmail(app.Email, apiKey.Key); message = "Successful Key Generation! An email has been sent containing your new API Key."; } catch { message = "Successful Key Generation! Please save the following information. An email containing this information was unable to send."; } // Return successful key generation response response = new HttpResponseContent(message, apiKey.Key); return(response); }
/// <summary> /// Validate the app publish request values, and call publish services /// </summary> /// <param name="request">Values from Publish POST request</param> /// <returns>Http status code and content</returns> public HttpResponseContent ValidatePublish(ApplicationRequest request) { HttpResponseContent response; // Body of http response content Uri logoUrl = null; // Validate publish request values if (request.Title == null) { throw new InvalidStringException("Invalid Title: Null"); } else if (!IsValidStringLength(request.Description, descriptionLength)) { throw new InvalidStringException("Invalid Description: Length cannot be greater than " + descriptionLength + " characters."); } else if (!IsValidUrl(request.LogoUrl, ref logoUrl) || !IsValidStringLength(request.LogoUrl, urlLength)) { throw new InvalidUrlException("Invalid Logo Url Format"); } else if (!IsValidImageExtension(logoUrl, imageType)) { throw new InvalidImageException("Invalid Logo Image Extension: Can only be " + imageType); } else if (!IsValidDimensions(logoUrl, xDimension, yDimension)) { throw new InvalidImageException("Invalid Logo Dimensions: Can be no more than " + xDimension + "x" + yDimension + " pixels."); } // Attempt to find api key var apiKey = _apiKeyService.GetKey(request.Key); // Key does not exist if (apiKey == null) { throw new InvalidApiKeyException("Invalid API Key: Key Not Found"); } // Key is found, but used else if (apiKey.IsUsed) { throw new InvalidApiKeyException("Invalid API Key: Key Has Already Been Used"); } // Attempt to find application that api key belongs to var app = _applicationService.GetApplication(apiKey.ApplicationId); // App does not exist, or does not match the api key if (app == null || app.Title != request.Title) { throw new InvalidApiKeyException("Invalid API Key: Key and Application Do Not Match"); } // Update values of application app.Description = request.Description; app.LogoUrl = request.LogoUrl; app.UnderMaintenance = request.UnderMaintenance; // Iterate through applications's api keys for (int i = 0; i < app.ApiKeys.Count; i++) { // Found api key belongs to application's collection of api keys if (app.ApiKeys.ElementAt(i).Id.Equals(apiKey.Id)) { // Invalidate api key app.ApiKeys.ElementAt(i).IsUsed = true; // Update values of application record var appResponse = _applicationService.UpdateApplication(app); // Application was not found if (appResponse == null) { throw new ArgumentException("Application Could Not Be Updated"); } // Save data store changes SaveChanges(appResponse); string message; // Email successful publish confirmation try { SendAppPublishEmail(app.Email, app); message = "Successful Publish to the KFC SSO portal! An email has been sent confirming your publish."; } catch { message = "Successful Publish to the KFC SSO portal! A confirmation email was unable to send."; } // Return successful publish response response = new HttpResponseContent(message); return(response); } } // Key does not exist throw new InvalidApiKeyException("Invalid API Key: Key Not Found"); }