Skip to content

Commit 720c262

Browse files
committed
Added some documentation to OAuth2Client and IOAuth2Client
1 parent 81e3846 commit 720c262

File tree

2 files changed

+70
-24
lines changed

2 files changed

+70
-24
lines changed

projects/RabbitMQ.Client.OAuth2/IOAuth2Client.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,19 @@ namespace RabbitMQ.Client.OAuth2
3636
{
3737
public interface IOAuth2Client
3838
{
39+
/// <summary>
40+
/// Request a new AccessToken from the Token Endpoint.
41+
/// </summary>
42+
/// <param name="cancellationToken">Cancellation token for this request</param>
43+
/// <returns>Token with Access and Refresh Token</returns>
3944
Task<IToken> RequestTokenAsync(CancellationToken cancellationToken = default);
45+
46+
/// <summary>
47+
/// Request a new AccessToken using the Refresh Token from the Token Endpoint.
48+
/// </summary>
49+
/// <param name="token">Token with the Refresh Token</param>
50+
/// <param name="cancellationToken">Cancellation token for this request</param>
51+
/// <returns>Token with Access and Refresh Token</returns>
4052
Task<IToken> RefreshTokenAsync(IToken token, CancellationToken cancellationToken = default);
4153
}
4254
}

projects/RabbitMQ.Client.OAuth2/OAuth2Client.cs

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ namespace RabbitMQ.Client.OAuth2
4242
{
4343
public class OAuth2ClientBuilder
4444
{
45-
/// <summary>Discovery endpoint subpath for all OpenID Connect issuers.</summary>
45+
/// <summary>
46+
/// Discovery endpoint subpath for all OpenID Connect issuers.
47+
/// </summary>
4648
const string DISCOVERY_ENDPOINT = ".well-known/openid-configuration";
4749

4850
private readonly string _clientId;
@@ -56,30 +58,53 @@ public class OAuth2ClientBuilder
5658
private IDictionary<string, string>? _additionalRequestParameters;
5759
private HttpClientHandler? _httpClientHandler;
5860

61+
/// <summary>
62+
/// Create a new builder for creating <see cref="OAuth2Client"/>s.
63+
/// </summary>
64+
/// <param name="clientId">Id of the client</param>
65+
/// <param name="clientSecret">Secret of the client</param>
66+
/// <param name="tokenEndpoint">Endpoint to receive the Access Token</param>
67+
/// <param name="issuer">Issuer of the Access Token. Used to automaticly receive the Token Endpoint while building</param>
68+
/// <remarks>
69+
/// Either <paramref name="tokenEndpoint"/> or <paramref name="issuer"/> must be provided.
70+
/// </remarks>
5971
public OAuth2ClientBuilder(string clientId, string clientSecret, Uri? tokenEndpoint = null, Uri? issuer = null)
6072
{
6173
_clientId = clientId ?? throw new ArgumentNullException(nameof(clientId));
6274
_clientSecret = clientSecret ?? throw new ArgumentNullException(nameof(clientSecret));
6375

6476
if (tokenEndpoint is null && issuer is null)
65-
throw new ArgumentException("Either token endpoint or issuer is required");
77+
throw new ArgumentException("Either tokenEndpoint or issuer is required");
6678

6779
_tokenEndpoint = tokenEndpoint;
6880
_issuer = issuer;
6981
}
7082

83+
/// <summary>
84+
/// Set the requested scopes for the client.
85+
/// </summary>
86+
/// <param name="scope">OAuth scopes to request from the Issuer</param>
7187
public OAuth2ClientBuilder SetScope(string scope)
7288
{
7389
_scope = scope ?? throw new ArgumentNullException(nameof(scope));
7490
return this;
7591
}
7692

93+
/// <summary>
94+
/// Set custom HTTP Client handler for requests of the OAuth2 client.
95+
/// </summary>
96+
/// <param name="handler">Custom handler for HTTP requests</param>
7797
public OAuth2ClientBuilder SetHttpClientHandler(HttpClientHandler handler)
7898
{
7999
_httpClientHandler = handler ?? throw new ArgumentNullException(nameof(handler));
80100
return this;
81101
}
82102

103+
/// <summary>
104+
/// Add a additional request parameter to each HTTP request.
105+
/// </summary>
106+
/// <param name="param">Name of the parameter</param>
107+
/// <param name="paramValue">Value of the parameter</param>
83108
public OAuth2ClientBuilder AddRequestParameter(string param, string paramValue)
84109
{
85110
if (param is null)
@@ -98,8 +123,13 @@ public OAuth2ClientBuilder AddRequestParameter(string param, string paramValue)
98123
return this;
99124
}
100125

126+
/// <summary>
127+
/// Build the <see cref="OAuth2Client"/> with the provided properties of the builder.
128+
/// </summary>
129+
/// <returns>Configured OAuth2Client</returns>
101130
public IOAuth2Client Build()
102131
{
132+
// Check if Token Endpoint is missing -> Use Issuer to receive Token Endpoint
103133
if (_tokenEndpoint is null)
104134
{
105135
// Don't know how to better handle backwards compatibily
@@ -111,8 +141,14 @@ public IOAuth2Client Build()
111141
_scope, _additionalRequestParameters, _httpClientHandler);
112142
}
113143

144+
/// <summary>
145+
/// Build the <see cref="OAuth2Client"/> with the provided properties of the builder.
146+
/// </summary>
147+
/// <param name="cancellationToken">Cancellation token for this method</param>
148+
/// <returns>Configured OAuth2Client</returns>
114149
public async ValueTask<IOAuth2Client> BuildAsync(CancellationToken cancellationToken = default)
115150
{
151+
// Check if Token Endpoint is missing -> Use Issuer to receive Token Endpoint
116152
if (_tokenEndpoint is null)
117153
{
118154
Uri tokenEndpoint = await GetTokenEndpointFromIssuerAsync(cancellationToken).ConfigureAwait(false);
@@ -124,6 +160,11 @@ public async ValueTask<IOAuth2Client> BuildAsync(CancellationToken cancellationT
124160
_scope, _additionalRequestParameters, _httpClientHandler);
125161
}
126162

163+
/// <summary>
164+
/// Receive Token Endpoint from discovery page of the Issuer.
165+
/// </summary>
166+
/// <param name="cancellationToken">Cancellation token for this request</param>
167+
/// <returns>Uri of the Token Endpoint</returns>
127168
private async Task<Uri> GetTokenEndpointFromIssuerAsync(CancellationToken cancellationToken = default)
128169
{
129170
if (_issuer is null)
@@ -155,10 +196,8 @@ private async Task<Uri> GetTokenEndpointFromIssuerAsync(CancellationToken cancel
155196
{
156197
throw new InvalidOperationException("No token endpoint was found");
157198
}
158-
else
159-
{
160-
return new Uri(discovery.TokenEndpoint);
161-
}
199+
200+
return new Uri(discovery.TokenEndpoint);
162201
}
163202
}
164203

@@ -198,19 +237,15 @@ public OAuth2Client(string clientId, string clientSecret, Uri tokenEndpoint,
198237
_additionalRequestParameters = additionalRequestParameters ?? EMPTY;
199238
_tokenEndpoint = tokenEndpoint;
200239

201-
if (httpClientHandler is null)
202-
{
203-
_httpClient = new HttpClient();
204-
}
205-
else
206-
{
207-
_httpClient = new HttpClient(httpClientHandler, false);
208-
}
240+
_httpClient = httpClientHandler is null
241+
? new HttpClient()
242+
: new HttpClient(httpClientHandler, false);
209243

210244
_httpClient.DefaultRequestHeaders.Accept.Clear();
211245
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
212246
}
213247

248+
/// <inheritdoc />
214249
public async Task<IToken> RequestTokenAsync(CancellationToken cancellationToken = default)
215250
{
216251
using HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, _tokenEndpoint);
@@ -229,12 +264,11 @@ public async Task<IToken> RequestTokenAsync(CancellationToken cancellationToken
229264
// TODO specific exception?
230265
throw new InvalidOperationException("token is null");
231266
}
232-
else
233-
{
234-
return new Token(token);
235-
}
267+
268+
return new Token(token);
236269
}
237270

271+
/// <inheritdoc />
238272
public async Task<IToken> RefreshTokenAsync(IToken token,
239273
CancellationToken cancellationToken = default)
240274
{
@@ -259,10 +293,8 @@ public async Task<IToken> RefreshTokenAsync(IToken token,
259293
// TODO specific exception?
260294
throw new InvalidOperationException("refreshed token is null");
261295
}
262-
else
263-
{
264-
return new Token(refreshedToken);
265-
}
296+
297+
return new Token(refreshedToken);
266298
}
267299

268300
public void Dispose()
@@ -291,8 +323,7 @@ private Dictionary<string, string> BuildRequestParameters()
291323
private Dictionary<string, string> BuildRefreshParameters(IToken token)
292324
{
293325
Dictionary<string, string> dict = BuildRequestParameters();
294-
dict.Remove(GRANT_TYPE);
295-
dict.Add(GRANT_TYPE, REFRESH_TOKEN);
326+
dict[GRANT_TYPE] = REFRESH_TOKEN;
296327

297328
if (_scope != null)
298329
{
@@ -349,6 +380,9 @@ public long ExpiresIn
349380
}
350381
}
351382

383+
/// <summary>
384+
/// Minimal version of the properties of the discovery endpoint.
385+
/// </summary>
352386
internal class OpenIDConnectDiscovery
353387
{
354388
public OpenIDConnectDiscovery()

0 commit comments

Comments
 (0)