Skip to content

Commit 5627c23

Browse files
committed
Add vhost creation
Add example Signed-off-by: Gabriele Santomaggio <[email protected]>
1 parent 02eda45 commit 5627c23

File tree

6 files changed

+45
-15
lines changed

6 files changed

+45
-15
lines changed

RabbitMQ.AMQP.Client/IManagement.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ Task<IQueueInfo> GetQueueInfoAsync(string queueName,
6363
/// <returns>A builder for <see cref="IBindingSpecification"/></returns>
6464
IBindingSpecification Binding();
6565

66-
public Task RefreshTokenAsync(string token);
6766
}
6867

6968
internal interface IManagementTopology

RabbitMQ.AMQP.Client/Impl/AmqpConnection.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Concurrent;
77
using System.Collections.Generic;
88
using System.Linq;
9+
using System.Text;
910
using System.Threading;
1011
using System.Threading.Tasks;
1112
using Amqp;
@@ -164,9 +165,18 @@ public IEnumerable<IConsumer> Consumers
164165
/// </summary>
165166
public long Id { get; set; }
166167

168+
/// <summary>
169+
/// Refresh the OAuth token and update the connection settings.
170+
/// </summary>
171+
/// <param name="token">OAuth token</param>
167172
public async Task RefreshTokenAsync(string token)
168173
{
169-
await _management.RefreshTokenAsync(token)
174+
// here we use the primitive RequestAsync method because we don't want to
175+
// expose this method in the IManagement interface
176+
// we need to update the connection settings with the new token
177+
int[] expectedResponseCodes = { AmqpManagement.Code204 };
178+
_ = await _management.RequestAsync(Encoding.ASCII.GetBytes(token),
179+
AmqpManagement.AuthTokens, AmqpManagement.Put, expectedResponseCodes)
170180
.ConfigureAwait(false);
171181
_connectionSettings.UpdateOAuthPassword(token);
172182
}

RabbitMQ.AMQP.Client/Impl/AmqpManagement.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class AmqpManagement : AbstractLifeCycle, IManagement, IManagementTopolog
4747
internal const string Post = "POST";
4848
internal const string Delete = "DELETE";
4949
private const string ReplyTo = "$me";
50-
private const string AuthTokens = "/auth/tokens";
50+
internal const string AuthTokens = "/auth/tokens";
5151

5252
protected readonly TaskCompletionSource<bool> _managementSessionClosedTcs =
5353
Utils.CreateTaskCompletionSource<bool>();
@@ -136,14 +136,6 @@ public IBindingSpecification Binding()
136136
return new AmqpBindingSpecification(this);
137137
}
138138

139-
public async Task RefreshTokenAsync(string token)
140-
{
141-
int[] expectedResponseCodes = { Code204 };
142-
_ = await RequestAsync(Encoding.ASCII.GetBytes(token),
143-
AuthTokens, Put, expectedResponseCodes)
144-
.ConfigureAwait(false);
145-
}
146-
147139
/// <summary>
148140
/// Open the management session.
149141
/// </summary>

RabbitMQ.AMQP.Client/PublicAPI.Unshipped.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ RabbitMQ.AMQP.Client.IManagement.GetQueueInfoAsync(RabbitMQ.AMQP.Client.IQueueSp
241241
RabbitMQ.AMQP.Client.IManagement.GetQueueInfoAsync(string! queueName, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<RabbitMQ.AMQP.Client.IQueueInfo!>!
242242
RabbitMQ.AMQP.Client.IManagement.Queue() -> RabbitMQ.AMQP.Client.IQueueSpecification!
243243
RabbitMQ.AMQP.Client.IManagement.Queue(string! name) -> RabbitMQ.AMQP.Client.IQueueSpecification!
244-
RabbitMQ.AMQP.Client.IManagement.RefreshTokenAsync(string! token) -> System.Threading.Tasks.Task!
245244
RabbitMQ.AMQP.Client.IMessage
246245
RabbitMQ.AMQP.Client.IMessage.AbsoluteExpiryTime() -> System.DateTime
247246
RabbitMQ.AMQP.Client.IMessage.AbsoluteExpiryTime(System.DateTime absoluteExpiryTime) -> RabbitMQ.AMQP.Client.IMessage!
@@ -389,7 +388,6 @@ RabbitMQ.AMQP.Client.Impl.AmqpManagement.GetQueueInfoAsync(RabbitMQ.AMQP.Client.
389388
RabbitMQ.AMQP.Client.Impl.AmqpManagement.GetQueueInfoAsync(string! queueName, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<RabbitMQ.AMQP.Client.IQueueInfo!>!
390389
RabbitMQ.AMQP.Client.Impl.AmqpManagement.Queue() -> RabbitMQ.AMQP.Client.IQueueSpecification!
391390
RabbitMQ.AMQP.Client.Impl.AmqpManagement.Queue(string! name) -> RabbitMQ.AMQP.Client.IQueueSpecification!
392-
RabbitMQ.AMQP.Client.Impl.AmqpManagement.RefreshTokenAsync(string! token) -> System.Threading.Tasks.Task!
393391
RabbitMQ.AMQP.Client.Impl.AmqpMessage
394392
RabbitMQ.AMQP.Client.Impl.AmqpMessage.AbsoluteExpiryTime() -> System.DateTime
395393
RabbitMQ.AMQP.Client.Impl.AmqpMessage.AbsoluteExpiryTime(System.DateTime absoluteExpiryTime) -> RabbitMQ.AMQP.Client.IMessage!

docs/Examples/OAuth2/Program.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,30 @@
1717
ConnectionSettingsBuilder.Create()
1818
.Host("localhost")
1919
.Port(5672)
20-
.OAuth2Options(new OAuth2Options(Token.GenerateToken(DateTime.UtcNow.AddMilliseconds(1500))))
20+
.OAuth2Options(new OAuth2Options(Token.GenerateToken(DateTime.UtcNow.AddSeconds(5))))
2121
.Build()).ConfigureAwait(false);
2222

2323
Trace.WriteLine(TraceLevel.Information, $"Connected to the broker {connection} successfully");
2424
Trace.WriteLine(TraceLevel.Information, $"Connection status {connection.State}");
2525

26-
Thread.Sleep(TimeSpan.FromSeconds(15));
26+
CancellationTokenSource cts = new();
27+
28+
_ = Task.Run(() =>
29+
{
30+
while (!cts.IsCancellationRequested)
31+
{
32+
string token = Token.GenerateToken(DateTime.UtcNow.AddSeconds(5));
33+
Trace.WriteLine(TraceLevel.Information, $"Token Refresh..{token}");
34+
connection.RefreshTokenAsync(token).Wait();
35+
Task.Delay(TimeSpan.FromSeconds(4), cts.Token).Wait();
36+
}
37+
});
38+
2739

2840
Console.WriteLine("Connection state: " + connection.State);
41+
42+
// press any key to exit
43+
Console.ReadKey();
44+
cts.Cancel();
45+
46+
await connection.CloseAsync().ConfigureAwait(false);

docs/Examples/OAuth2/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### OAuth2 Example
2+
3+
This example demonstrates how to use the OAuth2 authentication mechanism.
4+
5+
It is meant to be used with the RabbitMQ server configured for the CI.
6+
7+
To run the example:
8+
- make rabbitmq-server-start
9+
- dotnet run
10+
11+
12+
The example will create a connection given the token and refresh the token every 4 seconds.
13+

0 commit comments

Comments
 (0)