42
42
43
43
namespace OAuth2Test
44
44
{
45
- public enum Mode
46
- {
47
- uaa ,
48
- keycloak
49
- }
50
-
51
- public class OAuth2Options
52
- {
53
- private readonly Mode _mode ;
54
-
55
- public OAuth2Options ( Mode mode )
56
- {
57
- _mode = mode ;
58
- }
59
-
60
- public string Name
61
- {
62
- get
63
- {
64
- switch ( _mode )
65
- {
66
- case Mode . uaa :
67
- return "uaa" ;
68
- case Mode . keycloak :
69
- return "keycloak" ;
70
- default :
71
- throw new InvalidOperationException ( ) ;
72
- }
73
- }
74
- }
75
-
76
- public string ClientId => "producer" ;
77
-
78
- public string ClientSecret
79
- {
80
- get
81
- {
82
- switch ( _mode )
83
- {
84
- case Mode . uaa :
85
- return "producer_secret" ;
86
- case Mode . keycloak :
87
- return "kbOFBXI9tANgKUq8vXHLhT6YhbivgXxn" ;
88
- default :
89
- throw new InvalidOperationException ( ) ;
90
- }
91
- }
92
- }
93
-
94
- public string Scope
95
- {
96
- get
97
- {
98
- switch ( _mode )
99
- {
100
- case Mode . uaa :
101
- return string . Empty ;
102
- case Mode . keycloak :
103
- return "rabbitmq:configure:*/* rabbitmq:read:*/* rabbitmq:write:*/*" ;
104
- default :
105
- throw new InvalidOperationException ( ) ;
106
- }
107
- }
108
- }
109
-
110
- public string TokenEndpoint // => _mode switch
111
- {
112
- get
113
- {
114
- switch ( _mode )
115
- {
116
- case Mode . uaa :
117
- return "http://localhost:8080/oauth/token" ;
118
- case Mode . keycloak :
119
- return "http://localhost:8080/realms/test/protocol/openid-connect/token" ;
120
- default :
121
- throw new InvalidOperationException ( ) ;
122
- }
123
- }
124
- }
125
-
126
- public int TokenExpiresInSeconds => 60 ;
127
- }
128
-
129
45
public class TestOAuth2 : IAsyncLifetime
130
46
{
131
47
private const string Exchange = "test_direct" ;
@@ -134,10 +50,10 @@ public class TestOAuth2 : IAsyncLifetime
134
50
private readonly ITestOutputHelper _testOutputHelper ;
135
51
private readonly IConnectionFactory _connectionFactory ;
136
52
private readonly int _tokenExpiresInSeconds ;
137
- private readonly OAuth2ClientCredentialsProvider _credentialsProvider ;
53
+ private readonly OAuth2ClientCredentialsProvider _producerCredentialsProvider ;
54
+ private readonly OAuth2ClientCredentialsProvider _httpApiCredentialsProvider ;
138
55
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource ( ) ;
139
56
140
- private Util ? _util ;
141
57
private IConnection ? _connection ;
142
58
private CredentialsRefresher ? _credentialsRefresher ;
143
59
@@ -147,18 +63,21 @@ public TestOAuth2(ITestOutputHelper testOutputHelper)
147
63
148
64
string modeStr = Environment . GetEnvironmentVariable ( "OAUTH2_MODE" ) ?? "uaa" ;
149
65
Mode mode = ( Mode ) Enum . Parse ( typeof ( Mode ) , modeStr . ToLowerInvariant ( ) ) ;
150
- var options = new OAuth2Options ( mode ) ;
151
66
152
- _credentialsProvider = GetCredentialsProvider ( options ) ;
67
+ var producerOptions = new OAuth2ProducerOptions ( mode ) ;
68
+ _producerCredentialsProvider = GetCredentialsProvider ( producerOptions ) ;
69
+
70
+ var httpApiOptions = new OAuth2HttpApiOptions ( mode ) ;
71
+ _httpApiCredentialsProvider = GetCredentialsProvider ( httpApiOptions ) ;
153
72
154
73
_connectionFactory = new ConnectionFactory
155
74
{
156
75
AutomaticRecoveryEnabled = true ,
157
- CredentialsProvider = _credentialsProvider ,
76
+ CredentialsProvider = _producerCredentialsProvider ,
158
77
ClientProvidedName = nameof ( TestOAuth2 )
159
78
} ;
160
79
161
- _tokenExpiresInSeconds = options . TokenExpiresInSeconds ;
80
+ _tokenExpiresInSeconds = OAuth2OptionsBase . TokenExpiresInSeconds ;
162
81
}
163
82
164
83
public async Task InitializeAsync ( )
@@ -181,7 +100,7 @@ public async Task InitializeAsync()
181
100
_testOutputHelper . WriteLine ( "{0} [INFO] connection recovery succeeded" , DateTime . Now ) ;
182
101
} ;
183
102
184
- _credentialsRefresher = new CredentialsRefresher ( _credentialsProvider ,
103
+ _credentialsRefresher = new CredentialsRefresher ( _producerCredentialsProvider ,
185
104
OnCredentialsRefreshedAsync ,
186
105
_cancellationTokenSource . Token ) ;
187
106
}
@@ -200,7 +119,7 @@ public async Task DisposeAsync()
200
119
finally
201
120
{
202
121
_doneEvent . Dispose ( ) ;
203
- _credentialsProvider . Dispose ( ) ;
122
+ _producerCredentialsProvider . Dispose ( ) ;
204
123
_connection ? . Dispose ( ) ;
205
124
}
206
125
}
@@ -227,12 +146,6 @@ private Task OnCredentialsRefreshedAsync(Credentials? credentials, Exception? ex
227
146
Assert . Fail ( "credentials arg is unexpectedly null!" ) ;
228
147
}
229
148
230
- if ( _util != null )
231
- {
232
- _util . Dispose ( ) ;
233
- }
234
- _util = new Util ( "mgt_api_client" , credentials . Password ) ;
235
-
236
149
_testOutputHelper . WriteLine ( "{0} [INFO] calling UpdateSecretAsync" , DateTime . Now ) ;
237
150
Console . WriteLine ( "{0} [INFO] calling UpdateSecretAsync" , DateTime . Now ) ;
238
151
return _connection . UpdateSecretAsync ( credentials . Password , "Token refresh" , cancellationToken ) ;
@@ -241,6 +154,7 @@ private Task OnCredentialsRefreshedAsync(Credentials? credentials, Exception? ex
241
154
[ Fact ]
242
155
public async void IntegrationTest ( )
243
156
{
157
+ Util ? closeConnectionUtil = null ;
244
158
Task ? closeConnectionTask = null ;
245
159
246
160
using ( IChannel publishChannel = await DeclarePublishChannelAsync ( ) )
@@ -260,8 +174,15 @@ public async void IntegrationTest()
260
174
261
175
if ( i == 1 )
262
176
{
263
- Assert . NotNull ( _util ) ;
264
- closeConnectionTask = _util . CloseConnectionAsync ( _connection ) ;
177
+ async Task CloseConnection ( )
178
+ {
179
+ Assert . NotNull ( _connection ) ;
180
+ Credentials httpApiCredentials = await _httpApiCredentialsProvider . GetCredentialsAsync ( ) ;
181
+ closeConnectionUtil = new Util ( "mgt_api_client" , httpApiCredentials . Password ) ;
182
+ await closeConnectionUtil . CloseConnectionAsync ( _connection . ClientProvidedName ) ;
183
+ }
184
+
185
+ closeConnectionTask = Task . Run ( CloseConnection ) ;
265
186
}
266
187
267
188
await Task . Delay ( delaySpan ) ;
@@ -272,6 +193,12 @@ public async void IntegrationTest()
272
193
closeConnectionTask = null ;
273
194
}
274
195
196
+ if ( closeConnectionUtil != null )
197
+ {
198
+ closeConnectionUtil . Dispose ( ) ;
199
+ closeConnectionUtil = null ;
200
+ }
201
+
275
202
_testOutputHelper . WriteLine ( "{0} [INFO] Resuming ..." , DateTime . Now ) ;
276
203
277
204
await PublishAsync ( publishChannel ) ;
@@ -343,7 +270,7 @@ private async Task ConsumeAsync(IChannel consumeChannel)
343
270
await consumeChannel . BasicCancelAsync ( consumerTag ) ;
344
271
}
345
272
346
- private OAuth2ClientCredentialsProvider GetCredentialsProvider ( OAuth2Options opts )
273
+ private OAuth2ClientCredentialsProvider GetCredentialsProvider ( OAuth2OptionsBase opts )
347
274
{
348
275
_testOutputHelper . WriteLine ( "OAuth2Client " ) ;
349
276
_testOutputHelper . WriteLine ( $ "- ClientId: { opts . ClientId } ") ;
0 commit comments