Skip to content

Commit 6fd530b

Browse files
committed
* Restore OAuth2Client tests and other OAuth2 tests
1 parent 94173b7 commit 6fd530b

14 files changed

+484
-284
lines changed

projects/RabbitMQ.Client.OAuth2/CredentialsRefresher.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,6 @@
3333
using System.Threading;
3434
using System.Threading.Tasks;
3535

36-
// TODO rabbitmq-dotnet-client-1639
37-
/*
38-
private async Task NotifyCredentialRefreshedAsync(bool succesfully)
39-
{
40-
if (succesfully)
41-
{
42-
using var cts = new CancellationTokenSource(InternalConstants.DefaultConnectionCloseTimeout);
43-
await UpdateSecretAsync(_config.CredentialsProvider.Password, "Token refresh", cts.Token)
44-
.ConfigureAwait(false);
45-
}
46-
}
47-
*/
48-
4936
namespace RabbitMQ.Client.OAuth2
5037
{
5138
public delegate Task NotifyCredentialsRefreshedAsync(Credentials? credentials,
@@ -86,7 +73,7 @@ public CredentialsRefresher(ICredentialsProvider credentialsProvider,
8673

8774
_refreshTask = Task.Run(RefreshLoopAsync, _linkedCts.Token);
8875

89-
TimerBasedCredentialRefresherEventSource.Log.Registered(_credentialsProvider.Name);
76+
CredentialsRefresherEventSource.Log.Registered(_credentialsProvider.Name);
9077
}
9178

9279
public Credentials? Credentials => _credentials;

projects/RabbitMQ.Client.OAuth2/TimerBasedCredentialRefresherEventSource.cs renamed to projects/RabbitMQ.Client.OAuth2/CredentialsRefresherEventSource.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434

3535
namespace RabbitMQ.Client.OAuth2
3636
{
37-
[EventSource(Name = "TimerBasedCredentialRefresher")]
38-
public class TimerBasedCredentialRefresherEventSource : EventSource
37+
[EventSource(Name = "CredentialRefresher")]
38+
public class CredentialsRefresherEventSource : EventSource
3939
{
40-
public static TimerBasedCredentialRefresherEventSource Log { get; } = new TimerBasedCredentialRefresherEventSource();
40+
public static CredentialsRefresherEventSource Log { get; } = new CredentialsRefresherEventSource();
4141

4242
[Event(1)]
4343
public void Registered(string name) => WriteEvent(1, "Registered", name);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// This source code is dual-licensed under the Apache License, version
2+
// 2.0, and the Mozilla Public License, version 2.0.
3+
//
4+
// The APL v2.0:
5+
//
6+
//---------------------------------------------------------------------------
7+
// Copyright (c) 2007-2024 Broadcom. All Rights Reserved.
8+
//
9+
// Licensed under the Apache License, Version 2.0 (the "License");
10+
// you may not use this file except in compliance with the License.
11+
// You may obtain a copy of the License at
12+
//
13+
// https://www.apache.org/licenses/LICENSE-2.0
14+
//
15+
// Unless required by applicable law or agreed to in writing, software
16+
// distributed under the License is distributed on an "AS IS" BASIS,
17+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
// See the License for the specific language governing permissions and
19+
// limitations under the License.
20+
//---------------------------------------------------------------------------
21+
//
22+
// The MPL v2.0:
23+
//
24+
//---------------------------------------------------------------------------
25+
// This Source Code Form is subject to the terms of the Mozilla Public
26+
// License, v. 2.0. If a copy of the MPL was not distributed with this
27+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
28+
//
29+
// Copyright (c) 2007-2024 Broadcom. All Rights Reserved.
30+
//---------------------------------------------------------------------------
31+
32+
using System.Threading;
33+
using System.Threading.Tasks;
34+
35+
namespace RabbitMQ.Client.OAuth2
36+
{
37+
public interface IOAuth2Client
38+
{
39+
Task<IToken> RequestTokenAsync(CancellationToken cancellationToken = default);
40+
Task<IToken> RefreshTokenAsync(IToken token, CancellationToken cancellationToken = default);
41+
}
42+
}

projects/RabbitMQ.Client.OAuth2/OAuth2Client.cs

Lines changed: 14 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -39,65 +39,6 @@
3939

4040
namespace RabbitMQ.Client.OAuth2
4141
{
42-
public interface IOAuth2Client
43-
{
44-
Task<IToken> RequestTokenAsync(CancellationToken cancellationToken = default);
45-
Task<IToken> RefreshTokenAsync(IToken token, CancellationToken cancellationToken = default);
46-
}
47-
48-
public interface IToken
49-
{
50-
string AccessToken { get; }
51-
string RefreshToken { get; }
52-
TimeSpan ExpiresIn { get; }
53-
bool HasExpired { get; }
54-
}
55-
56-
public class Token : IToken
57-
{
58-
private readonly JsonToken _source;
59-
private readonly DateTime _lastTokenRenewal;
60-
61-
internal Token(JsonToken json)
62-
{
63-
this._source = json;
64-
this._lastTokenRenewal = DateTime.Now;
65-
}
66-
67-
public string AccessToken
68-
{
69-
get
70-
{
71-
return _source.access_token;
72-
}
73-
}
74-
75-
public string RefreshToken
76-
{
77-
get
78-
{
79-
return _source.refresh_token;
80-
}
81-
}
82-
83-
public TimeSpan ExpiresIn
84-
{
85-
get
86-
{
87-
return TimeSpan.FromSeconds(_source.expires_in);
88-
}
89-
}
90-
91-
bool IToken.HasExpired
92-
{
93-
get
94-
{
95-
TimeSpan age = DateTime.Now - _lastTokenRenewal;
96-
return age > ExpiresIn;
97-
}
98-
}
99-
}
100-
10142
public class OAuth2ClientBuilder
10243
{
10344
private readonly string _clientId;
@@ -112,7 +53,6 @@ public OAuth2ClientBuilder(string clientId, string clientSecret, Uri tokenEndpoi
11253
_clientId = clientId ?? throw new ArgumentNullException(nameof(clientId));
11354
_clientSecret = clientSecret ?? throw new ArgumentNullException(nameof(clientSecret));
11455
_tokenEndpoint = tokenEndpoint ?? throw new ArgumentNullException(nameof(tokenEndpoint));
115-
11656
}
11757

11858
public OAuth2ClientBuilder SetScope(string scope)
@@ -173,7 +113,7 @@ internal class OAuth2Client : IOAuth2Client, IDisposable
173113
private readonly string _clientId;
174114
private readonly string _clientSecret;
175115
private readonly Uri _tokenEndpoint;
176-
private readonly string _scope;
116+
private readonly string? _scope;
177117
private readonly IDictionary<string, string> _additionalRequestParameters;
178118

179119
public static readonly IDictionary<string, string> EMPTY = new Dictionary<string, string>();
@@ -185,11 +125,11 @@ public OAuth2Client(string clientId, string clientSecret, Uri tokenEndpoint,
185125
IDictionary<string, string>? additionalRequestParameters,
186126
HttpClientHandler? httpClientHandler)
187127
{
188-
this._clientId = clientId;
189-
this._clientSecret = clientSecret;
190-
this._scope = scope ?? string.Empty;
191-
this._additionalRequestParameters = additionalRequestParameters == null ? EMPTY : additionalRequestParameters;
192-
this._tokenEndpoint = tokenEndpoint;
128+
_clientId = clientId;
129+
_clientSecret = clientSecret;
130+
_scope = scope;
131+
_additionalRequestParameters = additionalRequestParameters == null ? EMPTY : additionalRequestParameters;
132+
_tokenEndpoint = tokenEndpoint;
193133

194134
_httpClient = httpClientHandler == null ? new HttpClient() :
195135
new HttpClient(httpClientHandler);
@@ -200,7 +140,7 @@ public OAuth2Client(string clientId, string clientSecret, Uri tokenEndpoint,
200140
public async Task<IToken> RequestTokenAsync(CancellationToken cancellationToken = default)
201141
{
202142
using HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, _tokenEndpoint);
203-
req.Content = new FormUrlEncodedContent(buildRequestParameters());
143+
req.Content = new FormUrlEncodedContent(BuildRequestParameters());
204144

205145
using HttpResponseMessage response = await _httpClient.SendAsync(req)
206146
.ConfigureAwait(false);
@@ -231,7 +171,7 @@ public async Task<IToken> RefreshTokenAsync(IToken token,
231171

232172
using HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, _tokenEndpoint)
233173
{
234-
Content = new FormUrlEncodedContent(buildRefreshParameters(token))
174+
Content = new FormUrlEncodedContent(BuildRefreshParameters(token))
235175
};
236176

237177
using HttpResponseMessage response = await _httpClient.SendAsync(req)
@@ -258,7 +198,7 @@ public void Dispose()
258198
_httpClient.Dispose();
259199
}
260200

261-
private Dictionary<string, string> buildRequestParameters()
201+
private Dictionary<string, string> BuildRequestParameters()
262202
{
263203
var dict = new Dictionary<string, string>(_additionalRequestParameters)
264204
{
@@ -276,16 +216,19 @@ private Dictionary<string, string> buildRequestParameters()
276216
return dict;
277217
}
278218

279-
private Dictionary<string, string> buildRefreshParameters(IToken token)
219+
private Dictionary<string, string> BuildRefreshParameters(IToken token)
280220
{
281-
var dict = buildRequestParameters();
221+
var dict = BuildRequestParameters();
282222
dict.Remove(GRANT_TYPE);
283223
dict.Add(GRANT_TYPE, REFRESH_TOKEN);
224+
284225
if (_scope != null)
285226
{
286227
dict.Add(SCOPE, _scope);
287228
}
229+
288230
dict.Add(REFRESH_TOKEN, token.RefreshToken);
231+
289232
return dict;
290233
}
291234
}

projects/RabbitMQ.Client.OAuth2/OAuth2CredentialsProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ await _semaphore.WaitAsync(cancellationToken)
9797
.ConfigureAwait(false);
9898
try
9999
{
100-
if (_token == null || _token.RefreshToken == null)
100+
if (_token == null || string.IsNullOrEmpty(_token.RefreshToken))
101101
{
102102
_token = await _oAuth2Client.RequestTokenAsync(cancellationToken)
103103
.ConfigureAwait(false);

projects/RabbitMQ.Client.OAuth2/PublicAPI.Unshipped.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ RabbitMQ.Client.OAuth2.CredentialsRefresher
22
RabbitMQ.Client.OAuth2.CredentialsRefresher.Credentials.get -> RabbitMQ.Client.Credentials?
33
RabbitMQ.Client.OAuth2.CredentialsRefresher.CredentialsRefresher(RabbitMQ.Client.ICredentialsProvider! credentialsProvider, RabbitMQ.Client.OAuth2.NotifyCredentialsRefreshedAsync! onRefreshed, System.Threading.CancellationToken cancellationToken) -> void
44
RabbitMQ.Client.OAuth2.CredentialsRefresher.Dispose() -> void
5+
RabbitMQ.Client.OAuth2.CredentialsRefresherEventSource
6+
RabbitMQ.Client.OAuth2.CredentialsRefresherEventSource.AlreadyRegistered(string! name) -> void
7+
RabbitMQ.Client.OAuth2.CredentialsRefresherEventSource.CredentialsRefresherEventSource() -> void
8+
RabbitMQ.Client.OAuth2.CredentialsRefresherEventSource.RefreshedCredentials(string! name, bool succesfully) -> void
9+
RabbitMQ.Client.OAuth2.CredentialsRefresherEventSource.Registered(string! name) -> void
10+
RabbitMQ.Client.OAuth2.CredentialsRefresherEventSource.ScheduledTimer(string! name, double interval) -> void
11+
RabbitMQ.Client.OAuth2.CredentialsRefresherEventSource.TriggeredTimer(string! name) -> void
12+
RabbitMQ.Client.OAuth2.CredentialsRefresherEventSource.Unregistered(string! name) -> void
513
RabbitMQ.Client.OAuth2.IOAuth2Client.RefreshTokenAsync(RabbitMQ.Client.OAuth2.IToken! token, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<RabbitMQ.Client.OAuth2.IToken!>!
614
RabbitMQ.Client.OAuth2.IOAuth2Client.RequestTokenAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<RabbitMQ.Client.OAuth2.IToken!>!
715
RabbitMQ.Client.OAuth2.NotifyCredentialsRefreshedAsync
816
RabbitMQ.Client.OAuth2.OAuth2ClientCredentialsProvider.Dispose() -> void
917
RabbitMQ.Client.OAuth2.OAuth2ClientCredentialsProvider.GetCredentialsAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<RabbitMQ.Client.Credentials!>!
10-
RabbitMQ.Client.OAuth2.TimerBasedCredentialRefresherEventSource
11-
RabbitMQ.Client.OAuth2.TimerBasedCredentialRefresherEventSource.TimerBasedCredentialRefresherEventSource() -> void
18+
static RabbitMQ.Client.OAuth2.CredentialsRefresherEventSource.Log.get -> RabbitMQ.Client.OAuth2.CredentialsRefresherEventSource!
1219
virtual RabbitMQ.Client.OAuth2.CredentialsRefresher.Dispose(bool disposing) -> void
13-
~RabbitMQ.Client.OAuth2.TimerBasedCredentialRefresherEventSource.AlreadyRegistered(string name) -> void
14-
~RabbitMQ.Client.OAuth2.TimerBasedCredentialRefresherEventSource.RefreshedCredentials(string name, bool succesfully) -> void
15-
~RabbitMQ.Client.OAuth2.TimerBasedCredentialRefresherEventSource.Registered(string name) -> void
16-
~RabbitMQ.Client.OAuth2.TimerBasedCredentialRefresherEventSource.ScheduledTimer(string name, double interval) -> void
17-
~RabbitMQ.Client.OAuth2.TimerBasedCredentialRefresherEventSource.TriggeredTimer(string name) -> void
18-
~RabbitMQ.Client.OAuth2.TimerBasedCredentialRefresherEventSource.Unregistered(string name) -> void
19-
~static RabbitMQ.Client.OAuth2.TimerBasedCredentialRefresherEventSource.Log.get -> RabbitMQ.Client.OAuth2.TimerBasedCredentialRefresherEventSource
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// This source code is dual-licensed under the Apache License, version
2+
// 2.0, and the Mozilla Public License, version 2.0.
3+
//
4+
// The APL v2.0:
5+
//
6+
//---------------------------------------------------------------------------
7+
// Copyright (c) 2007-2024 Broadcom. All Rights Reserved.
8+
//
9+
// Licensed under the Apache License, Version 2.0 (the "License");
10+
// you may not use this file except in compliance with the License.
11+
// You may obtain a copy of the License at
12+
//
13+
// https://www.apache.org/licenses/LICENSE-2.0
14+
//
15+
// Unless required by applicable law or agreed to in writing, software
16+
// distributed under the License is distributed on an "AS IS" BASIS,
17+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
// See the License for the specific language governing permissions and
19+
// limitations under the License.
20+
//---------------------------------------------------------------------------
21+
//
22+
// The MPL v2.0:
23+
//
24+
//---------------------------------------------------------------------------
25+
// This Source Code Form is subject to the terms of the Mozilla Public
26+
// License, v. 2.0. If a copy of the MPL was not distributed with this
27+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
28+
//
29+
// Copyright (c) 2007-2024 Broadcom. All Rights Reserved.
30+
//---------------------------------------------------------------------------
31+
32+
using System;
33+
34+
namespace RabbitMQ.Client.OAuth2
35+
{
36+
public interface IToken
37+
{
38+
string AccessToken { get; }
39+
string RefreshToken { get; }
40+
TimeSpan ExpiresIn { get; }
41+
bool HasExpired { get; }
42+
}
43+
44+
public class Token : IToken
45+
{
46+
private readonly JsonToken _source;
47+
private readonly DateTime _lastTokenRenewal;
48+
49+
internal Token(JsonToken json)
50+
{
51+
_source = json;
52+
_lastTokenRenewal = DateTime.Now;
53+
}
54+
55+
public string AccessToken
56+
{
57+
get
58+
{
59+
return _source.access_token;
60+
}
61+
}
62+
63+
public string RefreshToken
64+
{
65+
get
66+
{
67+
return _source.refresh_token;
68+
}
69+
}
70+
71+
public TimeSpan ExpiresIn
72+
{
73+
get
74+
{
75+
return TimeSpan.FromSeconds(_source.expires_in);
76+
}
77+
}
78+
79+
bool IToken.HasExpired
80+
{
81+
get
82+
{
83+
TimeSpan age = DateTime.Now - _lastTokenRenewal;
84+
return age > ExpiresIn;
85+
}
86+
}
87+
}
88+
}

projects/RabbitMQ.Client/client/api/BasicCredentialsProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class BasicCredentialsProvider : ICredentialsProvider
4444

4545
public BasicCredentialsProvider(string? name, string userName, string password)
4646
{
47-
_name = name ?? typeof(BasicCredentialsProvider).Name;
47+
_name = name ?? nameof(BasicCredentialsProvider);
4848
_userName = userName ?? throw new ArgumentNullException(nameof(userName));
4949
_password = password ?? throw new ArgumentNullException(nameof(password));
5050
_credentials = new Credentials(_name, _userName, _password, null);

0 commit comments

Comments
 (0)