11using System . Security . Claims ;
22using System . Text . Encodings . Web ;
33
4- using ActiveLogin . Authentication . BankId . Api . Models ;
54using ActiveLogin . Authentication . BankId . AspNetCore . ClaimsTransformation ;
65using ActiveLogin . Authentication . BankId . AspNetCore . DataProtection ;
76using ActiveLogin . Authentication . BankId . AspNetCore . Helpers ;
@@ -33,49 +32,40 @@ public class BankIdAuthHandler(
3332 IBankIdEventTrigger bankIdEventTrigger ,
3433 IBankIdSupportedDeviceDetector bankIdSupportedDeviceDetector ,
3534 IEnumerable < IBankIdClaimsTransformer > bankIdClaimsTransformers ,
36- IStateStorage < BankIdUiAuthState > stateStorage
35+ IStateStorage stateStorage
3736) : RemoteAuthenticationHandler < BankIdAuthOptions > ( options , loggerFactory , encoder )
3837{
39- private const string StateCookieNameParameterName = "StateCookie.Name" ;
4038 private readonly PathString _authPath = new ( $ "/{ BankIdConstants . Routes . ActiveLoginAreaName } /{ BankIdConstants . Routes . BankIdPathName } /{ BankIdConstants . Routes . BankIdAuthControllerPath } ") ;
41-
42- private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor ;
43- private readonly IAntiforgery _antiforgery = antiforgery ;
44- private readonly IBankIdUiOptionsProtector _uiOptionsProtector = uiOptionsProtector ;
45- private readonly IBankIdUiResultProtector _uiResultProtector = uiResultProtector ;
46- private readonly IBankIdEventTrigger _bankIdEventTrigger = bankIdEventTrigger ;
47- private readonly IBankIdSupportedDeviceDetector _bankIdSupportedDeviceDetector = bankIdSupportedDeviceDetector ;
48- private readonly IStateStorage < BankIdUiAuthState > _stateStorage = stateStorage ;
49- private readonly List < IBankIdClaimsTransformer > _bankIdClaimsTransformers = bankIdClaimsTransformers . ToList ( ) ;
39+ private readonly List < IBankIdClaimsTransformer > bankIdClaimsTransformers = bankIdClaimsTransformers . ToList ( ) ;
5040
5141 protected override async Task < HandleRequestResult > HandleRemoteAuthenticateAsync ( )
5242 {
53- var detectedDevice = _bankIdSupportedDeviceDetector . Detect ( ) ;
43+ var detectedDevice = bankIdSupportedDeviceDetector . Detect ( ) ;
5444
5545 var state = await GetStateFromCookie ( ) ;
5646 if ( state == null )
5747 {
5848 return await HandleRemoteAuthenticateFail ( BankIdConstants . ErrorMessages . InvalidStateCookie , detectedDevice ) ;
5949 }
6050
61- DeleteStateCookie ( ) ;
51+ await DeleteStateCookie ( ) ;
6252
6353 if ( ! Request . HasFormContentType )
6454 {
6555 await HandleRemoteAuthenticateFail ( BankIdConstants . ErrorMessages . InvalidUiResult , detectedDevice ) ;
6656 throw new ArgumentException ( BankIdConstants . ErrorMessages . InvalidUiResult ) ;
6757 }
6858
69- var httpContext = _httpContextAccessor . HttpContext ?? throw new InvalidOperationException ( BankIdConstants . ErrorMessages . CouldNotAccessHttpContext ) ;
70- await _antiforgery . ValidateRequestAsync ( httpContext ) ;
59+ var httpContext = httpContextAccessor . HttpContext ?? throw new InvalidOperationException ( BankIdConstants . ErrorMessages . CouldNotAccessHttpContext ) ;
60+ await antiforgery . ValidateRequestAsync ( httpContext ) ;
7161
7262 var protectedUiResult = Request . Form [ BankIdConstants . FormParameters . UiResult ] ;
7363 if ( StringValues . IsNullOrEmpty ( protectedUiResult ) )
7464 {
7565 return await HandleRemoteAuthenticateFail ( BankIdConstants . ErrorMessages . InvalidUiResult , detectedDevice ) ;
7666 }
7767
78- var uiResult = _uiResultProtector . Unprotect ( protectedUiResult . ToString ( ) ) ;
68+ var uiResult = uiResultProtector . Unprotect ( protectedUiResult . ToString ( ) ) ;
7969 if ( ! uiResult . IsSuccessful )
8070 {
8171 return await HandleRemoteAuthenticateFail ( BankIdConstants . ErrorMessages . InvalidUiResult , detectedDevice ) ;
@@ -84,7 +74,7 @@ protected override async Task<HandleRequestResult> HandleRemoteAuthenticateAsync
8474 var properties = state . AuthenticationProperties ;
8575 var ticket = await GetAuthenticationTicket ( uiResult , properties ) ;
8676
87- await _bankIdEventTrigger . TriggerAsync ( new BankIdAspNetAuthenticateSuccessEvent (
77+ await bankIdEventTrigger . TriggerAsync ( new BankIdAspNetAuthenticateSuccessEvent (
8878 PersonalIdentityNumber . Parse ( uiResult . PersonalIdentityNumber ) ,
8979 detectedDevice
9080 ) ) ;
@@ -94,7 +84,7 @@ await _bankIdEventTrigger.TriggerAsync(new BankIdAspNetAuthenticateSuccessEvent(
9484
9585 private async Task < HandleRequestResult > HandleRemoteAuthenticateFail ( string reason , BankIdSupportedDevice detectedDevice )
9686 {
97- await _bankIdEventTrigger . TriggerAsync ( new BankIdAspNetAuthenticateFailureEvent ( reason , detectedDevice ) ) ;
87+ await bankIdEventTrigger . TriggerAsync ( new BankIdAspNetAuthenticateFailureEvent ( reason , detectedDevice ) ) ;
9888
9989 return HandleRequestResult . Fail ( reason ) ;
10090 }
@@ -134,7 +124,7 @@ private async Task<IEnumerable<Claim>> GetClaims(BankIdUiResult uiAuthResult)
134124 uiAuthResult . GetCompletionData ( )
135125 ) ;
136126
137- foreach ( var transformer in _bankIdClaimsTransformers )
127+ foreach ( var transformer in bankIdClaimsTransformers )
138128 {
139129 await transformer . TransformClaims ( context ) ;
140130 }
@@ -157,8 +147,8 @@ protected override async Task HandleChallengeAsync(AuthenticationProperties prop
157147 Options . StateCookie . Name ?? string . Empty
158148 ) ;
159149
160- var detectedDevice = _bankIdSupportedDeviceDetector . Detect ( ) ;
161- await _bankIdEventTrigger . TriggerAsync ( new BankIdAspNetChallengeSuccessEvent ( detectedDevice , uiOptions . ToBankIdFlowOptions ( ) ) ) ;
150+ var detectedDevice = bankIdSupportedDeviceDetector . Detect ( ) ;
151+ await bankIdEventTrigger . TriggerAsync ( new BankIdAspNetChallengeSuccessEvent ( detectedDevice , uiOptions . ToBankIdFlowOptions ( ) ) ) ;
162152
163153 var loginUrl = GetInitUiUrl ( uiOptions ) ;
164154 Response . Redirect ( loginUrl ) ;
@@ -169,7 +159,7 @@ private string GetInitUiUrl(BankIdUiOptions uiOptions)
169159 var pathBase = Context . Request . PathBase ;
170160 var authUrl = pathBase . Add ( _authPath ) ;
171161 var returnUrl = pathBase . Add ( Options . CallbackPath ) ;
172- var protectedUiOptions = _uiOptionsProtector . Protect ( uiOptions ) ;
162+ var protectedUiOptions = uiOptionsProtector . Protect ( uiOptions ) ;
173163
174164 var queryBuilder = new QueryBuilder ( new Dictionary < string , string >
175165 {
@@ -182,42 +172,45 @@ private string GetInitUiUrl(BankIdUiOptions uiOptions)
182172
183173 private async Task AppendStateCookie ( AuthenticationProperties properties )
184174 {
185- Validators . ThrowIfNullOrWhitespace ( Options . StateCookie . Name , StateCookieNameParameterName ) ;
175+ Validators . ThrowIfNullOrWhitespace ( Options . StateCookie . Name , BankIdConstants . AuthStateKey ) ;
186176
187177 if ( Options . TimeProvider == null )
188178 {
189179 throw new InvalidOperationException ( BankIdConstants . ErrorMessages . TimeProviderNotSet ) ;
190180 }
191181
192182 var state = new BankIdUiAuthState ( properties ) ;
193- var stateKey = await _stateStorage . WriteAsync ( state ) ;
183+ var stateKey = await stateStorage . WriteAsync ( state ) ;
194184
195185 var cookieOptions = Options . StateCookie . Build ( Context , Options . TimeProvider . GetUtcNow ( ) ) ;
196186 Response . Cookies . Append ( Options . StateCookie . Name , stateKey , cookieOptions ) ;
197187 }
198188
199189 private Task < BankIdUiAuthState ? > GetStateFromCookie ( )
200190 {
201- Validators . ThrowIfNullOrWhitespace ( Options . StateCookie . Name , StateCookieNameParameterName ) ;
191+ Validators . ThrowIfNullOrWhitespace ( Options . StateCookie . Name , BankIdConstants . AuthStateKey ) ;
202192
203193 var stateKey = Request . Cookies [ Options . StateCookie . Name ] ;
204194 if ( string . IsNullOrEmpty ( stateKey ) )
205195 {
206196 return Task . FromResult < BankIdUiAuthState ? > ( null ) ;
207197 }
208198
209- return _stateStorage . ReadAsync ( new ( stateKey ) ) ;
199+ return stateStorage . ReadAsync < BankIdUiAuthState > ( new ( stateKey ) ) ;
210200 }
211201
212- private void DeleteStateCookie ( )
202+ private async Task DeleteStateCookie ( )
213203 {
214- Validators . ThrowIfNullOrWhitespace ( Options . StateCookie . Name , StateCookieNameParameterName ) ;
204+ Validators . ThrowIfNullOrWhitespace ( Options . StateCookie . Name , BankIdConstants . AuthStateKey ) ;
215205
216206 if ( Options . TimeProvider == null )
217207 {
218208 throw new InvalidOperationException ( BankIdConstants . ErrorMessages . TimeProviderNotSet ) ;
219209 }
220210
211+ var stateKey = Request . Cookies [ Options . StateCookie . Name ] ! ;
212+ _ = await stateStorage . RemoveAsync ( new ( stateKey ) ) ;
213+
221214 var cookieOptions = Options . StateCookie . Build ( Context , Options . TimeProvider . GetUtcNow ( ) ) ;
222215 Response . Cookies . Delete ( Options . StateCookie . Name , cookieOptions ) ;
223216 }
0 commit comments