13
13
using System . Security . Claims ;
14
14
using System . Threading . Tasks ;
15
15
using System . Xml . Linq ;
16
+ using AspNet . Security . OAuth . Apple ;
16
17
using AspNet . Security . OAuth . Infrastructure ;
17
18
using JustEat . HttpClientInterception ;
18
19
using MartinCostello . Logging . XUnit ;
22
23
using Microsoft . AspNetCore . Http ;
23
24
using Microsoft . AspNetCore . Mvc . Testing ;
24
25
using Microsoft . Extensions . DependencyInjection ;
26
+ using Microsoft . Extensions . DependencyInjection . Extensions ;
25
27
using Shouldly ;
26
28
using Xunit ;
27
29
using Xunit . Abstractions ;
@@ -150,7 +152,7 @@ void ConfigureServices(IServiceCollection services)
150
152
return Task . CompletedTask ;
151
153
} ;
152
154
153
- if ( options is Apple . AppleAuthenticationOptions appleOptions )
155
+ if ( options is AppleAuthenticationOptions appleOptions )
154
156
{
155
157
appleOptions . JwtSecurityTokenHandler = new FrozenJwtSecurityTokenHandler ( ) ;
156
158
}
@@ -166,6 +168,60 @@ void ConfigureServices(IServiceCollection services)
166
168
onCreatingTicketEventRaised . ShouldBeTrue ( ) ;
167
169
}
168
170
171
+ [ Fact ]
172
+ public async Task OnCreatingTicket_Is_Raised_By_Handler_Using_Custom_Events_Type ( )
173
+ {
174
+ // Arrange
175
+ bool onCreatingTicketEventRaised = false ;
176
+
177
+ void ConfigureServices ( IServiceCollection services )
178
+ {
179
+ services . TryAddScoped ( ( _ ) =>
180
+ {
181
+ return new CustomOAuthEvents ( )
182
+ {
183
+ OnCreatingTicket = ( context ) =>
184
+ {
185
+ onCreatingTicketEventRaised = true ;
186
+ return Task . CompletedTask ;
187
+ }
188
+ } ;
189
+ } ) ;
190
+ services . TryAddScoped ( ( _ ) =>
191
+ {
192
+ return new CustomAppleAuthenticationEvents ( )
193
+ {
194
+ OnCreatingTicket = ( context ) =>
195
+ {
196
+ onCreatingTicketEventRaised = true ;
197
+ return Task . CompletedTask ;
198
+ }
199
+ } ;
200
+ } ) ;
201
+
202
+ services . PostConfigureAll < TOptions > ( ( options ) =>
203
+ {
204
+ if ( options is AppleAuthenticationOptions appleOptions )
205
+ {
206
+ appleOptions . EventsType = typeof ( CustomAppleAuthenticationEvents ) ;
207
+ appleOptions . JwtSecurityTokenHandler = new FrozenJwtSecurityTokenHandler ( ) ;
208
+ }
209
+ else
210
+ {
211
+ options . EventsType = typeof ( CustomOAuthEvents ) ;
212
+ }
213
+ } ) ;
214
+ }
215
+
216
+ using var server = CreateTestServer ( ConfigureServices ) ;
217
+
218
+ // Act
219
+ var claims = await AuthenticateUserAsync ( server ) ;
220
+
221
+ // Assert
222
+ onCreatingTicketEventRaised . ShouldBeTrue ( ) ;
223
+ }
224
+
169
225
/// <summary>
170
226
/// Run the ChannelAsync for authentication
171
227
/// </summary>
@@ -252,5 +308,13 @@ protected void AssertClaim(IDictionary<string, Claim> actual, string claimType,
252
308
actualValue . ShouldBe ( claimValue ) ;
253
309
}
254
310
}
311
+
312
+ private sealed class CustomOAuthEvents : OAuthEvents
313
+ {
314
+ }
315
+
316
+ private sealed class CustomAppleAuthenticationEvents : AppleAuthenticationEvents
317
+ {
318
+ }
255
319
}
256
320
}
0 commit comments