Skip to content

Commit 3a2713b

Browse files
authored
Merge pull request #13 from umbraco/feature/google-auth
Feature/google auth
2 parents ebe4a91 + f731b31 commit 3a2713b

File tree

7 files changed

+104
-27
lines changed

7 files changed

+104
-27
lines changed

src/Umbraco.Cms.Integrations.OAuthProxy/Configuration/AppSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class AppSettings
1010

1111
public string ShopifyClientSecret { get; set; }
1212

13+
public string GoogleClientSecret { get; set; }
14+
1315
public string this[string propertyName] => (string)GetType().GetProperty(propertyName)?.GetValue(this, null);
1416
}
1517
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
namespace Umbraco.Cms.Integrations.OAuthProxy.Configuration
7+
{
8+
public static class ServiceConfiguration
9+
{
10+
/// <summary>
11+
/// Integrated services with their token URIs
12+
/// </summary>
13+
public static Dictionary<string, string> ServiceProviders = new Dictionary<string, string>
14+
{
15+
{ "Hubspot", "oauth/v1/token" },
16+
{ "HubspotForms", "oauth/v1/token" },
17+
{ "Semrush", "oauth2/access_token" },
18+
{ "Shopify", "oauth/access_token" },
19+
{ "Google", "token"}
20+
};
21+
22+
public static void AddServiceClients(this IServiceCollection services)
23+
{
24+
services.AddHttpClient("HubspotToken", c =>
25+
{
26+
c.BaseAddress = new Uri("https://api.hubapi.com/");
27+
});
28+
services.AddHttpClient("HubspotFormsToken", c =>
29+
{
30+
c.BaseAddress = new Uri("https://api.hubapi.com/");
31+
});
32+
services.AddHttpClient("SemrushToken", c =>
33+
{
34+
c.BaseAddress = new Uri("https://oauth.semrush.com/");
35+
});
36+
services.AddHttpClient("ShopifyToken", c =>
37+
{
38+
c.BaseAddress = new Uri("https://shop-replace.myshopify.com/admin/");
39+
});
40+
services.AddHttpClient("GoogleToken", c =>
41+
{
42+
c.BaseAddress = new Uri("https://oauth2.googleapis.com/");
43+
});
44+
}
45+
}
46+
}

src/Umbraco.Cms.Integrations.OAuthProxy/Controllers/OAuthProxyController.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ public class OAuthProxyController : Controller
2222
public const string ServiceNameHeaderKey = "service_name";
2323
public const string ServiceAddressReplacePrefixHeaderKey = "service_address_";
2424

25-
/// <summary>
26-
/// Integrated services with their token URIs
27-
/// </summary>
28-
private static readonly Dictionary<string, string> ValidServices = new()
29-
{
30-
{ "Hubspot", "oauth/v1/token" }, { "HubspotForms", "oauth/v1/token" }, { "Semrush", "oauth2/access_token" }, { "Shopify", "oauth/access_token" }
31-
};
32-
3325
public OAuthProxyController(IHttpClientFactory httpClientFactory, IOptions<AppSettings> appSettings)
3426
{
3527
_httpClientFactory = httpClientFactory;
@@ -45,7 +37,7 @@ public async Task ProxyTokenRequest()
4537
serviceName = "Hubspot";
4638
}
4739

48-
if (!ValidServices.ContainsKey(serviceName))
40+
if (!ServiceConfiguration.ServiceProviders.ContainsKey(serviceName))
4941
{
5042
throw
5143
new InvalidOperationException($"Provided {ServiceNameHeaderKey} header value of {serviceName} is not supported");
@@ -54,7 +46,7 @@ public async Task ProxyTokenRequest()
5446
var httpClient = GetClient(serviceName);
5547

5648
var response =
57-
await httpClient.PostAsync(ValidServices[serviceName], GetContent(Request.Form, serviceName));
49+
await httpClient.PostAsync(ServiceConfiguration.ServiceProviders[serviceName], GetContent(Request.Form, serviceName));
5850
var content = await response.Content.ReadAsStringAsync();
5951

6052
Response.StatusCode = (int)response.StatusCode;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@page "/oauth/google"
2+
@model Umbraco.Cms.Integrations.OAuthProxy.Pages.ShopifyModel
3+
@{
4+
ViewData["Title"] = "Authorization";
5+
}
6+
7+
<div class="text-center">
8+
<h1 class="display-4">Umbraco Integrations Authentication</h1>
9+
@if (!string.IsNullOrEmpty(Model.AuthorizationCode))
10+
{
11+
<div id="manual-complete" style="display: none">
12+
<p>To complete the integration, please copy the following code and enter it in Umbraco using the form provided.</p>
13+
14+
<p><input id="auth-code" type="text" value="@Model.AuthorizationCode" readonly style="width: 700px; font-size: 36px; text-align: center" /></p>
15+
16+
<button onclick="copyToClipboard()">Copy To Clipboard</button>
17+
</div>
18+
19+
<script>
20+
if (window.opener && typeof opener.postMessage === 'function') {
21+
opener.postMessage({ type: 'google:oauth:success', url: location.href, code: '@Model.AuthorizationCode' }, '*');
22+
window.close();
23+
} else {
24+
document.getElementById("manual-complete").style.display = "block";
25+
}1
26+
27+
function copyToClipboard() {
28+
var authCodeInputElement = document.getElementById("auth-code");
29+
authCodeInputElement.select();
30+
authCodeInputElement.setSelectionRange(0, 99999);
31+
navigator.clipboard.writeText(authCodeInputElement.value);
32+
alert("Code copied to clipboard");
33+
}
34+
</script>
35+
36+
}
37+
</div>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.AspNetCore.Mvc.RazorPages;
2+
3+
namespace Umbraco.Cms.Integrations.OAuthProxy.Pages
4+
{
5+
public class GoogleModel : PageModel
6+
{
7+
public string AuthorizationCode { get; set; }
8+
9+
public void OnGet()
10+
{
11+
AuthorizationCode = Request.Query["code"];
12+
}
13+
}
14+
}

src/Umbraco.Cms.Integrations.OAuthProxy/Startup.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,7 @@ public void ConfigureServices(IServiceCollection services)
2424
{
2525
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
2626

27-
services.AddHttpClient("HubspotToken", c =>
28-
{
29-
c.BaseAddress = new Uri("https://api.hubapi.com/");
30-
});
31-
services.AddHttpClient("HubspotFormsToken", c =>
32-
{
33-
c.BaseAddress = new Uri("https://api.hubapi.com/");
34-
});
35-
services.AddHttpClient("SemrushToken", c =>
36-
{
37-
c.BaseAddress = new Uri("https://oauth.semrush.com/");
38-
});
39-
services.AddHttpClient("ShopifyToken", c =>
40-
{
41-
c.BaseAddress = new Uri("https://shop-replace.myshopify.com/admin/");
42-
});
27+
services.AddServiceClients();
4328

4429
services.AddRazorPages();
4530
services.AddControllers();

src/Umbraco.Cms.Integrations.OAuthProxy/appsettings.Development.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"HubspotClientSecret": "",
1212
"HubspotFormsClientSecret": "",
1313
"SemrushClientSecret": "",
14-
"ShopifyClientSecret": ""
14+
"ShopifyClientSecret": "",
15+
"GoogleClientSecret": ""
1516
}
1617
}

0 commit comments

Comments
 (0)