Skip to content

Commit 5d68636

Browse files
committed
replace implementation
1 parent 9e1cd8f commit 5d68636

13 files changed

Lines changed: 157 additions & 42 deletions

File tree

samples/Standalone.MvcSample/Standalone.MvcSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@
3131
</ItemGroup>
3232

3333
<ItemGroup>
34-
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
34+
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.23.0" />
3535
</ItemGroup>
3636
</Project>

src/ActiveLogin.Authentication.BankId.AspNetCore/Areas/ActiveLogin/Controllers/BankIdUiControllerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ IStateStorage stateStorage
5050
return default;
5151
}
5252
var stateKey = new StateKey(cookie);
53-
return await stateStorage.ReadAsync(stateKey) as T;
53+
return await stateStorage.GetAsync<T>(stateKey);
5454
}
5555

5656
protected async Task<ActionResult> Initialize(string returnUrl, string apiControllerName, string protectedUiOptions, string viewName)

src/ActiveLogin.Authentication.BankId.AspNetCore/Areas/ActiveLogin/Controllers/BankIdUiSignApiController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,6 @@ public async Task<ActionResult<BankIdUiApiInitializeResponse>> Initialize(BankId
115115
}
116116

117117
var stateKey = new StateKey(cookie);
118-
return stateStorage.ReadAsync<BankIdUiSignState>(stateKey);
118+
return stateStorage.GetAsync<BankIdUiSignState>(stateKey);
119119
}
120120
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected override async Task<HandleRequestResult> HandleRemoteAuthenticateAsync
6767
return await HandleRemoteAuthenticateFail(BankIdConstants.ErrorMessages.InvalidStateCookie, detectedDevice);
6868
}
6969

70-
await DeleteStateCookie();
70+
DeleteStateCookie();
7171

7272
if (!Request.HasFormContentType)
7373
{
@@ -199,7 +199,7 @@ private async Task AppendStateCookie(AuthenticationProperties properties)
199199
}
200200

201201
var state = new BankIdUiAuthState(properties);
202-
var stateKey = await _stateStorage.WriteAsync(state);
202+
var stateKey = await _stateStorage.SetAsync(state);
203203

204204
var cookieOptions = Options.StateCookie.Build(Context, Options.TimeProvider.GetUtcNow());
205205
Response.Cookies.Append(Options.StateCookie.Name, stateKey, cookieOptions);
@@ -215,10 +215,10 @@ private async Task AppendStateCookie(AuthenticationProperties properties)
215215
return Task.FromResult<BankIdUiAuthState?>(null);
216216
}
217217

218-
return _stateStorage.ReadAsync<BankIdUiAuthState>(new(stateKey));
218+
return _stateStorage.GetAsync<BankIdUiAuthState>(new(stateKey));
219219
}
220220

221-
private async Task DeleteStateCookie()
221+
private void DeleteStateCookie()
222222
{
223223
Validators.ThrowIfNullOrWhitespace(Options.StateCookie.Name, BankIdConstants.AuthStateKey);
224224

@@ -227,9 +227,6 @@ private async Task DeleteStateCookie()
227227
throw new InvalidOperationException(BankIdConstants.ErrorMessages.TimeProviderNotSet);
228228
}
229229

230-
var stateKey = Request.Cookies[Options.StateCookie.Name]!;
231-
_ = await _stateStorage.RemoveAsync(new(stateKey));
232-
233230
var cookieOptions = Options.StateCookie.Build(Context, Options.TimeProvider.GetUtcNow());
234231
Response.Cookies.Delete(Options.StateCookie.Name, cookieOptions);
235232
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ private string GetUiInitUrl(HttpContext httpContext, string callbackPath, BankId
169169
}
170170

171171
var stateKey = new StateKey(cookie);
172-
return _bankIdStateStorage.RemoveAsync<BankIdUiSignState>(stateKey);
172+
return _bankIdStateStorage.GetAsync<BankIdUiSignState>(stateKey);
173173
}
174174

