diff --git a/SharpPulsar/Auth/OAuth2/protocol/ClientCredentialsExchanger.cs b/SharpPulsar/Auth/OAuth2/Protocol/IClientCredentialsExchanger.cs similarity index 95% rename from SharpPulsar/Auth/OAuth2/protocol/ClientCredentialsExchanger.cs rename to SharpPulsar/Auth/OAuth2/Protocol/IClientCredentialsExchanger.cs index f5103ade8..c1f6170d5 100644 --- a/SharpPulsar/Auth/OAuth2/protocol/ClientCredentialsExchanger.cs +++ b/SharpPulsar/Auth/OAuth2/Protocol/IClientCredentialsExchanger.cs @@ -22,7 +22,7 @@ namespace SharpPulsar.Auth.OAuth2.Protocol /// /// An interface for exchanging client credentials for an access token. /// - public interface ClientCredentialsExchanger : AutoCloseable + public interface IClientCredentialsExchanger// : AutoCloseable { /// /// Requests an exchange of client credentials for an access token. diff --git a/SharpPulsar/Auth/OAuth2/protocol/ClientCredentialsExchangeRequest.cs b/SharpPulsar/Auth/OAuth2/protocol/ClientCredentialsExchangeRequest.cs index 16b619976..e50004ca9 100644 --- a/SharpPulsar/Auth/OAuth2/protocol/ClientCredentialsExchangeRequest.cs +++ b/SharpPulsar/Auth/OAuth2/protocol/ClientCredentialsExchangeRequest.cs @@ -1,4 +1,5 @@ -/// +using System.Text.Json.Serialization; +/// /// Licensed to the Apache Software Foundation (ASF) under one /// or more contributor license agreements. See the NOTICE file /// distributed with this work for additional information @@ -24,17 +25,17 @@ namespace SharpPulsar.Auth.OAuth2.Protocol /// OAuth 2.0 RFC 6749, section 4.4"/> public class ClientCredentialsExchangeRequest { -// @JsonProperty("client_id") private String clientId; - private string clientId; + [JsonPropertyName("client_id")] + public string ClientId { get; set; } -// JsonProperty("client_secret") private String clientSecret; - private string clientSecret; + [JsonPropertyName("client_secret")] + public string ClientSecret { get; set; } -// JsonProperty("audience") private String audience; - private string audience; + [JsonPropertyName("audience")] + public string Audience { get; set; } -// JsonProperty("scope") private String scope; - private string scope; - } + [JsonPropertyName("scope")] + public string Scope { get; set; } + } } \ No newline at end of file diff --git a/SharpPulsar/Auth/OAuth2/protocol/DefaultMetadataResolver.cs b/SharpPulsar/Auth/OAuth2/protocol/DefaultMetadataResolver.cs index f79ffcb15..391d3a883 100644 --- a/SharpPulsar/Auth/OAuth2/protocol/DefaultMetadataResolver.cs +++ b/SharpPulsar/Auth/OAuth2/protocol/DefaultMetadataResolver.cs @@ -1,4 +1,6 @@ -using System.IO; +using System; +using System.IO; +using System.Security.Policy; /// /// Licensed to the Apache Software Foundation (ASF) under one @@ -32,29 +34,29 @@ public class DefaultMetadataResolver : MetadataResolver protected internal const int DefaultConnectTimeoutInSeconds = 10; protected internal const int DefaultReadTimeoutInSeconds = 30; - private readonly URL metadataUrl; + private readonly Uri _metadataUrl; private readonly ObjectReader objectReader; - private Duration connectTimeout; - private Duration readTimeout; + private TimeSpan _connectTimeout; + private TimeSpan _readTimeout; - public DefaultMetadataResolver(URL MetadataUrl) + public DefaultMetadataResolver(Uri metadataUrl) { - this.metadataUrl = MetadataUrl; + _metadataUrl = metadataUrl; this.objectReader = (new ObjectMapper()).readerFor(typeof(Metadata)); - // set a default timeout to ensure that this doesn't block - this.connectTimeout = Duration.ofSeconds(DefaultConnectTimeoutInSeconds); - this.readTimeout = Duration.ofSeconds(DefaultReadTimeoutInSeconds); + // set a default timeout to ensure that this doesn't block + _connectTimeout = TimeSpan.FromSeconds(DefaultConnectTimeoutInSeconds); + _readTimeout = TimeSpan.FromSeconds(DefaultReadTimeoutInSeconds); } - public virtual DefaultMetadataResolver WithConnectTimeout(Duration ConnectTimeout) + public virtual DefaultMetadataResolver WithConnectTimeout(TimeSpan connectTimeout) { - this.connectTimeout = ConnectTimeout; + _connectTimeout = connectTimeout; return this; } - public virtual DefaultMetadataResolver WithReadTimeout(Duration ReadTimeout) + public virtual DefaultMetadataResolver WithReadTimeout(TimeSpan readTimeout) { - this.readTimeout = ReadTimeout; + _readTimeout = readTimeout; return this; } @@ -67,13 +69,13 @@ public virtual Metadata Resolve() try { URLConnection C = this.metadataUrl.openConnection(); - if (connectTimeout != null) + if (_connectTimeout != null) { - C.setConnectTimeout((int) connectTimeout.toMillis()); + C.setConnectTimeout((int) _connectTimeout.toMillis()); } - if (readTimeout != null) + if (_readTimeout != null) { - C.setReadTimeout((int) readTimeout.toMillis()); + C.setReadTimeout((int) _readTimeout.toMillis()); } C.setRequestProperty("Accept", "application/json"); @@ -87,7 +89,7 @@ public virtual Metadata Resolve() } catch (IOException E) { - throw new IOException("Cannot obtain authorization metadata from " + metadataUrl.ToString(), E); + throw new IOException("Cannot obtain authorization metadata from " + _metadataUrl.ToString(), E); } } diff --git a/SharpPulsar/Auth/OAuth2/protocol/Metadata.cs b/SharpPulsar/Auth/OAuth2/protocol/Metadata.cs index 2ad27d803..328113f51 100644 --- a/SharpPulsar/Auth/OAuth2/protocol/Metadata.cs +++ b/SharpPulsar/Auth/OAuth2/protocol/Metadata.cs @@ -1,4 +1,5 @@ using System.Security.Policy; +using System.Text.Json.Serialization; /// /// Licensed to the Apache Software Foundation (ASF) under one /// or more contributor license agreements. See the NOTICE file @@ -25,20 +26,27 @@ namespace SharpPulsar.Auth.OAuth2.Protocol /// public class Metadata { - //JsonProperty("issuer") private java.net.URL authorizationEndpoint - public Url issuer; - //JsonProperty("authorization_endpoint") private java.net.URL authorizationEndpoint - public Url authorizationEndpoint; - //JsonProperty("token_endpoint") private java.net.URL tokenEndpoint - public Url tokenEndpoint; - //JsonProperty("userinfo_endpoint") private java.net.URL userInfoEndpoint; - public Url userInfoEndpoint; - //JsonProperty("revocation_endpoint") private java.net.URL revocationEndpoint; - public Url revocationEndpoint; - //JsonProperty("jwks_uri") private java.net.URL jwksUri - public Url jwksUri; - //JsonProperty("device_authorization_endpoint") private java.net.URL deviceAuthorizationEndpoint; - public Url deviceAuthorizationEndpoint; - } + + [JsonPropertyName("issuer")] + public Url Issuer { get; set; } + + [JsonPropertyName("authorization_endpoint")] + public Url AuthorizationEndpoint { get; set; } + + [JsonPropertyName("token_endpoint")] + public Url TokenEndpoint { get; set; } + + [JsonPropertyName("userinfo_endpoint")] + public Url UserInfoEndpoint { get; set; } + + [JsonPropertyName("revocation_endpoint")] + public Url RevocationEndpoint { get; set; } + + [JsonPropertyName("jwks_uri")] + public Url JwksUri { get; set; } + + [JsonPropertyName("device_authorization_endpoint")] + public Url DeviceAuthorizationEndpoint { get; set; } + } } \ No newline at end of file diff --git a/SharpPulsar/Auth/OAuth2/protocol/TokenClient.cs b/SharpPulsar/Auth/OAuth2/protocol/TokenClient.cs index ed50bfabf..f91beafce 100644 --- a/SharpPulsar/Auth/OAuth2/protocol/TokenClient.cs +++ b/SharpPulsar/Auth/OAuth2/protocol/TokenClient.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Net.Http; /// /// Licensed to the Apache Software Foundation (ASF) under one @@ -25,22 +26,22 @@ namespace SharpPulsar.Auth.OAuth2.Protocol /// /// A client for an OAuth 2.0 token endpoint. /// - public class TokenClient : ClientCredentialsExchanger + public class TokenClient : IClientCredentialsExchanger { protected internal const int DefaultConnectTimeoutInSeconds = 10; protected internal const int DefaultReadTimeoutInSeconds = 30; - private readonly URL tokenUrl; - private readonly AsyncHttpClient httpClient; + private readonly Uri tokenUrl; + private readonly HttpClient httpClient; - public TokenClient(URL TokenUrl) : this(TokenUrl, null) + public TokenClient(Uri tokenUrl) : this(tokenUrl, null) { } - internal TokenClient(URL TokenUrl, AsyncHttpClient HttpClient) + internal TokenClient(Uri tokenUrl, HttpClient httpClient) { - if (HttpClient == null) + if (httpClient == null) { DefaultAsyncHttpClientConfig.Builder ConfBuilder = new DefaultAsyncHttpClientConfig.Builder(); ConfBuilder.setFollowRedirect(true); @@ -52,14 +53,14 @@ internal TokenClient(URL TokenUrl, AsyncHttpClient HttpClient) } else { - this.httpClient = HttpClient; + this.httpClient = httpClient; } - this.tokenUrl = TokenUrl; + this.tokenUrl = tokenUrl; } public override void close() { - httpClient.close(); + httpClient.Dispose(); } /// diff --git a/SharpPulsar/Auth/OAuth2/protocol/TokenError.cs b/SharpPulsar/Auth/OAuth2/protocol/TokenError.cs index ebad5a042..f0cf59330 100644 --- a/SharpPulsar/Auth/OAuth2/protocol/TokenError.cs +++ b/SharpPulsar/Auth/OAuth2/protocol/TokenError.cs @@ -18,18 +18,24 @@ /// namespace SharpPulsar.Auth.OAuth2.Protocol { - /// - /// Represents an error returned from an OAuth 2.0 token endpoint. - /// + + using System.Text.Json.Serialization; + + /// + /// Represents an error returned from an OAuth 2.0 token endpoint. + /// /// - public class TokenError + public class TokenError { - //JsonProperty("error") private String error; - public string Error; - //JsonProperty("error_description") private String errorDescription; - public string ErrorDescription; - //JsonProperty("error_uri") private String errorUri; - public string ErrorUri; - } + [JsonPropertyName("error")] + public string Error { get; set; } + + [JsonPropertyName("error_description")] + public string ErrorDescription { get; set; } + + + [JsonPropertyName("error_uri")] + public string ErrorUri { get; set; } + } } \ No newline at end of file diff --git a/SharpPulsar/Auth/OAuth2/protocol/TokenExchangeException.cs b/SharpPulsar/Auth/OAuth2/protocol/TokenExchangeException.cs index f412f91a3..bd2694ae0 100644 --- a/SharpPulsar/Auth/OAuth2/protocol/TokenExchangeException.cs +++ b/SharpPulsar/Auth/OAuth2/protocol/TokenExchangeException.cs @@ -27,7 +27,7 @@ public class TokenExchangeException : Exception { private TokenError error; - public TokenExchangeException(TokenError Error) : base(string.Format("{0} ({1})", Error.getErrorDescription(), Error.getError())) + public TokenExchangeException(TokenError error) : base(string.Format("{0} ({1})", error.ErrorDescription, error.Error)) { this.error = Error; } diff --git a/SharpPulsar/Auth/OAuth2/protocol/TokenResult.cs b/SharpPulsar/Auth/OAuth2/protocol/TokenResult.cs index 4eece9bdf..1aaad6bcd 100644 --- a/SharpPulsar/Auth/OAuth2/protocol/TokenResult.cs +++ b/SharpPulsar/Auth/OAuth2/protocol/TokenResult.cs @@ -1,4 +1,5 @@ using System; +using System.Text.Json.Serialization; /// /// Licensed to the Apache Software Foundation (ASF) under one @@ -28,14 +29,18 @@ namespace SharpPulsar.Auth.OAuth2.Protocol public class TokenResult { private const long SerialVersionUID = 1L; - //JsonProperty("access_token") private String accessToken; - public string AccessToken; - //JsonProperty("id_token") private String idToken; - public string IdToken; - //JsonProperty("refresh_token") private String refreshToken; - public string RefreshToken; - //JsonProperty("expires_in") private int expiresIn; - public int ExpiresIn; - } + + [JsonPropertyName("access_token")] + public string AccessToken { get; set; } + + [JsonPropertyName("id_token")] + public string IdToken { get; set; } + + [JsonPropertyName("refresh_token")] + public string RefreshToken { get; set; } + + [JsonPropertyName("expires_in")] + public int ExpiresIn { get; set; } + } } \ No newline at end of file diff --git a/SharpPulsar/SocketImpl/SocketClient.cs b/SharpPulsar/SocketImpl/SocketClient.cs index f38b2dcdd..d234ee7a7 100644 --- a/SharpPulsar/SocketImpl/SocketClient.cs +++ b/SharpPulsar/SocketImpl/SocketClient.cs @@ -302,6 +302,7 @@ private async Task ConnectAsync(IPAddress[] serverAddresses, int port) try { await _socket.ConnectAsync(address, port).ConfigureAwait(false); + return; } catch (Exception exc) {