Skip to content

Commit 1a26d0f

Browse files
authored
Merge pull request #6821 from umbraco/cms15/twoFactor
2FA article for Umbraco 15
2 parents afee969 + bf58734 commit 1a26d0f

File tree

4 files changed

+115
-40
lines changed

4 files changed

+115
-40
lines changed

13/umbraco-cms/reference/security/two-factor-authentication.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@ At this point, the 2FA is active, but no members have set up 2FA yet. The setup
218218
}
219219
```
220220

221-
10. [Optional] Customize the text fields and buttons to match your websites tone of voice.
222-
223221
{% endcode %}
224222

223+
10. [Optional] Customize the text fields and buttons to match your websites tone of voice.
224+
225225
![The QR Code is shown along with a field to enter a value to set up the two factor authentication.](images/2fa-Members-QR-code.png)
226226

227227
### Test the set up for Members
Loading
Loading

15/umbraco-cms/reference/security/two-factor-authentication.md

+113-38
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,41 @@ description: >-
66

77
# Two-factor Authentication
88

9-
Two-factor authentication (2FA) for Umbraco members is activated by implementing an `ITwoFactorProvider` interface and registering the implementation. The implementation can use third-party packages to archive for example support for authentication apps like Microsoft- or Google Authentication App.
9+
This article includes guides for implementing two-factor authentication options for both backoffice users and website members:
10+
11+
* [Two-Factor Authentication for Members](#two-factor-authentication-for-members)
12+
* [Two-Factor Authentication for Users](#two-factor-authentication-for-users)
13+
14+
Two-factor authentication (2FA) for Umbraco Users and Members is activated by implementing an `ITwoFactorProvider` interface and registering the implementation. The implementation can use third-party packages to support authentication apps like the Microsoft- or Google Authentication Apps.
1015

1116
{% hint style="info" %}
1217
If you are using [Umbraco Cloud](https://umbraco.com/products/umbraco-cloud/), you can enable multi-factor authentication in Umbraco ID. For more information, see the [Multi-Factor Authentication](https://docs.umbraco.com/umbraco-cloud/set-up/multi-factor-authentication-on-cloud) article.
1318
{% endhint %}
1419

1520
## Two-factor authentication for Members
1621

17-
Since Umbraco does not control how the UI is for member login and profile edit. The UI for 2FA is shipped as part of the Partial View snippets. These can be used as a starting point, before styling the page as you would like.
22+
The following guide will take you through implementing an option for your website members to enable two-factor authentication.
23+
24+
{% hint style="info" %}
25+
A setup for members needs to be implemented on your website in order for you to follow this guide. This setup should include:
26+
27+
* Login and logout options.
28+
* Public access restriction configured on at least 1 content item.
1829

19-
### Example implementation for Authenticator Apps for Members
30+
[Learn more about setting up a members section in Umbraco.](../../tutorials/members-registration-and-login.md)
31+
{% endhint %}
2032

21-
In the following example, we will use the [GoogleAuthenticator NuGet Package](https://www.nuget.org/packages/GoogleAuthenticator/).
33+
As an example, the guide will use the [GoogleAuthenticator NuGet Package](https://www.nuget.org/packages/GoogleAuthenticator/). This package works for both Google and Microsoft authenticator apps. It can be used to generate the QR code needed to activate the app for the website.
2234

23-
Despite the name, this package works for both Google and Microsoft authenticator apps. It can be used to generate the QR code needed to activate the app for the website.
35+
1. Install the GoogleAuthenticator Nuget Package on your project.
36+
2. Create a new file in your project: `UmbracoAppAuthenticator.cs`.
37+
3. Update the file with the following code snippet.
2438

2539
{% code title="UmbracoAppAuthenticator.cs" lineNumbers="true" %}
40+
2641
```csharp
27-
using System;
28-
using System.Threading.Tasks;
2942
using Google.Authenticator;
43+
using System.Runtime.Serialization;
3044
using Umbraco.Cms.Core.Security;
3145
using Umbraco.Cms.Core.Services;
3246

@@ -35,17 +49,19 @@ namespace My.Website;
3549
/// <summary>
3650
/// Model with the required data to setup the authentication app.
3751
/// </summary>
38-
public class QrCodeSetupData
52+
53+
[DataContract]
54+
public class QrCodeSetupData : ISetupTwoFactorModel
3955
{
4056
/// <summary>
4157
/// The secret unique code for the user and this ITwoFactorProvider.
4258
/// </summary>
43-
public string Secret { get; init; }
59+
public string? Secret { get; init; }
4460

4561
/// <summary>
4662
/// The SetupCode from the GoogleAuthenticator code.
4763
/// </summary>
48-
public SetupCode SetupCode { get; init; }
64+
public SetupCode? SetupCode { get; init; }
4965
}
5066