175175
private Task<StateKey> SaveState(BankIdSignProperties properties, string configKey)
176176
{
177177
var state = new BankIdUiSignState(configKey, properties);
178-
return _bankIdStateStorage.WriteAsync(state);
178+
return _bankIdStateStorage.SetAsync(state);
179179
}
180180

181181
private void DeleteCookie(HttpContext httpContext, BankIdSignOptions options)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using ActiveLogin.Authentication.BankId.AspNetCore.DataProtection;
44
using ActiveLogin.Authentication.BankId.Core;
55

6+
using Microsoft.Extensions.Caching.Memory;
67
using Microsoft.Extensions.DependencyInjection;
78

89
namespace ActiveLogin.Authentication.BankId.AspNetCore.Sign;
@@ -70,6 +71,7 @@ private static void AddBankIdAuthDefaultServices(IBankIdSignBuilder builder)
7071

7172
BankIdCommonConfiguration.AddDefaultServices(services);
7273

74+
services.AddSingleton<IMemoryCache, MemoryCache>();
7375
services.AddSingleton<IStateStorage, InMemoryStateStorage>();
7476
services.AddTransient<IBankIdUiResultProtector, BankIdUiResultProtector>();
7577

src/ActiveLogin.Authentication.BankId.AspNetCore/StateStorageExtensions.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ namespace ActiveLogin.Authentication.BankId.AspNetCore;
55

66
internal static class StateStorageExtensions
77
{
8-
public static Task<T?> ReadAsync<T>(this IStateStorage stateStorage, StateKey key)
9-
where T : BankIdUiState
10-
{
11-
return stateStorage.ReadAsync(key).ContinueWith(t => t.Result as T);
12-
}
8+
// public static Task<T?> ReadAsync<T>(this IStateStorage stateStorage, StateKey key)
9+
// where T : BankIdUiState
10+
// {
11+
// return stateStorage.GetAsync(key);
12+
// }
1313

14-
public static Task<T?> RemoveAsync<T>(this IStateStorage stateStorage, StateKey key)
15-
where T : BankIdUiState
16-
{
17-
return stateStorage.RemoveAsync(key).ContinueWith(t => t.Result as T);
18-
}
14+
// public static Task<T?> RemoveAsync<T>(this IStateStorage stateStorage, StateKey key)
15+
// where T : BankIdUiState
16+
// {
17+
// return stateStorage.TryGetAsync(key);
18+
// }
1919
}

src/ActiveLogin.Authentication.BankId.AzureMonitor/ActiveLogin.Authentication.BankId.AzureMonitor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<ItemGroup>
1414
<FrameworkReference Include="Microsoft.AspNetCore.App" />
15-
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
15+
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.23.0" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

src/ActiveLogin.Authentication.BankId.Core/ActiveLogin.Authentication.BankId.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<ItemGroup>
1818
<PackageReference Include="Microsoft.Bcl.Cryptography" Version="9.0.2" />
19+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.2" />
1920
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.2" />
2021
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
2122
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />

src/ActiveLogin.Authentication.BankId.Core/IStateStorage.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
13
using Microsoft.Extensions.DependencyInjection;
24

35
namespace ActiveLogin.Authentication.BankId.Core;
@@ -9,9 +11,9 @@ public readonly record struct StateKey(string Key)
911

1012
public interface IStateStorage
1113
{
12-
Task<StateKey> WriteAsync(object value);
13-
Task<object?> ReadAsync(StateKey key);
14-
Task<object?> RemoveAsync(StateKey key);
14+
Task<T?> GetAsync<T>(StateKey key);
15+
Task<bool> TryGetAsync<T>(StateKey key, [NotNullWhen(true)] out T? value);
16+
Task<StateKey> SetAsync<T>(T value);
1517
}
1618

1719
public static class BankIdBuilderStateStorageExtensions

0 commit comments

Comments
 (0)