Skip to content

Commit 3ac34d9

Browse files
committed
Add basic struture to support subscription auto-verification
1 parent c6188a0 commit 3ac34d9

File tree

5 files changed

+49
-44
lines changed

5 files changed

+49
-44
lines changed

Diff for: ballerina/commons.bal

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ const int LISTENER_DETACH_ERROR = -4;
5555
const int LISTENER_STOP_ERROR = -5;
5656
const int CLIENT_INIT_ERROR = -10;
5757

58+
const DEFAULT_HUB_LEASE_SECONDS = 86400;
59+
5860
# Options to compress using Gzip or deflate.
5961
#
6062
# `AUTO`: When service behaves as a HTTP gateway inbound request/response accept-encoding option is set as the

Diff for: ballerina/http_service.bal

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ isolated service class HttpService {
2626
private final int defaultLeaseSeconds;
2727
private final SubscriptionHandler subscriptionHandler;
2828

29-
isolated function init(HttpToWebsubhubAdaptor adaptor, string hubUrl, int leaseSeconds,
30-
*ClientConfiguration clientConfig) {
29+
isolated function init(HttpToWebsubhubAdaptor adaptor, string hubUrl, ServiceConfiguration? serviceConfig) {
3130
self.adaptor = adaptor;
3231
self.hub = hubUrl;
33-
self.defaultLeaseSeconds = leaseSeconds;
34-
self.subscriptionHandler = new (adaptor, clientConfig);
32+
self.defaultLeaseSeconds = serviceConfig?.leaseSeconds ?: DEFAULT_HUB_LEASE_SECONDS;
33+
ClientConfiguration clientConfig = serviceConfig?.webHookConfig ?: {};
34+
boolean autoVerifySubscription = serviceConfig?.autoVerifySubscription ?: false;
35+
self.subscriptionHandler = new (adaptor, autoVerifySubscription, clientConfig);
3536
}
3637

3738
isolated resource function post .(http:Caller caller, http:Request request, http:Headers headers) returns Error? {

Diff for: ballerina/hub_controller.bal

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
// todo: implement this properly
1818

1919
public isolated class Controller {
20+
private final boolean autoVerifySubscription;
2021

21-
public function markAsVerified(Subscription|Unsubscription subscription) returns error? {
22+
isolated function init(boolean autoVerifySubscription) {
23+
self.autoVerifySubscription = autoVerifySubscription;
24+
}
25+
26+
public isolated function markAsVerified(Subscription|Unsubscription subscription) returns error? {
2227
return error("Not implemented yet");
2328
}
2429

25-
function skipSubscriptionVerification(Subscription|Unsubscription subscription) returns boolean {
30+
isolated function skipSubscriptionVerification(Subscription|Unsubscription subscription) returns boolean {
2631
return false;
2732
}
2833
}

Diff for: ballerina/hub_listener.bal

+4-16
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import ballerina/log;
1919

2020
# Represents a Service listener endpoint.
2121
public class Listener {
22-
private final int defaultHubLeaseSeconds = 864000;
2322
private http:Listener httpListener;
2423
private http:InferredListenerConfiguration listenerConfig;
2524
private int port;
@@ -65,18 +64,9 @@ public class Listener {
6564

6665
string hubUrl = self.retrieveHubUrl(name);
6766
ServiceConfiguration? configuration = retrieveServiceAnnotations('service);
68-
HttpToWebsubhubAdaptor adaptor = new('service);
69-
if configuration is ServiceConfiguration {
70-
int leaseSeconds = configuration?.leaseSeconds is int ? <int>(configuration?.leaseSeconds) : self.defaultHubLeaseSeconds;
71-
if configuration?.webHookConfig is ClientConfiguration {
72-
self.httpService = new(adaptor, hubUrl, leaseSeconds, <ClientConfiguration>(configuration?.webHookConfig));
73-
} else {
74-
self.httpService = new(adaptor, hubUrl, leaseSeconds);
75-
}
76-
} else {
77-
self.httpService = new(adaptor, hubUrl, self.defaultHubLeaseSeconds);
78-
}
79-
error? result = self.httpListener.attach(<HttpService> self.httpService, name);
67+
HttpToWebsubhubAdaptor adaptor = new ('service);
68+
self.httpService = new (adaptor, hubUrl, configuration);
69+
error? result = self.httpListener.attach(<HttpService>self.httpService, name);
8070
if (result is error) {
8171
return error Error("Error occurred while attaching the service", result, statusCode = LISTENER_ATTACH_ERROR);
8272
}
@@ -92,9 +82,7 @@ public class Listener {
9282
isolated function retrieveHubUrl(string[]|string? servicePath) returns string {
9383
string host = self.listenerConfig.host;
9484
string protocol = self.listenerConfig.secureSocket is () ? "http" : "https";
95-
96-
string concatenatedServicePath = "";
97-
85+
string concatenatedServicePath = "";
9886
if servicePath is string {
9987
concatenatedServicePath += "/" + <string>servicePath;
10088
} else if servicePath is string[] {

Diff for: ballerina/subscription.bal

+31-22
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ import ballerina/uuid;
1919

2020
isolated class SubscriptionHandler {
2121
private final HttpToWebsubhubAdaptor adaptor;
22+
private final Controller hubController;
2223
private final readonly & ClientConfiguration clientConfig;
2324

2425
private final boolean isOnSubscriptionAvailable;
2526
private final boolean isOnSubscriptionValidationAvailable;
2627
private final boolean isOnUnsubscriptionAvailable;
2728
private final boolean isOnUnsubscriptionValidationAvailable;
2829

29-
isolated function init(HttpToWebsubhubAdaptor adaptor, *ClientConfiguration clientConfig) {
30+
isolated function init(HttpToWebsubhubAdaptor adaptor, boolean autoVerifySubscription,
31+
ClientConfiguration clientConfig) {
3032
self.adaptor = adaptor;
33+
self.hubController = new (autoVerifySubscription);
3134
self.clientConfig = clientConfig.cloneReadOnly();
3235
string[] methodNames = adaptor.getServiceMethodNames();
3336
self.isOnSubscriptionAvailable = methodNames.indexOf("onSubscription") is int;
@@ -63,17 +66,20 @@ isolated class SubscriptionHandler {
6366
return;
6467
}
6568

66-
string challenge = uuid:createType4AsString();
67-
[string, string?][] params = [
68-
[HUB_MODE, MODE_SUBSCRIBE],
69-
[HUB_TOPIC, message.hubTopic],
70-
[HUB_CHALLENGE, challenge],
71-
[HUB_LEASE_SECONDS, message.hubLeaseSeconds]
72-
];
73-
http:Response subscriberResponse = check sendNotification(message.hubCallback, params, self.clientConfig);
74-
string responsePayload = check subscriberResponse.getTextPayload();
75-
if challenge != responsePayload {
76-
return;
69+
boolean skipIntentVerification = self.hubController.skipSubscriptionVerification(message);
70+
if !skipIntentVerification {
71+
string challenge = uuid:createType4AsString();
72+
[string, string?][] params = [
73+
[HUB_MODE, MODE_SUBSCRIBE],
74+
[HUB_TOPIC, message.hubTopic],
75+
[HUB_CHALLENGE, challenge],
76+
[HUB_LEASE_SECONDS, message.hubLeaseSeconds]
77+
];
78+
http:Response subscriberResponse = check sendNotification(message.hubCallback, params, self.clientConfig);
79+
string responsePayload = check subscriberResponse.getTextPayload();
80+
if challenge != responsePayload {
81+
return;
82+
}
7783
}
7884

7985
VerifiedSubscription verifiedSubscription = {
@@ -115,16 +121,19 @@ isolated class SubscriptionHandler {
115121
_ = check sendNotification(message.hubCallback, params, self.clientConfig);
116122
}
117123

118-
string challenge = uuid:createType4AsString();
119-
[string, string?][] params = [
120-
[HUB_MODE, MODE_UNSUBSCRIBE],
121-
[HUB_TOPIC, message.hubTopic],
122-
[HUB_CHALLENGE, challenge]
123-
];
124-
http:Response subscriberResponse = check sendNotification(message.hubCallback, params, self.clientConfig);
125-
string responsePayload = check subscriberResponse.getTextPayload();
126-
if challenge != responsePayload {
127-
return;
124+
boolean skipIntentVerification = self.hubController.skipSubscriptionVerification(message);
125+
if !skipIntentVerification {
126+
string challenge = uuid:createType4AsString();
127+
[string, string?][] params = [
128+
[HUB_MODE, MODE_UNSUBSCRIBE],
129+
[HUB_TOPIC, message.hubTopic],
130+
[HUB_CHALLENGE, challenge]
131+
];
132+
http:Response subscriberResponse = check sendNotification(message.hubCallback, params, self.clientConfig);
133+
string responsePayload = check subscriberResponse.getTextPayload();
134+
if challenge != responsePayload {
135+
return;
136+
}
128137
}
129138

130139
VerifiedUnsubscription verifiedUnsubscription = {

0 commit comments

Comments
 (0)