5167
/// <summary>
@@ -82,13 +98,14 @@ public class UmbracoAppAuthenticator : ITwoFactorProvider
8298
/// <param name="userOrMemberKey">The key of the user or member</param>
8399
/// <param name="secret">The secret that ensures only this user can connect to the authenticator app</param>
84100
/// <returns>The required data to setup the authenticator app</returns>
85-
public Task<object> GetSetupDataAsync(Guid userOrMemberKey, string secret)
101+
public Task<ISetupTwoFactorModel> GetSetupDataAsync(Guid userOrMemberKey, string secret)
86102
{
87103
var member = _memberService.GetByKey(userOrMemberKey);
88104

105+
var applicationName = "testingOn15";
89106
var twoFactorAuthenticator = new TwoFactorAuthenticator();
90-
SetupCode setupInfo = twoFactorAuthenticator.GenerateSetupCode("My application name", member.Username, secret, false);
91-
return Task.FromResult<object>(new QrCodeSetupData()
107+
SetupCode setupInfo = twoFactorAuthenticator.GenerateSetupCode(applicationName, member.Username, secret, false);
108+
return Task.FromResult<ISetupTwoFactorModel>(new QrCodeSetupData()
92109
{
93110
SetupCode = setupInfo,
94111
Secret = secret
@@ -111,13 +128,15 @@ public class UmbracoAppAuthenticator : ITwoFactorProvider
111128
public bool ValidateTwoFactorSetup(string secret, string token) => ValidateTwoFactorPIN(secret, token);
112129
}
113130
```
114-
{% endcode %}
115131

116-
First, we create a model with the information required to set up the 2FA provider. Then we implement the `ITwoFactorProvider` with the use of the `TwoFactorAuthenticator` from the GoogleAuthenticator NuGet package.
132+
{% endcode %}
117133

118-
Now we need to register the `UmbracoAppAuthenticator` implementation. This can be done on the `IUmbracoBuilder` in your startup or a composer.
134+
4. Update `namespace` on line 7 to match your project.
135+
5. Customize the `applicationName` variable on line 64.
136+
6. Create a Composer and register the `UmbracoAppAuthenticator` implementation as shown below.
119137

120138
{% code title="UmbracoAppAuthenticatorComposer.cs" lineNumbers="true" %}
139+
121140
```csharp
122141
using Umbraco.Cms.Core.Composing;
123142
using Umbraco.Cms.Core.DependencyInjection;
@@ -134,15 +153,28 @@ public class UmbracoAppAuthenticatorComposer : IComposer
134153
}
135154
}
136155
```
156+
137157
{% endcode %}
138158

139-
At this point, the 2FA is active, but no members have set up 2FA yet. The setup of 2FA depends on the type. In the case of App Authenticator, we will add the following to our **view** showing the edit profile of the member.
159+
At this point, the 2FA is active, but no members have set up 2FA yet. The setup of 2FA depends on the type. In the case of App Authenticator, the **view** showing the option to edit member profiles needs to be modified.
160+
161+
{% hint style="info" %}
162+
If you already have a members-only page with the edit profile options, you can skip directly to step 8.
163+
{% endhint %}
164+
165+
7. Add or choose a members-only page that should have the two-factor authentication setup.
166+
* The page needs to be behind the public access.
167+
* The page should **not** be using strongly types models.
168+
8. Open the view file for the selected page.
169+
9. Add the following code:
170+
171+
{% code title="ExampleMemberPage.cshtml" lineNumbers="true" %}
140172

141173
```csharp
142-
@using Umbraco.Cms.Core.Services
143-
@using Umbraco.Cms.Web.Website.Controllers
144-
@using Umbraco.Cms.Web.Website.Models
145-
@using My.Website @* Or whatever your namespace with the QrCodeSetupData model is *@
174+
@using Umbraco.Cms.Core.Services;
175+
@using Umbraco.Cms.Web.Website.Controllers;
176+
@using Umbraco.Cms.Web.Website.Models;
177+
@using My.Website;
146178
@inject MemberModelBuilderFactory memberModelBuilderFactory
147179
@inject ITwoFactorLoginService twoFactorLoginService
148180
@{
@@ -159,6 +191,9 @@ At this point, the 2FA is active, but no members have set up 2FA yet. The setup
159191
foreach (var providerName in providerNames)
160192
{
161193
var setupData = await twoFactorLoginService.GetSetupInfoAsync(profileModel.Key, providerName);
194+
195+
// If the `setupData` is `null` for the specified `providerName` it means the provider is already set up.
196+
// In this case, a button to disable the authentication is shown.
162197
if (setupData is null)
163198
{
164199
@using (Html.BeginUmbracoForm<UmbTwoFactorLoginController>(nameof(UmbTwoFactorLoginController.Disable)))
@@ -167,6 +202,7 @@ At this point, the 2FA is active, but no members have set up 2FA yet. The setup
167202
<button type="submit">Disable @providerName</button>
168203
}
169204
}
205+
// If `setupData` is not `null` the type is checked and the UI for how to set up the App Authenticator is shown.
170206
else if(setupData is QrCodeSetupData qrCodeSetupData)
171207
{
172208
@using (Html.BeginUmbracoForm<UmbTwoFactorLoginController>(nameof(UmbTwoFactorLoginController.ValidateAndSaveSetup)))
@@ -185,25 +221,45 @@ At this point, the 2FA is active, but no members have set up 2FA yet. The setup
185221
}
186222
```
187223

188-
In this razor-code sample, we get the current member's unique key and list all registered `ITwoFactorProvider` implementations.
224+
{% endcode %}
225+
226+
10. Update the `@using` in line 4 to match the namespace of your project.
227+
11. [Optional] Customize the text fields and buttons to match your websites tone of voice (lines 33-39).
228+
229+
![The QR Code is shown along with a field to enter a value to set up the two factor authentication.](images/2fa-members-configuration.png)
230+
231+
### Test the set up for Members
189232

190-
If the `setupData` is `null` for the specified `providerName` it means the provider is already set up. In this case, we show a disable button. Otherwise, we check the type and show the UI for how to set up the App Authenticator. We will show the QR Code and an input field to validate the code from the App Authenticator.
233+
1. Login to the website using a test member.
234+
2. Navigate to the page where the QR code was added.
235+
3. Scan the QR code and add the verification code.
236+
4. Logout of the website.
237+
5. Login and verify that it asks for the two factor authentication.
191238

192-
The last part required is to use the `Login` Partial View snippet.
239+
You can also check that the **Two-factor Authentication** option is checked on the member in the Umbraco backoffice.
240+
241+
![Check the Member profile in the Umbraco backoffice to verify whether two-factor authentication is enabeld.](images/2fa-member-backoffice.png)
193242

194243
### Notification when 2FA is requested for a member
195244

196245
When a 2FA login is requested for a member, the `MemberTwoFactorRequestedNotification` is published. This notification can also be used to send the member a one-time password via e-mail or phone. Even though these 2FA types are [not considered secure](https://docs.microsoft.com/en-us/aspnet/core/security/authentication/mfa?view=aspnetcore-6.0#mfa-sms) as App Authentication, it is still a massive improvement compared to no 2FA.
197246

198247
## Two-factor authentication for Users
199248

200-
Umbraco controls how the UI is for user login and user edits, but will still need a view for configuring each 2FA provider.
249+
The following guide will take you through implementing an option for backoffice users to enable two-factor authentication.
250+
251+
This guide will not cover setting up the UI for user login and edits as this is handled elsewhere in the CMS.
201252

202253
### Example implementation for Authenticator Apps for Users
203254

204-
In the following example, we will use the [GoogleAuthenticator NuGet Package](https://www.nuget.org/packages/GoogleAuthenticator/). Despite the name, this package works for both Google and Microsoft authenticator apps. It can be used to generate the QR code needed to activate the app for the website.
255+
As an example, the guide will use the [GoogleAuthenticator NuGet Package](https://www.nuget.org/packages/GoogleAuthenticator/). This package works for both Google and Microsoft authenticator apps. It can be used to generate the QR code needed to activate the app for the website.
256+
257+
1. Install the GoogleAuthenticator Nuget Package on your project.
258+
2. Create a new file in your project: `UmbracoUserAppAuthenticator.cs`.
259+
3. Update the file with the following code snippet.
260+
261+
{% code title="UmbracoUserAppAuthenticator.cs" lineNumbers="true" %}
205262

206-
{% code title="TwoFactorAuthInfo.cs" lineNumbers="true" %}
207263
```csharp
208264
using System.Runtime.Serialization;
209265
using Google.Authenticator;
@@ -263,8 +319,9 @@ public class UmbracoUserAppAuthenticator : ITwoFactorProvider
263319

264320
ArgumentNullException.ThrowIfNull(user);
265321

322+
var applicationName = "My application name";
266323
var twoFactorAuthenticator = new TwoFactorAuthenticator();
267-
SetupCode setupInfo = twoFactorAuthenticator.GenerateSetupCode("My application name", user.Username, secret, false);
324+
SetupCode setupInfo = twoFactorAuthenticator.GenerateSetupCode(applicationName, user.Username, secret, false);
268325
return Task.FromResult<ISetupTwoFactorModel>(new TwoFactorAuthInfo()
269326
{
270327
QrCodeSetupImageUrl = setupInfo.QrCodeSetupImageUrl,
@@ -288,20 +345,23 @@ public class UmbracoUserAppAuthenticator : ITwoFactorProvider
288345
public bool ValidateTwoFactorSetup(string secret, string token) => ValidateTwoFactorPIN(secret, token);
289346
}
290347
```
348+
291349
{% endcode %}
292350

293-
First, we create a model with the information required to set up the 2FA provider. Then we implement the `ITwoFactorProvider` with the use of the `TwoFactorAuthenticator` from the GoogleAuthenticator NuGet package.
351+
4. Update `namespace` on line 7 to match your project.
352+
5. Customize the `applicationName` variable on line 59.
353+
6. Create a new file in your project: `UmbracoUserAppAuthenticatorComposer.cs`.
354+
7. Implement a new composer and register the `UmbracoUserAppAuthenticator` implementation as shown below.
294355

295-
Now we need to register the `UmbracoUserAppAuthenticator` implementation and the view to show to set up this provider. This can be done on the `IUmbracoBuilder` in your startup or a composer.
356+
{% code title="UmbracoUserAppAuthenticatorComposer.cs" lineNumbers="true" %}
296357

297-
{% code title="UmbracoAppAuthenticatorComposer.cs" lineNumbers="true" %}
298358
```csharp
299359
using Umbraco.Cms.Core.Composing;
300360
using Umbraco.Cms.Core.Security;
301361

302362
namespace My.Website;
303363

304-
public class UmbracoAppAuthenticatorComposer : IComposer
364+
public class UmbracoUserAppAuthenticatorComposer : IComposer
305365
{
306366
public void Compose(IUmbracoBuilder builder)
307367
{
@@ -311,11 +371,18 @@ public class UmbracoAppAuthenticatorComposer : IComposer
311371
}
312372
}
313373
```
374+
314375
{% endcode %}
315376

316-
The last thing required is to register the provider in the Backoffice client so that the user can enable it. This can be done in a `umbraco-package.json` file.
377+
8. Update the `namespace` on line 4 to match your project.
378+
379+
With the 2FA in place, the provider needs to be registered in the backoffice client so the user can use it.
380+
381+
9. Add a new file to your project directory: `~/App_Plugins/TwoFactorProviders/umbraco-package.json`.
382+
10. Add the following code to the new file:
317383

318384
{% code title="~/App_Plugins/TwoFactorProviders/umbraco-package.json" lineNumbers="true" %}
385+
319386
```json
320387
{
321388
"$schema": "../../umbraco-package-schema.json",
@@ -334,27 +401,35 @@ The last thing required is to register the provider in the Backoffice client so
334401
]
335402
}
336403
```
404+
337405
{% endcode %}
338406

339407
At this point, the 2FA is active, but no users have set up 2FA yet.
340408

341-
* Each user can now enable the configured 2fa providers on their user. This can be done from the user panel by clicking the user avatar.
409+
### Test the set up for Users
410+
411+
Each user can now enable the configured 2FA providers on their user.
412+
413+
1. Access the Umbraco backoffice.
414+
2. Click the user avatar in the top-right corner.
342415

343416
![User panel](images/user-panel.jpg)
344417

345-
* When clicking the `Configure Two-Factor` button, a new panel is shown, listing all enabled two-factor providers.
418+
3. Select `Configure Two-Factor` button to get a list of all enabled two-factor providers.
346419

347420
![Configure 2fa](images/configure-2fa.jpg)
348421

349-
* When clicking `Enable` on one of these, the configured view for the specific provider will be shown
422+
4. Select `Enable` to show the configured view.
350423

351424
![Enable 2fa](images/enable-2fa.jpg)
352425

353-
* When the authenticator is enabled correctly, a disable button is shown instead.
426+
5. Follow the instructions to configure 2FA.
427+
428+
When the authenticator is enabled correctly, a disable button is shown instead.
354429

355430
![Disable 2fa](images/disable-2fa.jpg)
356431

357-
* To disable the two-factor authentication on your user, it is required to enter the verification code. Otherwise, admins are allowed to disable providers on other users.
432+
To disable the two-factor authentication on your user, it is required to enter the verification code.
358433

359434
![Verify disable](images/verify-disable.jpg)
360435

0 commit comments

Comments
 (0)