Skip to content

Commit dd857a4

Browse files
committed
Toggle API key validation between CMS and Forms package settings
1 parent 583ee0e commit dd857a4

File tree

7 files changed

+87
-8
lines changed

7 files changed

+87
-8
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Umbraco.Cms.Integrations.Automation.Zapier.Configuration
8+
{
9+
public abstract class AppSettings
10+
{
11+
public string UserGroupAlias { get; set; }
12+
13+
public string ApiKey { get; set; }
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections.Specialized;
2+
3+
namespace Umbraco.Cms.Integrations.Automation.Zapier.Configuration
4+
{
5+
public class ZapierFormsSettings : ZapierSettings
6+
{
7+
public ZapierFormsSettings()
8+
{
9+
10+
}
11+
12+
public ZapierFormsSettings(NameValueCollection appSettings)
13+
{
14+
UserGroupAlias = appSettings[Constants.UmbracoFormsIntegrationsAutomationZapierUserGroupAlias];
15+
16+
ApiKey = appSettings[Constants.UmbracoFormsIntegrationsAutomationZapierApiKey];
17+
}
18+
}
19+
}

src/Umbraco.Cms.Integrations.Automation.Zapier/Configuration/ZapierSettings.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Umbraco.Cms.Integrations.Automation.Zapier.Configuration
44
{
5-
public class ZapierSettings
5+
public class ZapierSettings : AppSettings
66
{
77
public ZapierSettings()
88
{
@@ -15,9 +15,5 @@ public ZapierSettings(NameValueCollection appSettings)
1515

1616
ApiKey = appSettings[Constants.UmbracoCmsIntegrationsAutomationZapierApiKey];
1717
}
18-
19-
public string UserGroupAlias { get; set; }
20-
21-
public string ApiKey { get; set; }
2218
}
2319
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public class Constants
1313

1414
public const string UmbracoCmsIntegrationsAutomationZapierApiKey = "Umbraco.Cms.Integrations.Automation.Zapier.ApiKey";
1515

16+
public const string UmbracoFormsIntegrationsAutomationZapierUserGroupAlias = "Umbraco.Forms.Integrations.Automation.Zapier.UserGroupAlias";
17+
18+
public const string UmbracoFormsIntegrationsAutomationZapierApiKey = "Umbraco.Forms.Integrations.Automation.Zapier.ApiKey";
19+
1620
public static class ZapierAppConfiguration
1721
{
1822
public const string UsernameHeaderKey = "X-USERNAME";
@@ -25,6 +29,8 @@ public static class ZapierAppConfiguration
2529
public static class Configuration
2630
{
2731
public const string Settings = "Umbraco:Cms:Integrations:Automation:Zapier:Settings";
32+
33+
public const string FormsSettings = "Umbraco:Forms:Integrations:Automation:Zapier:Settings";
2834
}
2935

3036
public static class ContentProperties

src/Umbraco.Cms.Integrations.Automation.Zapier/Controllers/AuthController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public AuthController(IUserValidationService userValidationService)
2828
}
2929

3030
[HttpPost]
31-
public async Task<bool> ValidateUser([FromBody] UserModel userModel) => await _userValidationService.Validate(userModel.Username, userModel.Password, userModel.ApiKey);
31+
public async Task<bool> ValidateUser([FromBody] UserModel userModel) =>
32+
await _userValidationService.Validate(userModel.Username, userModel.Password, userModel.ApiKey);
3233
}
3334
}

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,30 @@ public class UserValidationService : IUserValidationService
2222

2323
private readonly ZapierSettings _zapierSettings;
2424

25+
private readonly ZapierFormsSettings _zapierFormsSettings;
26+
2527
#if NETCOREAPP
2628
private readonly IBackOfficeUserManager _backOfficeUserManager;
2729

28-
public UserValidationService(IOptions<ZapierSettings> options, IBackOfficeUserManager backOfficeUserManager)
30+
public UserValidationService(
31+
IOptions<ZapierSettings> options,
32+
IOptions<ZapierFormsSettings> zapierFormsSettings,
33+
IBackOfficeUserManager backOfficeUserManager)
2934
{
3035
_backOfficeUserManager = backOfficeUserManager;
3136

3237
_zapierSettings = options.Value;
38+
39+
_zapierFormsSettings = zapierFormsSettings.Value;
3340
}
3441
#else
3542
public UserValidationService(IUserService userService)
3643
{
3744
_userService = userService;
3845

3946
_zapierSettings = new ZapierSettings(ConfigurationManager.AppSettings);
47+
48+
_zapierSettings = new ZapierFormsSettings(ConfigurationManager.AppSettings);
4049
}
4150
#endif
4251

@@ -50,8 +59,38 @@ public UserValidationService(IUserService userService)
5059
public async Task<bool> Validate(string username, string password, string apiKey)
5160
{
5261
if (!string.IsNullOrEmpty(apiKey))
62+
{
63+
return ValidateByApiKey(apiKey);
64+
}
65+
66+
return await ValidateByCredentials(username, password);
67+
}
68+
69+
/// <summary>
70+
/// Validates user based on provided API key.
71+
/// When both CMS and Forms packages are installed, both settings (CMS/Forms) will be compared.
72+
/// </summary>
73+
/// <param name="apiKey">Provided API key in the Zap authentication.</param>
74+
/// <returns></returns>
75+
private bool ValidateByApiKey(string apiKey)
76+
{
77+
// Check API key from CMS settings, if none, check Forms settings
78+
if (!string.IsNullOrEmpty(_zapierSettings.ApiKey))
5379
return apiKey == _zapierSettings.ApiKey;
80+
else if (!string.IsNullOrEmpty(_zapierFormsSettings.ApiKey))
81+
return apiKey == _zapierFormsSettings.ApiKey;
5482

83+
return false;
84+
}
85+
86+
/// <summary>
87+
/// Validates user based on provided credentials.
88+
/// </summary>
89+
/// <param name="username"></param>
90+
/// <param name="password"></param>
91+
/// <returns></returns>
92+
private async Task<bool> ValidateByCredentials(string username, string password)
93+
{
5594
#if NETCOREAPP
5695
var isUserValid =
5796
await _backOfficeUserManager.ValidateCredentialsAsync(username, password);

src/Umbraco.Cms.Integrations.Automation.Zapier/ZapierComposer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ public class ZapierComposer : IComposer
2121
#if NETCOREAPP
2222
public void Compose(IUmbracoBuilder builder)
2323
{
24-
var options = builder.Services
24+
builder.Services
2525
.AddOptions<ZapierSettings>()
2626
.Bind(builder.Config.GetSection(Constants.Configuration.Settings));
27+
builder.Services
28+
.AddOptions<ZapierFormsSettings>()
29+
.Bind(builder.Config.GetSection(Constants.Configuration.FormsSettings));
2730

2831
builder
2932
.AddNotificationHandler<UmbracoApplicationStartingNotification, UmbracoAppStartingHandler>();

0 commit comments

Comments
 (0)