public async Task <CreateResponse> Create([FromBody] CreateRequest request) { // Security: Request will only be allowed if "RequireTokenForGet" is false OR request.AuthToken is in list or in database if (_config.Security.RequireTokenForGet // Not in allowed list in config && _config.Security.AuthenticationTokens != null && !_config.Security.AuthenticationTokens.Contains(request.AuthToken) // And not in database && !_dbContext.AuthTokens.Any(w => w.AuthToken == request.AuthToken && w.CanCreate) ) { // Not accepted return(new CreateResponse() { Success = false, ErrorMessage = "Invalid authentication token" }); } // Create item var urlItem = new UrlItem() { Key = Helpers.GetRandomKey(_config.Url), Expires = request.Expires, Url = request.Url, Metadata = request.Metadata }; try { await _shortUrlService.CreateAsync(urlItem); } catch (Exception ex) { _logger.LogError(ex, $"API: Creating short url for: {request.Url}"); return(new CreateResponse() { Success = false, ErrorMessage = "Error: Have admin check logs for more information." }); } return(new CreateResponse() { Success = true, Key = urlItem.Key, ShortUrl = Helpers.GetShortUrl(_config.Url.OverrideUrl, Request, urlItem.Key), Expires = request.Expires }); }
public async Task <IActionResult> Index(IndexPostRequest request) { request.Url = request.Url?.Trim(); var model = new IndexViewModel() { GoogleAnalyticsId = _config.Google.AnalyticsId, GoogleReCaptchaKey = _config.Google.RecaptchaV3SiteKey }; // Create URL if (!string.IsNullOrEmpty(request.Url)) { // Check Google Captcha if (!string.IsNullOrWhiteSpace(_config.Google.RecaptchaV3SecretKey)) { using var client = new HttpClient(); var parameters = new Dictionary <string, string>() { { "secret", _config.Google.RecaptchaV3SecretKey }, { "response", request.G_Recaptcha_Response } }; var form = new FormUrlEncodedContent(parameters); var gResponse = await client.PostAsync("https://www.google.com/recaptcha/api/siteverify", form); if (!gResponse.IsSuccessStatusCode) { _logger.LogError($"Failed reCaptcha communication: {gResponse.StatusCode} {gResponse.ReasonPhrase}"); model.ErrorMessage = "Failed captcha communication."; return(View(model)); } var reCaptcha = await gResponse.Content.ReadFromJsonAsync <ReCaptchaVerifyResponse>(); if (!reCaptcha.success) { _logger.LogWarning($"User failed reCaptcha. Error code(s): {string.Join(", ", reCaptcha.ErrorCodes ?? new string[] { "null" })}."); model.ErrorMessage = "Failed captcha test."; return(View(model)); } } // Check if URL is valid if (!Uri.TryCreate(request.Url, UriKind.Absolute, out var url)) { model.Text = $"Invalid URL format: {request.Url}"; return(View(model)); } var urlItem = new UrlItem() { Url = request.Url }; try { await _shortUrlService.CreateAsync(urlItem); } catch (Exception ex) { _logger.LogError(ex, $"Creating short url for: {url}"); model.ErrorMessage = "Error creating short url. Check error logs for details."; } var shortUrl = Helpers.GetShortUrl(_config.Url.OverrideUrl, Request, urlItem.Key); model.Text = $"New short url created"; model.Url = shortUrl; } return(View(model)); }