Skip to content

Commit bd392ae

Browse files
committed
Supports authorization token
Fixes sort order Signed-off-by: Daniel Kastl <[email protected]>
1 parent 068aac1 commit bd392ae

File tree

6 files changed

+57
-5
lines changed

6 files changed

+57
-5
lines changed

app/views/subscription_templates/_list.html.erb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
<% if subscription_templates.any? %>
2+
<div class="box tabular">
3+
<p class="min-width">
4+
<%= content_tag :label, l(:field_subscription_auth_token) %>
5+
<%= text_field_tag :subscription_auth_token, "", :size => 70, :placeholder => l(:field_subscription_auth_token_placeholder) %><br>
6+
<em><%= l(:field_subscription_auth_token_hint) %></em>
7+
</p>
8+
</div>
9+
210
<table class="list subscription_templates">
311
<thead>
412
<tr>
@@ -14,7 +22,7 @@
1422
</tr>
1523
</thead>
1624
<tbody id="subscriptionTemplateList">
17-
<%= render collection: subscription_templates, partial: 'subscription_templates/subscription_template' %>
25+
<%= render collection: subscription_templates.sort_by(&:name), partial: 'subscription_templates/subscription_template' %>
1826
</tbody>
1927
</table>
2028

app/views/subscription_templates/copy.js.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ var command = "curl -i -X POST '<%= @broker_url %>' \\\n" +
88
"-H 'Fiware-ServicePath: <%= @subscription_template.fiware_servicepath %>' \\\n" +
99
<% end %>
1010
"--data-binary '" + '<%= @json_payload.html_safe %>' + "'";
11+
12+
var authToken = document.getElementById('subscription_auth_token').value;
13+
if (authToken) {
14+
command += " \\\n-H 'Authorization: Bearer " + authToken + "'";
15+
}
16+
1117
console.log(command);
1218

1319
// Function to copy command to clipboard

app/views/subscription_templates/publish.js.erb

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ headers.append('Content-Type', 'application/json');
1010
headers.append('Fiware-ServicePath', '<%= @subscription_template.fiware_servicepath %>');
1111
<% end %>
1212

13+
var authToken = document.getElementById('subscription_auth_token').value;
14+
if (authToken) {
15+
headers.append('Authorization', 'Bearer ' + authToken);
16+
}
17+
1318
// Build the request options
1419
var requestOptions = {
1520
method: 'POST',
@@ -37,11 +42,27 @@ function showNotification(message) {
3742
// Send the POST request
3843
fetch('<%= @broker_url %>', requestOptions)
3944
.then(response => {
40-
var location = response.headers.get('Location');
41-
if (!location) {
45+
if (!response.ok) {
46+
if (response.status === 401) {
47+
throw new Error("<%= l(:subscription_unauthorized_error) %>");
48+
} else {
49+
throw new Error("<%= l(:subscription_published_error) %>");
50+
}
51+
}
52+
53+
// Case-insensitive retrieval of the "Location" header
54+
var locationHeader = null;
55+
response.headers.forEach((value, name) => {
56+
if (name.toLowerCase() === 'location') {
57+
locationHeader = value;
58+
}
59+
});
60+
61+
if (!locationHeader) {
4262
throw new Error('Location header is missing in the response');
4363
}
44-
var parts = location.split('/');
64+
65+
var parts = locationHeader.split('/');
4566
var subscriptionId = parts[parts.length - 1];
4667

4768
// Send the PATCH request to update the subscription_id
@@ -67,5 +88,5 @@ fetch('<%= @broker_url %>', requestOptions)
6788
console.log('error', error);
6889

6990
// Show error notification
70-
showNotification("<%= l(:subscription_published_error) %>");
91+
showNotification(error.message);
7192
});

app/views/subscription_templates/unpublish.js.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// Build the request headers
22
var headers = new Headers();
33

4+
var authToken = document.getElementById('subscription_auth_token').value;
5+
if (authToken) {
6+
headers.append('Authorization', 'Bearer ' + authToken);
7+
}
8+
49
// Build the request options for the DELETE request
510
var deleteRequestOptions = {
611
method: 'DELETE',

config/locales/en.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ en:
7575
project_geometry_not_defined: "Project boundary is not defined"
7676
project_geometry_not_supported_multipolygon: "Project boundary is a multipolygon, which is not supported for geospatial queries"
7777

78+
field_subscription_auth_token: "Authorization token"
79+
field_subscription_auth_token_placeholder: "Bearer token"
80+
field_subscription_auth_token_hint: "Authorization token to be included in the request headers. Leave empty for no authorization. The token is not stored in the database."
81+
7882
link_to_copy: "Clipboard"
7983
link_to_copy_hint: "Copy command to clipboard"
8084
command_copied: "Command copied to clipboard"
@@ -89,6 +93,8 @@ en:
8993
subscription_unpublished: "Subscription unpublished from broker"
9094
subscription_unpublished_warning: "There was a problem unpublishing the subscription from the broker"
9195

96+
subscription_unauthorized_error: "Unauthorized to perform this action"
97+
9298
gtt_fiware:
9399
attachement_not_found: "Attachment not found"
94100
category_not_found: "Category not found"

config/locales/ja.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ en:
7575
project_geometry_not_defined: "Project boundary is not defined"
7676
project_geometry_not_supported_multipolygon: "Project boundary is a multipolygon, which is not supported for geospatial queries"
7777

78+
field_subscription_auth_token: "Authorization token"
79+
field_subscription_auth_token_placeholder: "Bearer token"
80+
field_subscription_auth_token_hint: "Authorization token to be included in the request headers. Leave empty for no authorization. The token is not stored in the database."
81+
7882
link_to_copy: "Clipboard"
7983
link_to_copy_hint: "Copy command to clipboard"
8084
command_copied: "Command copied to clipboard"
@@ -89,6 +93,8 @@ en:
8993
subscription_unpublished: "Subscription unpublished from broker"
9094
subscription_unpublished_warning: "There was a problem unpublishing the subscription from the broker"
9195

96+
subscription_unauthorized_error: "Unauthorized to perform this action"
97+
9298
gtt_fiware:
9399
attachement_not_found: "Attachment not found"
94100
category_not_found: "Category not found"

0 commit comments

Comments
 (0)