1- using System . Collections . Generic ;
1+ // Licensed under the MIT License.
2+ // Copyright (c) 2018-2022 the AppCore .NET project.
3+
4+ using System . Collections . Generic ;
25using System . Security . Authentication ;
36using System . Security . Claims ;
47using System . Threading ;
1215
1316namespace AppCore . Extensions . Http . Authentication . OAuth . AspNetCore ;
1417
18+ /// <summary>
19+ /// Provides the base class for <see cref="IOAuthUserTokenStore"/> which stores tokens in the authentication
20+ /// session.
21+ /// </summary>
22+ /// <typeparam name="TOptions">The type of the <see cref="OAuthUserOptions"/>.</typeparam>
1523public abstract class AuthenticationSessionOAuthUserTokenStore < TOptions > : IOAuthUserTokenStore
1624 where TOptions : OAuthUserOptions
1725{
@@ -23,6 +31,12 @@ public abstract class AuthenticationSessionOAuthUserTokenStore<TOptions> : IOAut
2331 // this requires this service to be added as scoped to the DI system
2432 private readonly Dictionary < string , AuthenticateResult > _cache = new ( ) ;
2533
34+ /// <summary>
35+ /// Initializes a new instance of the <see cref="AuthenticationSessionOAuthUserTokenStore{TOptions}"/> class.
36+ /// </summary>
37+ /// <param name="httpContextAccessor">The <see cref="IHttpContextAccessor"/>.</param>
38+ /// <param name="optionsMonitor">The <see cref="IOptionsMonitor{TOptions}"/>.</param>
39+ /// <param name="logger">The <see cref="ILogger{TCategoryName}"/>.</param>
2640 protected AuthenticationSessionOAuthUserTokenStore (
2741 IHttpContextAccessor httpContextAccessor ,
2842 IOptionsMonitor < TOptions > optionsMonitor ,
@@ -67,7 +81,7 @@ private async Task<string> GetSignInScheme(HttpContext context, TOptions options
6781 return scheme ;
6882 }
6983
70- private async Task < AuthenticateResult ? > TryAuthenticateAsync ( HttpContext httpContext , string signInScheme , TOptions options )
84+ private async Task < AuthenticateResult ? > TryAuthenticateAsync ( HttpContext httpContext , string signInScheme )
7185 {
7286 // check the cache in case the token was re-issued via StoreTokenAsync
7387 if ( ! _cache . TryGetValue ( signInScheme , out AuthenticateResult ? result ) )
@@ -92,13 +106,17 @@ private async Task<string> GetSignInScheme(HttpContext context, TOptions options
92106 return result ;
93107 }
94108
109+ /// <summary>
110+ /// Ensures that the <paramref name="scheme"/> is compatible.
111+ /// </summary>
112+ /// <param name="scheme">The <see cref="AuthenticationScheme"/>.</param>
95113 protected abstract void EnsureCompatibleScheme ( AuthenticationScheme scheme ) ;
96114
115+ /// <inheritdoc />
97116 public async Task StoreTokenAsync (
98117 AuthenticationScheme scheme ,
99118 ClaimsPrincipal user ,
100119 OAuthUserToken token ,
101- OAuthUserParameters ? parameters = null ,
102120 CancellationToken cancellationToken = default )
103121 {
104122 Ensure . Arg . NotNull ( scheme ) ;
@@ -111,12 +129,12 @@ public async Task StoreTokenAsync(
111129 TOptions options = _optionsMonitor . Get ( scheme . Name ) ;
112130 string signInScheme = await GetSignInScheme ( httpContext , options ) ;
113131
114- AuthenticateResult ? result = await TryAuthenticateAsync ( httpContext , signInScheme , options ) ;
132+ AuthenticateResult ? result = await TryAuthenticateAsync ( httpContext , signInScheme ) ;
115133 if ( result == null )
116134 throw new AuthenticationException ( "User is not authenticated, cannot store tokens." ) ;
117135
118136 ClaimsPrincipal principal = result . Principal ! ;
119- SetUserToken ( principal , result . Properties ! , token , options ) ;
137+ StoreToken ( principal , result . Properties ! , token , options ) ;
120138
121139 if ( result . Properties ! . AllowRefresh . GetValueOrDefault ( true ) )
122140 {
@@ -128,16 +146,23 @@ public async Task StoreTokenAsync(
128146 _cache [ signInScheme ] = AuthenticateResult . Success ( new AuthenticationTicket ( principal , result . Properties , signInScheme ) ) ;
129147 }
130148
131- protected abstract void SetUserToken (
149+ /// <summary>
150+ /// Stores the token in the authentication session.
151+ /// </summary>
152+ /// <param name="principal"></param>
153+ /// <param name="properties"></param>
154+ /// <param name="token"></param>
155+ /// <param name="options"></param>
156+ protected abstract void StoreToken (
132157 ClaimsPrincipal principal ,
133158 AuthenticationProperties properties ,
134159 OAuthUserToken token ,
135160 TOptions options ) ;
136161
162+ /// <inheritdoc />
137163 public async Task < OAuthUserToken > GetTokenAsync (
138164 AuthenticationScheme scheme ,
139165 ClaimsPrincipal user ,
140- OAuthUserParameters ? parameters = null ,
141166 CancellationToken cancellationToken = default )
142167 {
143168 Ensure . Arg . NotNull ( scheme ) ;
@@ -149,22 +174,29 @@ public async Task<OAuthUserToken> GetTokenAsync(
149174 TOptions options = _optionsMonitor . Get ( scheme . Name ) ;
150175 string signInScheme = await GetSignInScheme ( httpContext , options ) ;
151176
152- AuthenticateResult ? result = await TryAuthenticateAsync ( httpContext , signInScheme , options ) ;
177+ AuthenticateResult ? result = await TryAuthenticateAsync ( httpContext , signInScheme ) ;
153178 if ( result == null )
154179 throw new AuthenticationException ( "User is not authenticated, cannot get tokens." ) ;
155180
156- return GetUserToken ( result . Principal ! , result . Properties ! , options ) ;
181+ return GetToken ( result . Principal ! , result . Properties ! , options ) ;
157182 }
158183
159- protected abstract OAuthUserToken GetUserToken (
184+ /// <summary>
185+ /// Gets the token from the authentication session.
186+ /// </summary>
187+ /// <param name="principal"></param>
188+ /// <param name="properties"></param>
189+ /// <param name="options"></param>
190+ /// <returns></returns>
191+ protected abstract OAuthUserToken GetToken (
160192 ClaimsPrincipal principal ,
161193 AuthenticationProperties properties ,
162194 TOptions options ) ;
163195
196+ /// <inheritdoc />
164197 public Task ClearTokenAsync (
165198 AuthenticationScheme scheme ,
166199 ClaimsPrincipal user ,
167- OAuthUserParameters ? parameters = null ,
168200 CancellationToken cancellationToken = default )
169201 {
170202 Ensure . Arg . NotNull ( scheme ) ;
0 commit comments