66using Azure ;
77using NUnit . Framework ;
88using Authentication . OAuth2 ;
9- using Azure . Core . Pipeline ;
109using Azure . Core ;
11- using System . Collections . Generic ;
12- using System . Net ;
13- using System . Threading ;
14- using System ;
15- using System . Reflection ;
1610using NUnit . Framework . Internal ;
11+ using static CadlRanchProjects . Tests . OAuth2TestHelper ;
1712
1813namespace CadlRanchProjects . Tests
1914{
@@ -38,90 +33,5 @@ public Task Authentication_OAuth2_invalid() => Test((host) =>
3833 Assert . AreEqual ( 403 , exception . Status ) ;
3934 return Task . CompletedTask ;
4035 } ) ;
41-
42- public class MockCredential : TokenCredential
43- {
44- public override ValueTask < AccessToken > GetTokenAsync ( TokenRequestContext requestContext , CancellationToken cancellationToken )
45- {
46- return new ( GetToken ( requestContext , cancellationToken ) ) ;
47- }
48-
49- public override AccessToken GetToken ( TokenRequestContext requestContext , CancellationToken cancellationToken )
50- {
51- return new AccessToken ( string . Join ( " " , requestContext . Scopes ) , DateTimeOffset . MaxValue ) ;
52- }
53- }
54-
55- // Only for bypassing HTTPS check purpose
56- public class MockBearerTokenAuthenticationPolicy : BearerTokenAuthenticationPolicy
57- {
58- private readonly HttpPipelineTransport _transport ;
59-
60- public MockBearerTokenAuthenticationPolicy ( TokenCredential credential , IEnumerable < string > scopes , HttpPipelineTransport transport ) : base ( credential , scopes )
61- {
62- _transport = transport ;
63- }
64-
65- public override ValueTask ProcessAsync ( HttpMessage message , ReadOnlyMemory < HttpPipelinePolicy > pipeline )
66- {
67- return ProcessAsync ( message , pipeline , true ) ;
68- }
69-
70- public override void Process ( HttpMessage message , ReadOnlyMemory < HttpPipelinePolicy > pipeline )
71- {
72- ProcessAsync ( message , pipeline , false ) . EnsureCompleted ( ) ;
73- }
74-
75- protected new async ValueTask ProcessNextAsync ( HttpMessage message , ReadOnlyMemory < HttpPipelinePolicy > pipeline )
76- {
77- await _transport . ProcessAsync ( message ) . ConfigureAwait ( false ) ;
78-
79- var response = message . Response ;
80- Type responseType = response . GetType ( ) ;
81- PropertyInfo propInfo = responseType . GetProperty ( "IsError" , BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ) ;
82- propInfo . SetValue ( response , message . ResponseClassifier . IsErrorResponse ( message ) ) ;
83- }
84-
85- protected new void ProcessNext ( HttpMessage message , ReadOnlyMemory < HttpPipelinePolicy > pipeline )
86- {
87- _transport . Process ( message ) ;
88- }
89-
90- private async ValueTask ProcessAsync ( HttpMessage message , ReadOnlyMemory < HttpPipelinePolicy > pipeline , bool async )
91- {
92- if ( async)
93- {
94- await AuthorizeRequestAsync ( message ) . ConfigureAwait ( false ) ;
95- await ProcessNextAsync ( message , pipeline ) . ConfigureAwait ( false ) ;
96- }
97- else
98- {
99- AuthorizeRequest ( message ) ;
100- ProcessNext( message , pipeline ) ;
101- }
102-
103- // Check if we have received a challenge or we have not yet issued the first request.
104- if ( message . Response . Status == ( int ) HttpStatusCode . Unauthorized && message . Response . Headers . Contains ( HttpHeader . Names . WwwAuthenticate ) )
105- {
106- // Attempt to get the TokenRequestContext based on the challenge.
107- // If we fail to get the context, the challenge was not present or invalid.
108- // If we succeed in getting the context, authenticate the request and pass it up the policy chain.
109- if ( async )
110- {
111- if ( await AuthorizeRequestOnChallengeAsync ( message ) . ConfigureAwait ( false ) )
112- {
113- await ProcessNextAsync ( message , pipeline ) . ConfigureAwait ( false ) ;
114- }
115- }
116- else
117- {
118- if ( AuthorizeRequestOnChallenge ( message ) )
119- {
120- ProcessNext ( message , pipeline ) ;
121- }
122- }
123- }
124- }
125- }
12636 }
12737}
0 commit comments