Skip to content

Commit da8494d

Browse files
committed
Shopify OAuth handler
1 parent c318c24 commit da8494d

File tree

6 files changed

+82
-4
lines changed

6 files changed

+82
-4
lines changed

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

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

99
public string SemrushClientSecret { get; set; }
1010

11+
public string ShopifyClientSecret { get; set; }
12+
1113
public string this[string propertyName] => (string)GetType().GetProperty(propertyName)?.GetValue(this, null);
1214
}
1315
}

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Net.Http;
5+
using System.Security;
56
using System.Threading.Tasks;
67

78
using Microsoft.AspNetCore.Http;
89
using Microsoft.AspNetCore.Mvc;
910
using Microsoft.Extensions.Options;
11+
using Microsoft.Extensions.Primitives;
1012
using Umbraco.Cms.Integrations.OAuthProxy.Configuration;
1113

1214
namespace Umbraco.Cms.Integrations.OAuthProxy.Controllers
@@ -18,13 +20,14 @@ public class OAuthProxyController : Controller
1820
private readonly AppSettings _appSettings;
1921

2022
public const string ServiceNameHeaderKey = "service_name";
23+
public const string ServiceAddressReplacePrefixHeaderKey = "service_address_";
2124

2225
/// <summary>
2326
/// Integrated services with their token URIs
2427
/// </summary>
25-
private static Dictionary<string, string> ValidServices = new()
28+
private static readonly Dictionary<string, string> ValidServices = new()
2629
{
27-
{ "Hubspot", "oauth/v1/token" }, { "HubspotForms", "oauth/v1/token" }, { "Semrush", "oauth2/access_token" }
30+
{ "Hubspot", "oauth/v1/token" }, { "HubspotForms", "oauth/v1/token" }, { "Semrush", "oauth2/access_token" }, { "Shopify", "oauth/access_token" }
2831
};
2932

3033
public OAuthProxyController(IHttpClientFactory httpClientFactory, IOptions<AppSettings> appSettings)
@@ -48,7 +51,7 @@ public async Task ProxyTokenRequest()
4851
new InvalidOperationException($"Provided {ServiceNameHeaderKey} header value of {serviceName} is not supported");
4952
}
5053

51-
var httpClient = _httpClientFactory.CreateClient($"{serviceName}Token");
54+
var httpClient = GetClient(serviceName);
5255

5356
var response =
5457
await httpClient.PostAsync(ValidServices[serviceName], GetContent(Request.Form, serviceName));
@@ -62,6 +65,22 @@ public async Task ProxyTokenRequest()
6265
await Response.WriteAsync(content);
6366
}
6467

68+
private HttpClient GetClient(string serviceName)
69+
{
70+
var httpClient = _httpClientFactory.CreateClient($"{serviceName}Token");
71+
72+
var serviceAddressReplaceHeader = Request.Headers.FirstOrDefault(p => p.Key.Contains(ServiceAddressReplacePrefixHeaderKey));
73+
if (!serviceAddressReplaceHeader.Equals(default(KeyValuePair<string, StringValues>)) && httpClient.BaseAddress != null)
74+
{
75+
var replaceKey = serviceAddressReplaceHeader.Key.Replace(ServiceAddressReplacePrefixHeaderKey, string.Empty);
76+
77+
var baseAddress = httpClient.BaseAddress.ToString().Replace($"{replaceKey}", serviceAddressReplaceHeader.Value);
78+
httpClient.BaseAddress = new Uri(baseAddress);
79+
}
80+
81+
return httpClient;
82+
}
83+
6584
private HttpContent GetContent(IFormCollection form, string serviceName)
6685
{
6786
var dictionary = form.ToDictionary(x => x.Key, x => x.Value.ToString());
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@page "/oauth/shopify"
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: 'shopify: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 ShopifyModel : 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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Extensions.Configuration;
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Hosting;
8+
89
using Umbraco.Cms.Integrations.OAuthProxy.Configuration;
910

1011
namespace Umbraco.Cms.Integrations.OAuthProxy
@@ -35,6 +36,10 @@ public void ConfigureServices(IServiceCollection services)
3536
{
3637
c.BaseAddress = new Uri("https://oauth.semrush.com/");
3738
});
39+
services.AddHttpClient("ShopifyToken", c =>
40+
{
41+
c.BaseAddress = new Uri("https://shop-replace.myshopify.com/admin/");
42+
});
3843

3944
services.AddRazorPages();
4045
services.AddControllers();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"AppSettings": {
1111
"HubspotClientSecret": "",
1212
"HubspotFormsClientSecret": "",
13-
"SemrushClientSecret": ""
13+
"SemrushClientSecret": "",
14+
"ShopifyClientSecret": ""
1415
}
1516
}

0 commit comments

Comments
 (0)