Skip to content

Commit d099c55

Browse files
committed
Factory updates to support refresh token action
1 parent 44bd9a9 commit d099c55

File tree

6 files changed

+77
-12
lines changed

6 files changed

+77
-12
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ public class ServiceType
55
{
66
public enum ServiceTypeEnum
77
{
8-
Hubspot,
9-
Semrush,
8+
HubspotAuthorization,
9+
SemrushAuthorization,
10+
SemrushReauthorization,
1011
Shopify
1112
}
1213
}

src/Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy/Configuration/Settings.cs

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

88
public string AuthEndpoint { get; set; }
99

10+
public string RefreshAuthEndpoint { get; set; }
11+
1012
public string ClientId { get; set; }
1113

1214
public string ClientSecret { get; set; }

src/Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy/Factories/AuthorizationServiceFactory.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using System;
22
using System.Net.Http;
3-
using Microsoft.AspNetCore.Http;
3+
44
using Microsoft.Extensions.Configuration;
5-
using Microsoft.Extensions.Options;
5+
66
using Umbraco.Cms.Integrations.Authorization.Core.Models.Enums;
77
using Umbraco.Cms.Integrations.Authorization.Core.Interfaces;
8-
using Umbraco.Cms.Integrations.Authorization.Core.Models.Dtos;
9-
using Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Configuration;
108
using Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Services;
119

1210
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Factories
@@ -28,10 +26,12 @@ public IAuthorizationService Create(ServiceType.ServiceTypeEnum serviceType)
2826
{
2927
switch (serviceType)
3028
{
31-
case ServiceType.ServiceTypeEnum.Hubspot:
29+
case ServiceType.ServiceTypeEnum.HubspotAuthorization:
3230
return new HubspotService(_httpClientFactory, _configuration);
33-
case ServiceType.ServiceTypeEnum.Semrush:
34-
return new SemrushService(_httpClientFactory, _configuration);
31+
case ServiceType.ServiceTypeEnum.SemrushAuthorization:
32+
return new SemrushAuthorizationService(_httpClientFactory, _configuration);
33+
case ServiceType.ServiceTypeEnum.SemrushReauthorization:
34+
return new SemrushReauthorizationService(_httpClientFactory, _configuration);
3535
default: throw new NotImplementedException("Service Type not implemented.");
3636
}
3737
}

src/Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy/Services/SemrushService.cs renamed to src/Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy/Services/SemrushAuthorizationService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@
55

66
using Microsoft.AspNetCore.Http;
77
using Microsoft.Extensions.Configuration;
8-
using Microsoft.Extensions.Options;
98

109
using Umbraco.Cms.Integrations.Authorization.Core.Models.Dtos;
1110
using Umbraco.Cms.Integrations.Authorization.Core.Interfaces;
1211
using Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Configuration;
1312

1413
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Services
1514
{
16-
public class SemrushService: IAuthorizationService
15+
public class SemrushAuthorizationService: IAuthorizationService
1716
{
1817
private readonly IHttpClientFactory _httpClientFactory;
1918

2019
private readonly IConfiguration _configuration;
2120

2221
private readonly Settings _settings;
2322

24-
public SemrushService(IHttpClientFactory httpClientFactory, IConfiguration configuration)
23+
public SemrushAuthorizationService(IHttpClientFactory httpClientFactory, IConfiguration configuration)
2524
{
2625
_httpClientFactory = httpClientFactory;
2726

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Linq;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.Extensions.Configuration;
8+
9+
using Umbraco.Cms.Integrations.Authorization.Core.Models.Dtos;
10+
using Umbraco.Cms.Integrations.Authorization.Core.Interfaces;
11+
using Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Configuration;
12+
13+
namespace Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy.Services
14+
{
15+
public class SemrushReauthorizationService: IAuthorizationService
16+
{
17+
private readonly IHttpClientFactory _httpClientFactory;
18+
19+
private readonly IConfiguration _configuration;
20+
21+
private readonly Settings _settings;
22+
23+
public SemrushReauthorizationService(IHttpClientFactory httpClientFactory, IConfiguration configuration)
24+
{
25+
_httpClientFactory = httpClientFactory;
26+
27+
_configuration = configuration;
28+
29+
_settings = new Settings();
30+
_configuration.GetSection("SemrushSettings").Bind(_settings);
31+
}
32+
33+
public async Task<ResponseDto> ProcessAsync(HttpContent content)
34+
{
35+
var client = _httpClientFactory.CreateClient();
36+
client.BaseAddress = new Uri(_settings.BaseUrl);
37+
38+
var response = await client.PostAsync(_settings.RefreshAuthEndpoint, content);
39+
40+
var responseContent = await response.Content.ReadAsStringAsync();
41+
42+
return new ResponseDto
43+
{
44+
StatusCode = (int) response.StatusCode,
45+
ContentType = response.Content.Headers.ContentType?.ToString(),
46+
ContentLength = response.Content.Headers.ContentLength,
47+
Content = responseContent
48+
};
49+
}
50+
51+
public HttpContent GetContent(IFormCollection form)
52+
{
53+
var dictionary = form.ToDictionary(x => x.Key, x => x.Value.ToString());
54+
55+
dictionary.Add("client_id", _settings.ClientId);
56+
dictionary.Add("client_secret", _settings.ClientSecret);
57+
dictionary.Add("grant_type", "refresh_token");
58+
59+
return new FormUrlEncodedContent(dictionary);
60+
}
61+
}
62+
}

src/Umbraco.Forms.Integrations.Crm.Hubspot.OAuthProxy/appsettings.Development.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"SemrushSettings": {
1111
"BaseUrl": "https://oauth.semrush.com/",
1212
"AuthEndpoint": "oauth2/access_token",
13+
"RefreshAuthEndpoint": "oauth2/access_token",
1314
"ApiBaseUrl": "https://api.semrush.com",
1415
"ClientId": "umbraco",
1516
"ClientSecret": "XOPzh6WKjOI5bfhB0M26dIWFHLfktOWk",

0 commit comments

Comments
 (0)