Skip to content

Commit e0be609

Browse files
committed
PR review and refactoring
1 parent d099c55 commit e0be609

File tree

68 files changed

+132
-448
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+132
-448
lines changed

src/Umbraco.Cms.Integrations.Authorization/Dependencies.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Umbraco.Cms.Integrations.Authorization/Interfaces/IAuthorizationService.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Umbraco.Cms.Integrations.Authorization/Models/Dtos/ResponseDto.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Umbraco.Cms.Integrations.Authorization/Models/Enums/ServiceType.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Umbraco.Cms.Integrations.Authorization/Umbraco.Cms.Integrations.Authorization.Core.csproj

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"dotnet-ef": {
6+
"version": "6.0.1",
7+
"commands": [
8+
"dotnet-ef"
9+
]
10+
}
11+
}
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Configuration
2+
{
3+
public class AppSettings
4+
{
5+
public string HubspotAuthUri { get; set; }
6+
7+
public string HubspotClientSecret { get; set; }
8+
9+
public string SemrushAuthUri { get; set; }
10+
11+
public string SemrushClientSecret { get; set; }
12+
13+
public string this[string propertyName] => (string) GetType().GetProperty(propertyName)?.GetValue(this, null);
14+
}
15+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Http;
5+
using System.Threading.Tasks;
6+
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Options;
10+
11+
using Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Configuration;
12+
13+
namespace Umbraco.Cms.Integrations.OAuthProxy.Controllers
14+
{
15+
[ApiController]
16+
public class OAuthProxyController : Controller
17+
{
18+
private readonly IHttpClientFactory _httpClientFactory;
19+
private readonly AppSettings _appSettings;
20+
21+
public const string ServiceNameHeaderKey = "service_name";
22+
23+
private static List<string> ValidServiceNames = new List<string> {"Hubspot", "Semrush"};
24+
25+
public OAuthProxyController(IHttpClientFactory httpClientFactory, IOptions<AppSettings> appSettings)
26+
{
27+
_httpClientFactory = httpClientFactory;
28+
_appSettings = appSettings.Value;
29+
}
30+
31+
[HttpPost]
32+
[Route("/oauth/v1/token")]
33+
public async Task ProxyTokenRequest()
34+
{
35+
if (!Request.Headers.TryGetValue(ServiceNameHeaderKey, out var serviceName))
36+
{
37+
serviceName = "Hubspot";
38+
}
39+
40+
if (!ValidServiceNames.Contains(serviceName))
41+
{
42+
throw
43+
new InvalidOperationException($"Provided {ServiceNameHeaderKey} header value of {serviceName} is not supported");
44+
}
45+
46+
var httpClient = _httpClientFactory.CreateClient($"{serviceName}Token");
47+
48+
var response =
49+
await httpClient.PostAsync(GetAuthUri(serviceName), GetContent(Request.Form, serviceName));
50+
var content = await response.Content.ReadAsStringAsync();
51+
52+
Response.StatusCode = (int)response.StatusCode;
53+
54+
Response.ContentType = response.Content.Headers.ContentType?.ToString();
55+
Response.ContentLength = response.Content.Headers.ContentLength;
56+
57+
await Response.WriteAsync(content);
58+
}
59+
60+
private HttpContent GetContent(IFormCollection form, string serviceName)
61+
{
62+
var dictionary = form.ToDictionary(x => x.Key, x => x.Value.ToString());
63+
dictionary.Add("client_secret", GetClientSecret(serviceName));
64+
return new FormUrlEncodedContent(dictionary);
65+
}
66+
67+
private string GetClientSecret(string serviceName) => _appSettings[$"{serviceName}ClientSecret"];
68+
69+
private string GetAuthUri(string serviceName) => _appSettings[$"{serviceName}AuthUri"];
70+
}
71+
}

0 commit comments

Comments
 (0)