-
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 all commits
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 |
---|---|---|
|
@@ -87,21 +87,36 @@ public IActionResult Post([FromForm] ClientModel model) | |
/// <param name="verification">Hub's verification response to our subscription attempt</param> | ||
/// <returns></returns> | ||
[HttpGet("{subscriptionId}")] | ||
public IActionResult Get(string subscriptionId, [FromQuery] SubscriptionVerification verification) | ||
public IActionResult Get(string subscriptionId) | ||
{ | ||
if (!Request.Query.ContainsKey("hub.challenge")) | ||
{ | ||
this.logger.LogDebug($"Missing hub.challenge"); | ||
return NotFound(); | ||
} | ||
|
||
if (!Request.Query.ContainsKey("hub.topic")) | ||
{ | ||
this.logger.LogDebug($"Missing hub.topic"); | ||
return NotFound(); | ||
} | ||
|
||
string challenge = Request.Query["hub.challenge"]; | ||
string topic = Request.Query["hub.topic"]; | ||
|
||
//Received a verification request for non-pending subscription, return a NotFound response | ||
if (!pendingSubs.ContainsKey(subscriptionId)) { return NotFound(); } | ||
|
||
Subscription sub = pendingSubs[subscriptionId]; | ||
|
||
//Validate verification subcription with our subscription. If a match return challenge | ||
//otherwise return NotFound response. | ||
if (SubsEqual(sub, verification)) | ||
if (topic == sub.Topic) | ||
{ | ||
//Move subscription to active sub collection and remove from pending subs | ||
activeSubs.Add(subscriptionId, sub); | ||
pendingSubs.Remove(subscriptionId); | ||
return this.Content(verification.Challenge); | ||
return this.Content(challenge); | ||
} | ||
else | ||
{ | ||
|
@@ -148,7 +163,7 @@ public async Task<IActionResult> Subscribe(string subscriptionUrl, string topic, | |
UID = subUID, | ||
Callback = new Uri(this.Request.Scheme + "://" + this.Request.Host + "/client/" + subUID), | ||
Events = events.Split(";", StringSplitOptions.RemoveEmptyEntries), | ||
Mode = SubscriptionMode.Subscribe, | ||
Mode = SubscriptionMode.subscribe, | ||
Secret = secret, | ||
LeaseSeconds = 3600, | ||
Topic = topic | ||
|
@@ -179,14 +194,14 @@ public async Task<IActionResult> Subscribe(string subscriptionUrl, string topic, | |
return View("FHIRcastClient", internalModel); | ||
} | ||
|
||
[Route("unsubscribe/{subscriptionId}")] | ||
[Route("Unsubscribe/{subscriptionId}")] | ||
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. Is this suggestion correct? Routes are generally lower-case and I think we should stick to that convention (even if it doesn't matter, technically). |
||
[HttpPost] | ||
public async Task<IActionResult> Unsubscribe(string subscriptionId) | ||
{ | ||
this.logger.LogDebug($"Unsubscribing subscription {subscriptionId}"); | ||
if (!activeSubs.ContainsKey(subscriptionId)) { return View("FHIRcastClient", internalModel); } | ||
Subscription sub = activeSubs[subscriptionId]; | ||
sub.Mode = SubscriptionMode.Unsubscribe; | ||
sub.Mode = SubscriptionMode.unsubscribe; | ||
|
||
var httpClient = new HttpClient(); | ||
var result = await httpClient.PostAsync(sub.HubURL, | ||
|
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
).