Skip to content

Commit 174c084

Browse files
authored
Merge pull request #4 from umbraco/feature/semrush-auth
Feature/semrush auth
2 parents df4987e + 2386849 commit 174c084

Some content is hidden

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

57 files changed

+127
-66
lines changed

azure-pipeline - Crm.Hubspot.OAuthProxy.yml renamed to azure-pipeline - Cms.OAuthProxy.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pool:
55
vmImage: 'windows-latest'
66

77
variables:
8-
projectName: 'Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy'
8+
projectName: 'Umbraco.Cms.Integrations.OAuthProxy'
99
project: 'src/$(projectName)/$(projectName).csproj'
1010
buildConfiguration: 'Release'
1111
azureServiceConnectionDev: 'Umbraco Forms Integrations - Umbraco Cloud Services Dev - Hubspot Auth'
@@ -36,9 +36,14 @@ steps:
3636
appSettings: |
3737
[
3838
{
39-
"name": "AppSettings:ClientSecret",
39+
"name": "AppSettings:HubspotClientSecret",
4040
"value": "$(hubSpotAppSecretKey)",
4141
"slotSetting": false
42+
},
43+
{
44+
"name": "AppSettings:SemrushClientSecret",
45+
"value": "$(semrushAppSecretKey)",
46+
"slotSetting": false
4247
}
4348
]
4449
- task: AzureWebApp@1
@@ -55,8 +60,13 @@ steps:
5560
appSettings: |
5661
[
5762
{
58-
"name": "AppSettings:ClientSecret",
63+
"name": "AppSettings:HubspotClientSecret",
5964
"value": "$(hubSpotAppSecretKey)",
6065
"slotSetting": false
66+
},
67+
{
68+
"name": "AppSettings:SemrushClientSecret",
69+
"value": "$(semrushAppSecretKey)",
70+
"slotSetting": false
6171
}
6272
]
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Configuration
2+
{
3+
public class AppSettings
4+
{
5+
public string HubspotClientSecret { get; set; }
6+
7+
public string SemrushClientSecret { get; set; }
8+
9+
public string this[string propertyName] => (string) GetType().GetProperty(propertyName)?.GetValue(this, null);
10+
}
11+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
/// <summary>
24+
/// Integrated services with their token URIs
25+
/// </summary>
26+
private static Dictionary<string, string> ValidServices = new Dictionary<string, string>
27+
{
28+
{ "Hubspot", "oauth/v1/token" }, { "Semrush", "oauth2/access_token" }
29+
};
30+
31+
//private static List<string> ValidServiceNames = new List<string> {"Hubspot", "Semrush"};
32+
33+
public OAuthProxyController(IHttpClientFactory httpClientFactory, IOptions<AppSettings> appSettings)
34+
{
35+
_httpClientFactory = httpClientFactory;
36+
_appSettings = appSettings.Value;
37+
}
38+
39+
[HttpPost]
40+
[Route("/oauth/v1/token")]
41+
public async Task ProxyTokenRequest()
42+
{
43+
if (!Request.Headers.TryGetValue(ServiceNameHeaderKey, out var serviceName))
44+
{
45+
serviceName = "Hubspot";
46+
}
47+
48+
if (!ValidServices.ContainsKey(serviceName))
49+
{
50+
throw
51+
new InvalidOperationException($"Provided {ServiceNameHeaderKey} header value of {serviceName} is not supported");
52+
}
53+
54+
var httpClient = _httpClientFactory.CreateClient($"{serviceName}Token");
55+
56+
var response =
57+
await httpClient.PostAsync(ValidServices[serviceName], GetContent(Request.Form, serviceName));
58+
var content = await response.Content.ReadAsStringAsync();
59+
60+
Response.StatusCode = (int)response.StatusCode;
61+
62+
Response.ContentType = response.Content.Headers.ContentType?.ToString();
63+
Response.ContentLength = response.Content.Headers.ContentLength;
64+
65+
await Response.WriteAsync(content);
66+
}
67+
68+
private HttpContent GetContent(IFormCollection form, string serviceName)
69+
{
70+
var dictionary = form.ToDictionary(x => x.Key, x => x.Value.ToString());
71+
dictionary.Add("client_secret", GetClientSecret(serviceName));
72+
return new FormUrlEncodedContent(dictionary);
73+
}
74+
75+
private string GetClientSecret(string serviceName) => _appSettings[$"{serviceName}ClientSecret"];
76+
}
77+
}

0 commit comments

Comments
 (0)