-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EG API updates #18274
Merged
Merged
EG API updates #18274
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 109 additions & 103 deletions
212
sdk/eventgrid/Azure.Messaging.EventGrid/api/Azure.Messaging.EventGrid.netstandard2.0.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/EventGridSasBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Web; | ||
using Azure.Core; | ||
|
||
namespace Azure.Messaging.EventGrid | ||
{ | ||
/// <summary> | ||
/// This <see cref="EventGridSasBuilder"/> is used to generate a Shared Access Signature (SAS) for an Azure Event Grid topic. | ||
/// </summary> | ||
public class EventGridSasBuilder | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EventGridSasBuilder"/> class. | ||
/// </summary> | ||
/// <param name="endpoint">The endpoint to generate a shared access signature for. | ||
/// For example, "https://TOPIC-NAME.REGION-NAME.eventgrid.azure.net/eventGrid/api/events".</param> | ||
/// <param name="expiresOn">The time at which the shared access signature should expire.</param> | ||
public EventGridSasBuilder(Uri endpoint, DateTimeOffset expiresOn) | ||
{ | ||
Argument.AssertNotNull(endpoint, nameof(endpoint)); | ||
Endpoint = endpoint; | ||
ExpiresOn = expiresOn; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the endpoint to generate a shared access signature for. | ||
/// </summary> | ||
public Uri Endpoint | ||
{ | ||
get | ||
{ | ||
return _endpoint; | ||
} | ||
set | ||
{ | ||
Argument.AssertNotNull(value, nameof(value)); | ||
_endpoint = value; | ||
} | ||
} | ||
|
||
private Uri _endpoint; | ||
|
||
/// <summary> | ||
/// Gets or sets the time at which the shared access signature should expire. | ||
/// </summary> | ||
public DateTimeOffset ExpiresOn { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the service version to use when generating the shared access signature. | ||
/// </summary> | ||
public EventGridPublisherClientOptions.ServiceVersion ApiVersion { get; set; } = EventGridPublisherClientOptions.LatestVersion; | ||
|
||
/// <summary> | ||
/// Generates a shared access signature that can be used to authenticate with a topic. | ||
/// The signature can be used as the input to the <see cref="AzureSasCredential(string)"/> constructor. | ||
/// This credential can then be passed to the <see cref="EventGridPublisherClient(Uri, AzureSasCredential, EventGridPublisherClientOptions)"/> constructor. | ||
/// </summary> | ||
/// <param name="key">The <see cref="AzureKeyCredential"/> to use to authenticate with the service | ||
/// when generating the shared access signature.</param> | ||
/// <returns>A shared access signature that can be used to authenticate with an Event Grid topic.</returns> | ||
public string GenerateSas(AzureKeyCredential key) | ||
{ | ||
Argument.AssertNotNull(key, nameof(key)); | ||
const char Resource = 'r'; | ||
const char Expiration = 'e'; | ||
const char Signature = 's'; | ||
|
||
var uriBuilder = new RequestUriBuilder(); | ||
uriBuilder.Reset(Endpoint); | ||
uriBuilder.AppendQuery("api-version", ApiVersion.GetVersionString(), true); | ||
string encodedResource = HttpUtility.UrlEncode(uriBuilder.ToString()); | ||
var encodedExpirationUtc = HttpUtility.UrlEncode(ExpiresOn.ToString(CultureInfo.InvariantCulture)); | ||
|
||
string unsignedSas = $"{Resource}={encodedResource}&{Expiration}={encodedExpirationUtc}"; | ||
using (var hmac = new HMACSHA256(Convert.FromBase64String(key.Key))) | ||
{ | ||
string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(unsignedSas))); | ||
string encodedSignature = HttpUtility.UrlEncode(signature); | ||
string signedSas = $"{unsignedSas}&{Signature}={encodedSignature}"; | ||
|
||
return signedSas; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was previously using "en-us" for the culture as that is what was referenced in https://docs.microsoft.com/en-us/azure/event-grid/security-authenticate-publishing-clients#generate-sas-token-programmatically
But InvariantCulture seems to work fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slight preference for keeping en-US since that's what was documented by the service, but I agree in practice there is unlikely to be a difference between the two cultures in practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, will revert.