-
Notifications
You must be signed in to change notification settings - Fork 8
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
Migrate to b7a 1.0.3 and introduce basic auth for the hub service #70
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
afe7ff2
Migrate to b7a 1.0.3
MaryamZi 33abfb1
Add authn/z for the hub
MaryamZi 6be80ec
Allow specifying auth config for subscriber
MaryamZi af39084
Return an error on unsupported subscriber version
MaryamZi 238e2ca
Fix formatting
MaryamZi 3f4a5c6
Refactor code
MaryamZi 4c10ee7
Introduce a subscription filter
MaryamZi ad63077
Merge branch 'master' of https://github.com/ECLK/Results-Dist
MaryamZi 7249035
Persist username-callback tuples
MaryamZi 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
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 |
---|---|---|
@@ -1,8 +1,17 @@ | ||
import ballerina/auth; | ||
import ballerina/config; | ||
import ballerina/http; | ||
|
||
# Listener for results tabulation to deliver results to us. | ||
listener http:Listener resultsListener = new (config:getAsInt("eclk.pub.port", 8181)); | ||
|
||
http:BasicAuthHandler inboundBasicAuthHandler = new(new auth:InboundBasicAuthProvider()); | ||
|
||
# Listener for media orgs to subscribe, for the website and for them to pull specific results. | ||
listener http:Listener mediaListener = new (config:getAsInt("eclk.hub.port", 9090)); | ||
listener http:Listener mediaListener = new (config:getAsInt("eclk.hub.port", 9090), config = { | ||
auth: { | ||
authHandlers: [inboundBasicAuthHandler], | ||
mandateSecureSocket: false | ||
}, | ||
filters: [new SubscriptionFilter()] | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import ballerina/auth; | ||
import ballerina/encoding; | ||
import ballerina/http; | ||
import ballerina/log; | ||
import ballerina/websub; | ||
|
||
const HUB_TOPIC = "hub.topic"; | ||
const HUB_CALLBACK = "hub.callback"; | ||
|
||
map<string> callbackMap = {}; | ||
|
||
public type SubscriptionFilter object { | ||
*http:RequestFilter; | ||
|
||
public function filterRequest(http:Caller caller, http:Request request, http:FilterContext context) returns boolean { | ||
map<string>|error params = request.getFormParams(); | ||
|
||
if params is error { | ||
log:printError("error extracting form params", params); | ||
return false; | ||
} | ||
|
||
map<string> paramMap = <map<string>> params; | ||
if !paramMap.hasKey(HUB_TOPIC) || !paramMap.hasKey(HUB_CALLBACK) { | ||
log:printError("topic and/or callback not available"); | ||
return false; | ||
} | ||
|
||
string topic = paramMap.get(HUB_TOPIC); | ||
string callback = paramMap.get(HUB_CALLBACK); | ||
|
||
string|error decodedTopic = encoding:decodeUriComponent(topic, "UTF-8"); | ||
if (decodedTopic is string) { | ||
topic = decodedTopic; | ||
} else { | ||
log:printWarn("error decoding topic, using the original form: " + topic + ". Error: " + decodedTopic.toString()); | ||
} | ||
|
||
if (topic != JSON_RESULTS_TOPIC) { | ||
log:printError("subscription request received for invalid topic " + topic); | ||
return false; | ||
} | ||
|
||
string|error decodedCallback = encoding:decodeUriComponent(callback, "UTF-8"); | ||
if (decodedCallback is string) { | ||
callback = decodedCallback; | ||
} else { | ||
log:printWarn("error decoding callback, using the original form: " + callback + ". Error: " + decodedCallback.toString()); | ||
} | ||
|
||
websub:Hub hubVar = <websub:Hub> hub; | ||
|
||
if (!request.hasHeader(http:AUTH_HEADER)) { | ||
return false; | ||
} | ||
|
||
string headerValue = request.getHeader(http:AUTH_HEADER); | ||
|
||
if !(headerValue.startsWith(auth:AUTH_SCHEME_BASIC)) { | ||
return false; | ||
} | ||
|
||
string credential = headerValue.substring(5, headerValue.length()).trim(); | ||
|
||
var result = auth:extractUsernameAndPassword(credential); | ||
|
||
if (result is [string, string]) { | ||
[string, string][username, _] = result; | ||
|
||
if callbackMap.hasKey(username) { | ||
error? remResult = hubVar.removeSubscription(topic, callbackMap.get(username)); | ||
log:printInfo("Removing existing subscription for username: " + username); | ||
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. Show the callback URL |
||
if (remResult is error) { | ||
log:printError("error removing existing subscription for username: " + username, remResult); | ||
} | ||
updateUserCallback(username, 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. Add a log showing that we added a new subscription and show the callback URL |
||
} else { | ||
saveUserCallback(username, callback); | ||
} | ||
callbackMap[username] = <@untainted> callback; | ||
} else { | ||
log:printError("Error extracting credentials", result); | ||
return false; | ||
} | ||
return true; | ||
} | ||
}; |
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
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.
We need to support image sending too .. so user has to subscribe to both the JSON and the image.
See: #48
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.
We can fix this after merging this code.