Skip to content

Commit

Permalink
review: Allows creating UrlSigner directly from a StorageClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
amanda-tarafa committed Mar 5, 2024
1 parent 7f2bc6b commit 24db1df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void StorageClientSignerTest(SigningV4Test test)
(test.ClientEndpoint.StartsWith("http") ? test.ClientEndpoint : $"https://{test.ClientEndpoint}"),
Credential = StorageConformanceTestData.TestCredential,
}.Build();
var signer = storageClient.UrlSigner;
var signer = storageClient.CreateUrlSigner();

SignerTest(test, signer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,41 +66,6 @@ public abstract partial class StorageClient : IDisposable
/// </summary>
public virtual EncryptionKey EncryptionKey { get { throw new NotImplementedException(); } }

/// <summary>
/// A URL signer built base on this client, that is using the same credential as this client, as well
/// as defaulting to this client's URI scheme and host. If the credential used by this client is not compatible
/// with <see cref="UrlSigner"/> this will be null. See <see cref="UrlSigner"/>'s documentation for more information
/// on compatible credentials.
/// </summary>
/// <remarks>
/// Because credentials used by this client may changed, this property will return a new <see cref="UrlSigner"/> instance
/// each time it is called.
/// </remarks>
public UrlSigner UrlSigner
{
get
{
UrlSigner signer;
try
{
signer = UrlSigner.FromCredential(Service.HttpClient.MessageHandler.Credential);
}
catch (InvalidOperationException)
{
// The credential does not support signing.
return null;
}

var baseUri = new Uri(Service.BaseUri);
return signer.WithDefaultOptionsOverride(new UrlSigner.DefaultOptionsOverrides(
baseUri.Scheme, baseUri.Host,
// If the original URI didn't specify a port, we want signed URLs to not have a port either;
// but Uri.Port returns the default port for the URI scheme when the port was not specified on the
// original URI.
baseUri.IsDefaultPort && !Service.BaseUri.Contains(baseUri.Port.ToString()) ? null : baseUri.Port));
}
}

/// <summary>
/// Asynchronously creates a <see cref="StorageClient"/> using application default credentials.
/// For any non-default values, please use <see cref="StorageClientBuilder"/>.
Expand Down Expand Up @@ -167,6 +132,29 @@ public static StorageClient CreateUnauthenticated() =>
UnauthenticatedAccess = true
}.Build();

/// <summary>
/// Creates a URL signer built base on this client, that is using the same credential as this client, as well
/// as defaulting to this client's URI scheme, host and port. If the credential used by this client is not compatible
/// with <see cref="UrlSigner"/> this method will throw <see cref="InvalidOperationException"/>.
/// See <see cref="UrlSigner"/>'s documentation for more information on compatible credentials.
/// </summary>
/// <remarks>
/// Because credentials used by this client may changed, this method will always return a new <see cref="UrlSigner"/> instance
/// each time it is called.
/// </remarks>
public UrlSigner CreateUrlSigner()
{
UrlSigner signer = UrlSigner.FromCredential(Service.HttpClient.MessageHandler.Credential);

var baseUri = new Uri(Service.BaseUri);
return signer.WithDefaultOptionsOverride(new UrlSigner.DefaultOptionsOverrides(
baseUri.Scheme, baseUri.Host,
// If the original URI didn't specify a port, we want signed URLs to not have a port either;
// but Uri.Port returns the default port for the URI scheme when the port was not specified on the
// original URI.
baseUri.IsDefaultPort && !Service.BaseUri.Contains(baseUri.Port.ToString()) ? null : baseUri.Port));
}

/// <summary>
/// Dispose of this instance. See the <see cref="StorageClient"/> remarks on when this should be called.
/// </summary>
Expand Down

0 comments on commit 24db1df

Please sign in to comment.