Skip to content

Commit 2f92b4b

Browse files
authored
Merge pull request #93 from umbraco/bugfix/zapier-auth
Update Zapier API key authentication flow
2 parents 2e36b13 + 4f8177f commit 2f92b4b

File tree

10 files changed

+74
-55
lines changed

10 files changed

+74
-55
lines changed

src/Umbraco.Forms.Integrations.Automation.Zapier/Configuration/ZapierSettings.cs renamed to src/Umbraco.Forms.Integrations.Automation.Zapier/Configuration/AppSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Umbraco.Forms.Integrations.Automation.Zapier.Configuration
22
{
3-
public class ZapierSettings
3+
public class AppSettings
44
{
55
public string UserGroupAlias { get; set; }
66

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Configuration
2+
{
3+
public class ZapierCmsSettings : AppSettings
4+
{
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Configuration
2+
{
3+
public class ZapierFormsSettings : AppSettings
4+
{
5+
}
6+
}

src/Umbraco.Forms.Integrations.Automation.Zapier/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public static class ZapierAppConfiguration
1414

1515
public static class Configuration
1616
{
17+
public const string CmsSettings = "Umbraco:CMS:Integrations:Automation:Zapier:Settings";
18+
1719
public const string Settings = "Umbraco:Forms:Integrations:Automation:Zapier:Settings";
1820
}
1921

src/Umbraco.Forms.Integrations.Automation.Zapier/Controllers/FormController.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33

44
using Umbraco.Forms.Integrations.Automation.Zapier.Models.Dtos;
55
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
6-
using Umbraco.Forms.Integrations.Automation.Zapier.Configuration;
7-
8-
using Microsoft.Extensions.Options;
96

107
namespace Umbraco.Forms.Integrations.Automation.Zapier.Controllers
118
{
@@ -16,11 +13,9 @@ public class FormController : ZapierFormAuthorizedApiController
1613
{
1714
private readonly ZapierFormService _zapierFormService;
1815

19-
public FormController(IOptions<ZapierSettings> options, IUserValidationService userValidationService, ZapierFormService zapierFormService)
20-
: base(options, userValidationService)
21-
{
22-
_zapierFormService = zapierFormService;
23-
}
16+
public FormController(
17+
IUserValidationService userValidationService, ZapierFormService zapierFormService)
18+
: base(userValidationService) => _zapierFormService = zapierFormService;
2419

2520
public IEnumerable<FormDto> GetForms()
2621
{

src/Umbraco.Forms.Integrations.Automation.Zapier/Controllers/FormPollingController.cs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
using System.Collections.Generic;
2-
using System.Linq;
3-
4-
using Microsoft.Extensions.Options;
5-
6-
using Umbraco.Forms.Core.Data.Storage;
72
using Umbraco.Forms.Integrations.Automation.Zapier.Extensions;
8-
using Umbraco.Forms.Integrations.Automation.Zapier.Helpers;
93
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
10-
using Umbraco.Forms.Integrations.Automation.Zapier.Configuration;
114

125
namespace Umbraco.Forms.Integrations.Automation.Zapier.Controllers
136
{
@@ -19,21 +12,10 @@ public class FormPollingController : ZapierFormAuthorizedApiController
1912
{
2013
private readonly ZapierFormService _zapierFormService;
2114

22-
private readonly IRecordStorage _recordStorage;
23-
24-
private readonly UmbUrlHelper _umbUrlHelper;
25-
26-
public FormPollingController(IOptions<ZapierSettings> options, ZapierFormService zapierFormService, IRecordStorage recordStorage,
27-
UmbUrlHelper umbUrlHelper,
15+
public FormPollingController(
16+
ZapierFormService zapierFormService,
2817
IUserValidationService userValidationService)
29-
: base(options, userValidationService)
30-
{
31-
_zapierFormService = zapierFormService;
32-
33-
_recordStorage = recordStorage;
34-
35-
_umbUrlHelper = umbUrlHelper;
36-
}
18+
: base(userValidationService) => _zapierFormService = zapierFormService;
3719

3820
public List<Dictionary<string, string>> GetFormPropertiesById(string id)
3921
{

src/Umbraco.Forms.Integrations.Automation.Zapier/Controllers/ZapierFormAuthorizedApiController.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
using System.Linq;
2-
3-
using Microsoft.Extensions.Options;
4-
5-
using Umbraco.Forms.Integrations.Automation.Zapier.Configuration;
6-
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
72
using Umbraco.Cms.Web.Common.Controllers;
3+
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
84

95
namespace Umbraco.Forms.Integrations.Automation.Zapier.Controllers
106
{
117
public class ZapierFormAuthorizedApiController : UmbracoApiController
128
{
13-
private readonly ZapierSettings Options;
14-
159
private readonly IUserValidationService _userValidationService;
1610

17-
public ZapierFormAuthorizedApiController(IOptions<ZapierSettings> options, IUserValidationService userValidationService)
18-
{
19-
Options = options.Value;
20-
11+
public ZapierFormAuthorizedApiController(
12+
IUserValidationService userValidationService) =>
2113
_userValidationService = userValidationService;
22-
}
2314

2415
public bool IsAccessValid()
2516
{
@@ -39,10 +30,7 @@ public bool IsAccessValid()
3930

4031
if (string.IsNullOrEmpty(apiKey) && (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))) return false;
4132

42-
if (!string.IsNullOrEmpty(apiKey))
43-
return apiKey == Options.ApiKey;
44-
45-
var isAuthorized = _userValidationService.Validate(username, password, Options.ApiKey).GetAwaiter()
33+
var isAuthorized = _userValidationService.Validate(username, password, apiKey).GetAwaiter()
4634
.GetResult();
4735
if (!isAuthorized) return false;
4836

src/Umbraco.Forms.Integrations.Automation.Zapier/Services/UserValidationService.cs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,73 @@ namespace Umbraco.Forms.Integrations.Automation.Zapier.Services
1111
{
1212
public class UserValidationService : IUserValidationService
1313
{
14-
private readonly ZapierSettings _zapierSettings;
14+
private readonly ZapierCmsSettings _zapierCmsSettings;
15+
private readonly ZapierFormsSettings _zapierFormsSettings;
1516

1617
private readonly IUserService _userService;
1718

1819
private readonly IBackOfficeUserManager _backOfficeUserManager;
1920

20-
public UserValidationService(IOptions<ZapierSettings> options, IUserService userService, IBackOfficeUserManager backOfficeUserManager)
21+
public UserValidationService(
22+
IOptions<ZapierCmsSettings> cmsOptions,
23+
IOptions<ZapierFormsSettings> formsOptions,
24+
IUserService userService,
25+
IBackOfficeUserManager backOfficeUserManager)
2126
{
22-
_zapierSettings = options.Value;
27+
_zapierCmsSettings = cmsOptions.Value;
28+
_zapierFormsSettings = formsOptions.Value;
2329

2430
_backOfficeUserManager = backOfficeUserManager;
2531

2632
_userService = userService;
2733
}
2834

35+
/// <summary>
36+
/// Allow access by validating API Key. If API key is missing, validate user credentials.
37+
/// </summary>
38+
/// <param name="username"></param>
39+
/// <param name="password"></param>
40+
/// <param name="apiKey"></param>
41+
/// <returns></returns>
2942
public async Task<bool> Validate(string username, string password, string apiKey)
3043
{
3144
if (!string.IsNullOrEmpty(apiKey))
32-
return apiKey == _zapierSettings.ApiKey;
45+
{
46+
return ValidateByApiKey(apiKey);
47+
}
48+
49+
return await ValidateByCredentials(username, password);
50+
}
51+
52+
/// <summary>
53+
/// Validates user based on provided API key.
54+
/// When both CMS and Forms packages are installed, both settings (CMS/Forms) will be compared.
55+
/// </summary>
56+
/// <param name="apiKey">Provided API key in the Zap authentication.</param>
57+
/// <returns></returns>
58+
private bool ValidateByApiKey(string apiKey) =>
59+
// Check API key from CMS and Forms settings.
60+
(!string.IsNullOrEmpty(_zapierCmsSettings.ApiKey) && _zapierCmsSettings.ApiKey == apiKey)
61+
|| (!string.IsNullOrEmpty(_zapierFormsSettings.ApiKey) && _zapierFormsSettings.ApiKey == apiKey);
3362

63+
/// <summary>
64+
/// Validates user based on provided credentials.
65+
/// </summary>
66+
/// <param name="username"></param>
67+
/// <param name="password"></param>
68+
/// <returns></returns>
69+
private async Task<bool> ValidateByCredentials(string username, string password)
70+
{
3471
var isUserValid =
3572
await _backOfficeUserManager.ValidateCredentialsAsync(username, password);
3673

3774
if (!isUserValid) return false;
3875

39-
if (!string.IsNullOrEmpty(_zapierSettings.UserGroupAlias))
76+
if (!string.IsNullOrEmpty(_zapierFormsSettings.UserGroupAlias))
4077
{
4178
var user = _userService.GetByUsername(username);
4279

43-
return user != null && user.Groups.Any(p => p.Alias == _zapierSettings.UserGroupAlias);
80+
return user != null && user.Groups.Any(p => p.Alias == _zapierFormsSettings.UserGroupAlias);
4481
}
4582

4683
return true;

src/Umbraco.Forms.Integrations.Automation.Zapier/Umbraco.Forms.Integrations.Automation.Zapier.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageIconUrl></PackageIconUrl>
1212
<PackageProjectUrl>https://github.com/umbraco/Umbraco.Forms.Integrations/tree/main-v10/src/Umbraco.Forms.Integrations.Automation.Zapier</PackageProjectUrl>
1313
<RepositoryUrl>https://github.com/umbraco/Umbraco.Forms.Integrations</RepositoryUrl>
14-
<Version>2.0.5</Version>
14+
<Version>2.0.6</Version>
1515
<Authors>Umbraco HQ</Authors>
1616
<Company>Umbraco</Company>
1717
<PackageTags>Umbraco;Umbraco-Marketplace</PackageTags>

src/Umbraco.Forms.Integrations.Automation.Zapier/ZapierFormsComposer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ public class ZapierFormsComposer : IComposer
1414
{
1515
public void Compose(IUmbracoBuilder builder)
1616
{
17-
var options = builder.Services
18-
.AddOptions<ZapierSettings>()
17+
builder.Services
18+
.AddOptions<ZapierCmsSettings>()
19+
.Bind(builder.Config.GetSection(Constants.Configuration.CmsSettings));
20+
builder.Services
21+
.AddOptions<ZapierFormsSettings>()
1922
.Bind(builder.Config.GetSection(Constants.Configuration.Settings));
2023

2124
builder

0 commit comments

Comments
 (0)