Skip to content

Commit

Permalink
Url
Browse files Browse the repository at this point in the history
  • Loading branch information
eaba committed Jul 11, 2022
1 parent 1796eab commit cfbb63d
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace SharpPulsar.Auth.OAuth2.Protocol
/// <summary>
/// An interface for exchanging client credentials for an access token.
/// </summary>
public interface ClientCredentialsExchanger : AutoCloseable
public interface IClientCredentialsExchanger// : AutoCloseable
{
/// <summary>
/// Requests an exchange of client credentials for an access token. </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <summary>
using System.Text.Json.Serialization;
/// <summary>
/// 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
Expand All @@ -24,17 +25,17 @@ namespace SharpPulsar.Auth.OAuth2.Protocol
/// <seealso cref="<a href="https://tools.ietf.org/html/rfc6749.section-4.4">OAuth 2.0 RFC 6749, section 4.4</a>"/>
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; }
}

}
38 changes: 20 additions & 18 deletions SharpPulsar/Auth/OAuth2/protocol/DefaultMetadataResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.IO;
using System;
using System.IO;
using System.Security.Policy;

/// <summary>
/// Licensed to the Apache Software Foundation (ASF) under one
Expand Down Expand Up @@ -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;
}

Expand All @@ -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");

Expand All @@ -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);
}
}

Expand Down
38 changes: 23 additions & 15 deletions SharpPulsar/Auth/OAuth2/protocol/Metadata.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Policy;
using System.Text.Json.Serialization;
/// <summary>
/// Licensed to the Apache Software Foundation (ASF) under one
/// or more contributor license agreements. See the NOTICE file
Expand All @@ -25,20 +26,27 @@ namespace SharpPulsar.Auth.OAuth2.Protocol
/// </summary>
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; }
}

}
19 changes: 10 additions & 9 deletions SharpPulsar/Auth/OAuth2/protocol/TokenClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net.Http;

/// <summary>
/// Licensed to the Apache Software Foundation (ASF) under one
Expand All @@ -25,22 +26,22 @@ namespace SharpPulsar.Auth.OAuth2.Protocol
/// <summary>
/// A client for an OAuth 2.0 token endpoint.
/// </summary>
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);
Expand All @@ -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();
}

/// <summary>
Expand Down
28 changes: 17 additions & 11 deletions SharpPulsar/Auth/OAuth2/protocol/TokenError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@
/// </summary>
namespace SharpPulsar.Auth.OAuth2.Protocol
{
/// <summary>
/// Represents an error returned from an OAuth 2.0 token endpoint.
/// </summary>

using System.Text.Json.Serialization;

/// <summary>
/// Represents an error returned from an OAuth 2.0 token endpoint.
/// </summary>
///
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; }
}

}
2 changes: 1 addition & 1 deletion SharpPulsar/Auth/OAuth2/protocol/TokenExchangeException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 14 additions & 9 deletions SharpPulsar/Auth/OAuth2/protocol/TokenResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text.Json.Serialization;

/// <summary>
/// Licensed to the Apache Software Foundation (ASF) under one
Expand Down Expand Up @@ -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; }
}

}
1 change: 1 addition & 0 deletions SharpPulsar/SocketImpl/SocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ private async Task ConnectAsync(IPAddress[] serverAddresses, int port)
try
{
await _socket.ConnectAsync(address, port).ConfigureAwait(false);
return;
}
catch (Exception exc)
{
Expand Down

0 comments on commit cfbb63d

Please sign in to comment.