Skip to content

Commit f8bd791

Browse files
committed
Added proxy URL for authentication requests.
1 parent 2fa27b2 commit f8bd791

File tree

13 files changed

+88
-34
lines changed

13 files changed

+88
-34
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,5 @@ src/Umbraco.Forms.Integrations.TestSite/Media
396396
src/Umbraco.Forms.Integrations.TestSite/scripts
397397
src/Umbraco.Forms.Integrations.TestSite/Umbraco
398398
src/Umbraco.Forms.Integrations.TestSite/Views
399-
*.zip
399+
*.zip
400+
appsettings.Local.json
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Configuration
2+
{
3+
public class AppSettings
4+
{
5+
public string ClientSecret { get; set; }
6+
}
7+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Extensions.Options;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Net.Http;
7+
using System.Threading.Tasks;
8+
using Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Configuration;
9+
10+
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Controllers
11+
{
12+
[ApiController]
13+
public class OAuthProxyController : Controller
14+
{
15+
private readonly IHttpClientFactory _httpClientFactory;
16+
private readonly AppSettings _appSettings;
17+
18+
public OAuthProxyController(IHttpClientFactory httpClientFactory, IOptions<AppSettings> appSettings)
19+
{
20+
_httpClientFactory = httpClientFactory;
21+
_appSettings = appSettings.Value;
22+
}
23+
24+
[HttpPost]
25+
[Route("/oauth/v1/token")]
26+
public async Task ProxyTokenRequest()
27+
{
28+
var httpClient = _httpClientFactory.CreateClient("HubspotToken");
29+
30+
var response = await httpClient.PostAsync("oauth/v1/token", GetContent(Request.Form));
31+
var content = await response.Content.ReadAsStringAsync();
32+
33+
Response.StatusCode = (int)response.StatusCode;
34+
35+
Response.ContentType = response.Content.Headers.ContentType?.ToString();
36+
Response.ContentLength = response.Content.Headers.ContentLength;
37+
38+
await Response.WriteAsync(content);
39+
}
40+
41+
private HttpContent GetContent(IFormCollection form)
42+
{
43+
var dictionary = form.ToDictionary(x => x.Key, x => x.Value.ToString());
44+
dictionary.Add("client_secret", _appSettings.ClientSecret);
45+
return new FormUrlEncodedContent(dictionary);
46+
}
47+
}
48+
}

src/Umbraco.Forms.Integrations.Crm.Hubspot.OAuthCallback/Pages/Error.cshtml.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
using Microsoft.AspNetCore.Mvc;
22
using Microsoft.AspNetCore.Mvc.RazorPages;
33
using Microsoft.Extensions.Logging;
4-
using System;
5-
using System.Collections.Generic;
64
using System.Diagnostics;
7-
using System.Linq;
8-
using System.Threading.Tasks;
95

10-
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthCallback.Pages
6+
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Pages
117
{
128
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
139
[IgnoreAntiforgeryToken]

src/Umbraco.Forms.Integrations.Crm.Hubspot.OAuthCallback/Pages/Index.cshtml.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
using Microsoft.AspNetCore.Mvc;
2-
using Microsoft.AspNetCore.Mvc.RazorPages;
1+
using Microsoft.AspNetCore.Mvc.RazorPages;
32
using Microsoft.Extensions.Logging;
4-
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
7-
using System.Threading.Tasks;
83

9-
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthCallback.Pages
4+
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Pages
105
{
116
public class IndexModel : PageModel
127
{
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
@using Umbraco.Forms.Integrations.Crm.Hubspot.OAuthCallback
2-
@namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthCallback.Pages
1+
@using Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy
2+
@namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Pages
33
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

src/Umbraco.Forms.Integrations.Crm.Hubspot.OAuthCallback/Program.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
using Microsoft.AspNetCore.Hosting;
22
using Microsoft.Extensions.Configuration;
33
using Microsoft.Extensions.Hosting;
4-
using Microsoft.Extensions.Logging;
5-
using System;
6-
using System.Collections.Generic;
7-
using System.Linq;
8-
using System.Threading.Tasks;
94

10-
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthCallback
5+
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy
116
{
127
public class Program
138
{
@@ -18,6 +13,13 @@ public static void Main(string[] args)
1813

1914
public static IHostBuilder CreateHostBuilder(string[] args) =>
2015
Host.CreateDefaultBuilder(args)
16+
#if DEBUG
17+
.ConfigureAppConfiguration(config
18+
=> config.AddJsonFile(
19+
"appsettings.Local.json",
20+
optional: true,
21+
reloadOnChange: true))
22+
#endif
2123
.ConfigureWebHostDefaults(webBuilder =>
2224
{
2325
webBuilder.UseStartup<Startup>();

src/Umbraco.Forms.Integrations.Crm.Hubspot.OAuthCallback/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"ASPNETCORE_ENVIRONMENT": "Development"
1616
}
1717
},
18-
"Umbraco.Forms.Integrations.Crm.Hubspot.OAuthCallback": {
18+
"Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy": {
1919
"commandName": "Project",
2020
"dotnetRunMessages": "true",
2121
"launchBrowser": true,

src/Umbraco.Forms.Integrations.Crm.Hubspot.OAuthCallback/Startup.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
using System.Collections.Generic;
99
using System.Linq;
1010
using System.Threading.Tasks;
11+
using Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Configuration;
1112

12-
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthCallback
13+
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy
1314
{
1415
public class Startup
1516
{
@@ -23,7 +24,15 @@ public Startup(IConfiguration configuration)
2324
// This method gets called by the runtime. Use this method to add services to the container.
2425
public void ConfigureServices(IServiceCollection services)
2526
{
27+
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
28+
29+
services.AddHttpClient("HubspotToken", c =>
30+
{
31+
c.BaseAddress = new Uri("https://api.hubapi.com");
32+
});
33+
2634
services.AddRazorPages();
35+
services.AddControllers();
2736
}
2837

2938
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -49,6 +58,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4958

5059
app.UseEndpoints(endpoints =>
5160
{
61+
endpoints.MapControllers();
5262
endpoints.MapRazorPages();
5363
});
5464
}

0 commit comments

Comments
 (0)