|
| 1 | +#if NETCOREAPP |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | + |
| 4 | +using Umbraco.Cms.Web.BackOffice.Controllers; |
| 5 | +using Umbraco.Cms.Web.Common.Attributes; |
| 6 | +#else |
| 7 | +using System.Web.Http; |
| 8 | + |
| 9 | +using Umbraco.Web.Mvc; |
| 10 | +using Umbraco.Web.WebApi; |
| 11 | +#endif |
| 12 | + |
| 13 | +using System; |
| 14 | +using System.Collections.Generic; |
| 15 | +using System.Net.Http; |
| 16 | +using System.Threading.Tasks; |
| 17 | +using Umbraco.Cms.Integrations.SEO.GoogleSearchConsole.URLInspectionTool.Models.Dtos; |
| 18 | +using Umbraco.Cms.Integrations.SEO.GoogleSearchConsole.URLInspectionTool.Services; |
| 19 | + |
| 20 | + |
| 21 | +namespace Umbraco.Cms.Integrations.SEO.GoogleSearchConsole.URLInspectionTool.Controllers |
| 22 | +{ |
| 23 | + [PluginController("UmbracoCmsIntegrationsGoogleSearchConsole")] |
| 24 | + public class UrlInspectionApiController : UmbracoAuthorizedApiController |
| 25 | + { |
| 26 | + // Using a static HttpClient (see: https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/). |
| 27 | + private readonly static HttpClient s_client = new HttpClient(); |
| 28 | + |
| 29 | + // Access to the client within the class is via ClientFactory(), allowing us to mock the responses in tests. |
| 30 | + internal static Func<HttpClient> ClientFactory = () => s_client; |
| 31 | + |
| 32 | + private readonly GoogleService _googleService; |
| 33 | + |
| 34 | + private readonly ITokenService _tokenService; |
| 35 | + |
| 36 | + public UrlInspectionApiController(GoogleService googleService, ITokenService tokenService) |
| 37 | + { |
| 38 | + _googleService = googleService; |
| 39 | + |
| 40 | + _tokenService = tokenService; |
| 41 | + } |
| 42 | + |
| 43 | + [HttpGet] |
| 44 | + public string GetAuthorizationUrl() => _googleService.GetAuthorizationUrl(); |
| 45 | + |
| 46 | + [HttpPost] |
| 47 | + public async Task<string> GetAccessToken([FromBody] AuthorizationRequestDto authorizationRequestDto) |
| 48 | + { |
| 49 | + var requestData = new Dictionary<string, string> |
| 50 | + { |
| 51 | + {"code", authorizationRequestDto.Code}, |
| 52 | + {"client_id", _googleService.GetClientId()}, |
| 53 | + {"redirect_uri", "oauth/google"}, |
| 54 | + {"grant_type", "authorization_code"} |
| 55 | + }; |
| 56 | + |
| 57 | + var requestMessage = new HttpRequestMessage |
| 58 | + { |
| 59 | + Method = HttpMethod.Post, |
| 60 | + RequestUri = new Uri(_googleService.GetAuthProxyTokenEndpoint()), |
| 61 | + Content = new FormUrlEncodedContent(requestData), |
| 62 | + }; |
| 63 | + requestMessage.Headers.Add(_googleService.ServiceHeaderKey.Key, _googleService.ServiceHeaderKey.Value); |
| 64 | + |
| 65 | + var response = await ClientFactory().SendAsync(requestMessage); |
| 66 | + if (response.IsSuccessStatusCode) |
| 67 | + { |
| 68 | + var result = await response.Content.ReadAsStringAsync(); |
| 69 | + |
| 70 | + _tokenService.SaveParameters(_googleService.TokenDbKey, result); |
| 71 | + |
| 72 | + return result; |
| 73 | + } |
| 74 | + |
| 75 | + return "error"; |
| 76 | + } |
| 77 | + |
| 78 | + [HttpPost] |
| 79 | + public void RevokeToken() |
| 80 | + { |
| 81 | + _tokenService.RemoveParameters(_googleService.TokenDbKey); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments