-
Notifications
You must be signed in to change notification settings - Fork 3
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
client url verification and unsubscribe #7
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,8 +62,8 @@ public class SubscriptionVerification : SubscriptionWithLease { | |
} | ||
|
||
public enum SubscriptionMode { | ||
Subscribe, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should change the Enum casing just to get an easy fix for |
||
Unsubscribe, | ||
subscribe, | ||
unsubscribe, | ||
} | ||
|
||
public class SubscriptionComparer : IEqualityComparer<Subscription> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,40 +18,49 @@ public async Task<ClientValidationOutcome> ValidateSubscription(Subscription sub | |
throw new ArgumentNullException(nameof(subscription)); | ||
} | ||
|
||
SubscriptionBase callbackParameters = null; | ||
|
||
HttpResponseMessage response = null; | ||
string challenge = Guid.NewGuid().ToString("n"); | ||
|
||
if (outcome == HubValidationOutcome.Canceled) { | ||
logger.LogDebug("Simulating canceled subscription."); | ||
|
||
callbackParameters = new SubscriptionCancelled | ||
SubscriptionBase callbackParameters = new SubscriptionCancelled | ||
{ | ||
Reason = $"The subscription {subscription} was canceled for testing purposes.", | ||
}; | ||
} else { | ||
logger.LogDebug("Verifying subscription."); | ||
|
||
callbackParameters = new SubscriptionVerification | ||
SubscriptionVerification callbackParameters = new SubscriptionVerification | ||
{ | ||
// Note that this is not necessarily cryptographically random/secure. | ||
Challenge = Guid.NewGuid().ToString("n"), | ||
}; | ||
} | ||
Challenge = challenge, | ||
LeaseSeconds = subscription.LeaseSeconds, | ||
Callback = subscription.Callback, | ||
Events = subscription.Events, | ||
Mode = subscription.Mode, | ||
Topic = subscription.Topic, | ||
}; | ||
|
||
// Default parametres for both cancel/verify. | ||
callbackParameters.Callback = subscription.Callback; | ||
callbackParameters.Events = subscription.Events; | ||
callbackParameters.Mode = subscription.Mode; | ||
callbackParameters.Topic = subscription.Topic; | ||
logger.LogDebug($"Calling callback url: {subscription.Callback}"); | ||
string verifyUrl = subscription.Callback + "?" + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You moved the logic for constructing the URL from the |
||
$"&hub.mode={callbackParameters.Mode.ToString().ToLower()}" + | ||
$"&hub.topic={callbackParameters.Topic}" + | ||
$"&hub.challenge={callbackParameters.Challenge}" + | ||
$"&hub.events={string.Join(",", callbackParameters.Events)}" + | ||
$"&hub.lease_seconds={callbackParameters.LeaseSeconds}"; | ||
response = await new HttpClient().GetAsync(verifyUrl); | ||
} | ||
|
||
logger.LogDebug($"Calling callback url: {subscription.Callback}"); | ||
var callbackUri = new SubscriptionCallback().GetCallbackUri(subscription, callbackParameters); | ||
var response = await new HttpClient().GetAsync(callbackUri); | ||
|
||
|
||
if (!response.IsSuccessStatusCode) { | ||
logger.LogInformation($"Status code was not success but instead {response.StatusCode}"); | ||
return ClientValidationOutcome.NotVerified; | ||
} | ||
if (outcome == HubValidationOutcome.Valid) { | ||
var challenge = ((SubscriptionVerification)callbackParameters).Challenge; | ||
|
||
var responseBody = (await response.Content.ReadAsStringAsync()); | ||
if (responseBody != challenge) { | ||
logger.LogInformation($"Callback result for verification request was not equal to challenge. Response body: '{responseBody}', Challenge: '{challenge}'."); | ||
|
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.
Did you see how validation is done when posting subscriptions? The validation is basically set on the model using attributes specifying what is required etc. The framework then generates error messages that can be used in the response (which should probably be
Bad request
and notNot found
).