55using ActiveLogin . Authentication . BankId . AspNetCore . Models ;
66using ActiveLogin . Authentication . BankId . AspNetCore . Sign ;
77using ActiveLogin . Authentication . BankId . AspNetCore . StateHandling ;
8+ using ActiveLogin . Authentication . BankId . Core ;
89using ActiveLogin . Authentication . BankId . Core . UserMessage ;
910
1011using Microsoft . AspNetCore . Antiforgery ;
1415namespace ActiveLogin . Authentication . BankId . AspNetCore . Areas . ActiveLogin . Controllers ;
1516
1617[ NonController ]
17- public abstract class BankIdUiControllerBase : Controller
18+ public abstract class BankIdUiControllerBase < T > : Controller
19+ where T : BankIdUiState
1820{
1921 private readonly IAntiforgery _antiforgery ;
2022 private readonly IStringLocalizer < ActiveLoginResources > _localizer ;
2123 private readonly IBankIdUserMessageLocalizer _bankIdUserMessageLocalizer ;
2224 private readonly IBankIdUiOptionsProtector _uiOptionsProtector ;
2325 private readonly IBankIdInvalidStateHandler _bankIdInvalidStateHandler ;
24- private readonly IBankIdUiStateProtector _bankIdUiStateProtector ;
26+ private readonly IStateStorage stateStorage ;
2527
26- protected BankIdUiControllerBase (
28+ public BankIdUiControllerBase (
2729 IAntiforgery antiforgery ,
2830 IStringLocalizer < ActiveLoginResources > localizer ,
2931 IBankIdUserMessageLocalizer bankIdUserMessageLocalizer ,
3032 IBankIdUiOptionsProtector uiOptionsProtector ,
3133 IBankIdInvalidStateHandler bankIdInvalidStateHandler ,
32- IBankIdUiStateProtector bankIdUiStateProtector )
34+ IStateStorage stateStorage
35+ )
3336 {
37+ this . stateStorage = stateStorage ;
3438 _antiforgery = antiforgery ;
3539 _localizer = localizer ;
3640 _bankIdUserMessageLocalizer = bankIdUserMessageLocalizer ;
3741 _uiOptionsProtector = uiOptionsProtector ;
3842 _bankIdInvalidStateHandler = bankIdInvalidStateHandler ;
39- _bankIdUiStateProtector = bankIdUiStateProtector ;
43+ }
44+
45+ protected async Task < T ? > GetUIState ( BankIdUiOptions uiOptions )
46+ {
47+ var cookie = HttpContext . Request . Cookies [ uiOptions . StateKeyCookieName ] ;
48+ if ( cookie is null )
49+ {
50+ return default ;
51+ }
52+ var stateKey = new StateKey ( cookie ) ;
53+ return await stateStorage . GetAsync < T > ( stateKey ) ;
4054 }
4155
4256 protected async Task < ActionResult > Initialize ( string returnUrl , string apiControllerName , string protectedUiOptions , string viewName )
@@ -58,32 +72,40 @@ protected async Task<ActionResult> Initialize(string returnUrl, string apiContro
5872 return new EmptyResult ( ) ;
5973 }
6074
61- var antiforgeryTokens = _antiforgery . GetAndStoreTokens ( HttpContext ) ;
75+ var state = await GetUIState ( uiOptions ) ;
6276
63- var protectedState = Request . Cookies [ uiOptions . StateCookieName ] ;
64- if ( protectedState == null )
77+ if ( state == null )
6578 {
6679 var invalidStateContext = new BankIdInvalidStateContext ( uiOptions . CancelReturnUrl ) ;
6780 await _bankIdInvalidStateHandler . HandleAsync ( invalidStateContext ) ;
6881
6982 return new EmptyResult ( ) ;
7083 }
71- var state = _bankIdUiStateProtector . Unprotect ( protectedState ) ;
7284
85+ var antiforgeryTokens = _antiforgery . GetAndStoreTokens ( HttpContext ) ;
7386 var viewModel = GetUiViewModel ( returnUrl , apiControllerName , protectedUiOptions , uiOptions , state , antiforgeryTokens ) ;
7487
7588 return View ( viewName , viewModel ) ;
7689 }
7790
7891 private bool HasStateCookie ( BankIdUiOptions uiOptions )
7992 {
80- if ( string . IsNullOrEmpty ( uiOptions . StateCookieName )
81- || ! HttpContext . Request . Cookies . ContainsKey ( uiOptions . StateCookieName ) )
93+ if ( string . IsNullOrEmpty ( uiOptions . StateKeyCookieName ) )
94+ {
95+ return false ;
96+ }
97+
98+ if ( ! HttpContext . Request . Cookies . ContainsKey ( uiOptions . StateKeyCookieName ) )
99+ {
100+ return false ;
101+ }
102+
103+ if ( string . IsNullOrEmpty ( HttpContext . Request . Cookies [ uiOptions . StateKeyCookieName ] ) )
82104 {
83105 return false ;
84106 }
85107
86- return ! string . IsNullOrEmpty ( HttpContext . Request . Cookies [ uiOptions . StateCookieName ] ) ;
108+ return true ;
87109 }
88110
89111 private BankIdUiViewModel GetUiViewModel ( string returnUrl , string apiControllerName , string protectedUiOptions , BankIdUiOptions unprotectedUiOptions , BankIdUiState uiState , AntiforgeryTokenSet antiforgeryTokens )
@@ -122,7 +144,7 @@ private BankIdUiViewModel GetUiViewModel(string returnUrl, string apiControllerN
122144 var localizedCancelButtonText = _localizer [ "Cancel_Button" ] ;
123145 var localizedQrCodeImageAltText = _localizer [ "Qr_Code_Image" ] ;
124146
125- if ( uiState is BankIdUiSignState signState )
147+ if ( uiState is BankIdUiSignState signState )
126148 {
127149 var uiSignData = new BankIdUiSignData
128150 {
0 commit comments