Skip to content

Commit b361e3e

Browse files
elinohlssonElin FokineRobert Folkessonrfolkes
authored
Feature/return risk (#487)
* Implement return risk. * Update documentation about adding risk to claims. * fix doc spelling err --------- Co-authored-by: Elin Fokine <ElinO@activesolution.se> Co-authored-by: Robert Folkesson <robert.folkesson@activesolution.se> Co-authored-by: Robert Folkesson <robert.folkesson@gmail.com>
1 parent 8ba6351 commit b361e3e

12 files changed

Lines changed: 76 additions & 16 deletions

File tree

docs/articles/bankid.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ The root CA-certificates specified in _BankID Relying Party Guidelines_ (#7 for
7777
It is expected that you have a basic understanding of how [ASP.NET](https://docs.microsoft.com/en-us/aspnet/core/), [ASP.NET MVC](https://docs.microsoft.com/en-us/aspnet/core/mvc/overview) and [ASP.NET Authentication](https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity) works before getting started.
7878

7979

80-
Active Login is designed to make it very easy to get started with BankID, but in the end you are responsible for making sure that you are complient with the technical guidelines and/or legal agreements.
80+
Active Login is designed to make it very easy to get started with BankID, but in the end you are responsible for making sure that you are compliant with the technical guidelines and/or legal agreements.
8181

8282
Therefore, before you start using Active Login, please read the documentation relevant to your needs. This will also make sure you understand the concepts better.
8383

@@ -473,6 +473,13 @@ BankId options allows you to set and override some options such as the below req
473473
// the transaction will be blocked. The risk indication requires that the endUserIp is correct.
474474
// An incorrect IP-address will result in legitimate transactions being blocked.
475475
options.BankIdAllowedRiskLevel = BankIdAllowedRiskLevel.Low;
476+
477+
// If this is set to true a risk indicator will be included in the collect response when the order completes.
478+
// If a risk indicator is required for the order to complete, for example, if a risk requirement is applied,
479+
// the returnRisk property is ignored, and a risk indicator is always included; otherwise a default value of
480+
// false is used. The risk indication requires that the endUserIp is correct. Please note that the assessed
481+
// risk will not be returned if the order was blocked, which may happen if a risk requirement is set.
482+
options.BankIdReturnRisk = true;
476483
});
477484
```
478485

@@ -482,6 +489,7 @@ If you want to apply some options for all BankID schemes, you can do so by using
482489
.Configure(options =>
483490
{
484491
options.BankIdRequireMrtd = true;
492+
options.BankIdReturnRisk = true;
485493
});
486494
```
487495

@@ -666,6 +674,22 @@ public class BankIdPinHintClaimsTransformer : IBankIdClaimsTransformer
666674
}
667675
```
668676

677+
#### Example: Add risk claim
678+
679+
If the application whats to act on the evaluated risk level for the transaction it could be extracted from the completion data returned by BankID.
680+
681+
```csharp
682+
public class BankIdTxnClaimsTransformer : IBankIdClaimsTransformer
683+
{
684+
public Task TransformClaims(BankIdClaimsTransformationContext context)
685+
{
686+
if (context.BankIdCompletionData != null && context.BankIdCompletionData.Risk != null)
687+
context.AddClaim("user_risk", context.BankIdCompletionData.Risk);
688+
689+
return Task.CompletedTask;
690+
}
691+
}
692+
```
669693

670694
### Return URL for cancellation
671695

src/ActiveLogin.Authentication.BankId.AspNetCore/Auth/BankIdAuthHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ protected override async Task HandleChallengeAsync(AuthenticationProperties prop
163163
Options.BankIdSameDevice,
164164
Options.BankIdRequirePinCode,
165165
Options.BankIdRequireMrtd,
166+
Options.BankIdReturnRisk,
166167
BankIdHandlerHelper.GetCancelReturnUrl(properties.Items),
167168
Options.StateCookie.Name ?? string.Empty
168169
);

src/ActiveLogin.Authentication.BankId.AspNetCore/Auth/BankIdAuthOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ public class BankIdAuthOptions : RemoteAuthenticationOptions
3939
/// </summary>
4040
public BankIdAllowedRiskLevel BankIdAllowedRiskLevel { get; set; } = BankIdAllowedRiskLevel.NoRiskLevel;
4141

42+
/// <summary>
43+
/// If this is set to true a risk indicator will be included in the collect response when the order completes.
44+
/// If a risk indicator is required for the order to complete, for example, if a risk requirement is applied,
45+
/// the returnRisk property is ignored, and a risk indicator is always included; otherwise a default value of
46+
/// false is used. The risk indication requires that the endUserIp is correct. Please note that the assessed
47+
/// risk will not be returned if the order was blocked, which may happen if a risk requirement is set.
48+
/// </summary>
49+
public bool BankIdReturnRisk { get; set; } = false;
50+
4251
/// <summary>
4352
/// Auto launch the BankID app on the current device.
4453
/// </summary>

src/ActiveLogin.Authentication.BankId.AspNetCore/DataProtection/Serialization/BankIdUiOptionsSerializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ protected override void Write(BinaryWriter writer, BankIdUiOptions model)
1515
writer.Write(model.SameDevice);
1616
writer.Write(model.RequirePinCode);
1717
writer.Write(model.RequireMrtd);
18+
writer.Write(model.ReturnRisk);
1819
writer.Write(model.CancelReturnUrl);
1920
writer.Write(model.StateCookieName);
2021
}
@@ -38,6 +39,7 @@ protected override BankIdUiOptions Read(BinaryReader reader)
3839
reader.ReadBoolean(),
3940
reader.ReadBoolean(),
4041
reader.ReadBoolean(),
42+
reader.ReadBoolean(),
4143
reader.ReadString(),
4244
reader.ReadString()
4345
);

src/ActiveLogin.Authentication.BankId.AspNetCore/Models/BankIdUiOptions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public BankIdUiOptions(
1111
bool sameDevice,
1212
bool requirePinCode,
1313
bool requireMrtd,
14+
bool returnRisk,
1415
string cancelReturnUrl,
1516
string stateCookieName)
1617
{
@@ -19,6 +20,7 @@ public BankIdUiOptions(
1920
SameDevice = sameDevice;
2021
RequirePinCode = requirePinCode;
2122
RequireMrtd = requireMrtd;
23+
ReturnRisk = returnRisk;
2224
CancelReturnUrl = cancelReturnUrl;
2325
StateCookieName = stateCookieName;
2426
}
@@ -31,6 +33,8 @@ public BankIdUiOptions(
3133

3234
public bool RequireMrtd { get; }
3335

36+
public bool ReturnRisk { get; }
37+
3438
public BankIdAllowedRiskLevel AllowedRiskLevel { get; }
3539

3640
public string CancelReturnUrl { get; }

src/ActiveLogin.Authentication.BankId.AspNetCore/Models/BankIdUiOptionsExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public static class BankIdUiOptionsExtensions
99
options.AllowedRiskLevel,
1010
options.SameDevice,
1111
options.RequirePinCode,
12-
options.RequireMrtd
12+
options.RequireMrtd,
13+
options.ReturnRisk
1314
);
1415
}

src/ActiveLogin.Authentication.BankId.AspNetCore/Sign/BankIdSignOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ public class BankIdSignOptions
2323
/// </summary>
2424
public bool BankIdRequireMrtd { get; set; } = false;
2525

26+
/// <summary>
27+
/// If this is set to true a risk indicator will be included in the collect response when the order completes.
28+
/// If a risk indicator is required for the order to complete, for example, if a risk requirement is applied,
29+
/// the returnRisk property is ignored, and a risk indicator is always included; otherwise a default value of
30+
/// false is used. The risk indication requires that the endUserIp is correct. Please note that the assessed
31+
/// risk will not be returned if the order was blocked, which may happen if a risk requirement is set.
32+
/// </summary>
33+
public bool BankIdReturnRisk { get; set; } = false;
34+
2635
/// <summary>
2736
/// Set the acceptable risk level for the transaction. If the risk is higher than the specified level,
2837
/// the transaction will be blocked. The risk indication requires that the endUserIp is correct.

src/ActiveLogin.Authentication.BankId.AspNetCore/Sign/BankIdSignService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public Task InitiateSignAsync(BankIdSignProperties properties, string callbackPa
6767
options.BankIdSameDevice,
6868
options.BankIdRequirePinCode,
6969
options.BankIdRequireMrtd,
70+
options.BankIdReturnRisk,
7071
BankIdHandlerHelper.GetCancelReturnUrl(properties.Items),
7172
options.StateCookie.Name ?? string.Empty
7273
);

src/ActiveLogin.Authentication.BankId.Core/Flow/BankIdFlowService.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ private async Task<AuthRequest> GetAuthRequest(BankIdFlowOptions flowOptions)
115115
var requirePinCode = resolvedRequirements.RequirePinCode ?? flowOptions.RequirePinCode;
116116
var requestRequirement = new Requirement(resolvedCertificatePolicies, resolvedRiskLevel, requirePinCode, requireMrtd, requiredPersonalIdentityNumber?.To12DigitString());
117117

118+
var returnRisk = flowOptions.ReturnRisk;
119+
118120
var authRequestContext = new BankIdAuthRequestContext(endUserIp, requestRequirement);
119121
var userData = await _bankIdAuthUserDataResolver.GetUserDataAsync(authRequestContext);
120122
var (webDeviceData, appDeviceData) = GetDeviceData();
@@ -126,7 +128,7 @@ private async Task<AuthRequest> GetAuthRequest(BankIdFlowOptions flowOptions)
126128
userData.UserNonVisibleData,
127129
userData.UserVisibleDataFormat,
128130
returnUrl: null,
129-
returnRisk: null,
131+
returnRisk: returnRisk,
130132
web: webDeviceData,
131133
app: appDeviceData
132134
);
@@ -181,6 +183,9 @@ private SignRequest GetSignRequest(BankIdFlowOptions flowOptions, BankIdSignData
181183
var requireMrtd = bankIdSignData.RequireMrtd ?? flowOptions.RequireMrtd;
182184
var requirePinCode = bankIdSignData.RequirePinCode ?? flowOptions.RequirePinCode;
183185
var requestRequirement = new Requirement(resolvedCertificatePolicies, resolvedRiskLevel, requirePinCode, requireMrtd, requiredPersonalIdentityNumber?.To12DigitString());
186+
187+
var returnRisk = flowOptions.ReturnRisk;
188+
184189
var (webDeviceData, appDeviceData) = GetDeviceData();
185190

186191
return new SignRequest(
@@ -190,7 +195,7 @@ private SignRequest GetSignRequest(BankIdFlowOptions flowOptions, BankIdSignData
190195
userVisibleDataFormat: bankIdSignData.UserVisibleDataFormat,
191196
requirement: requestRequirement,
192197
returnUrl: null,
193-
returnRisk: null,
198+
returnRisk: returnRisk,
194199
web: webDeviceData,
195200
app: appDeviceData
196201
);

src/ActiveLogin.Authentication.BankId.Core/Models/BankIdFlowOptions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ public BankIdFlowOptions(
1212
bool sameDevice,
1313
bool requirePinCode,
1414
bool requireMrtd,
15+
bool requireRisk,
1516
PersonalIdentityNumber? requiredPersonalIdentityNumber = null)
1617
{
1718
CertificatePolicies = certificatePolicies;
1819
AllowedRiskLevel = allowedRiskLevel;
1920
SameDevice = sameDevice;
2021
RequirePinCode = requirePinCode;
2122
RequireMrtd = requireMrtd;
23+
ReturnRisk = requireRisk;
2224
RequiredPersonalIdentityNumber = requiredPersonalIdentityNumber;
2325
}
2426

@@ -30,6 +32,8 @@ public BankIdFlowOptions(
3032

3133
public bool RequireMrtd { get; }
3234

35+
public bool ReturnRisk { get; }
36+
3337
public BankIdAllowedRiskLevel AllowedRiskLevel { get; }
3438

3539
public PersonalIdentityNumber? RequiredPersonalIdentityNumber { get; }

0 commit comments

Comments
 (0)