Skip to content

Commit

Permalink
Supports authorization token
Browse files Browse the repository at this point in the history
Fixes sort order

Signed-off-by: Daniel Kastl <[email protected]>
  • Loading branch information
dkastl committed May 22, 2024
1 parent 068aac1 commit bd392ae
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
10 changes: 9 additions & 1 deletion app/views/subscription_templates/_list.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<% if subscription_templates.any? %>
<div class="box tabular">
<p class="min-width">
<%= content_tag :label, l(:field_subscription_auth_token) %>
<%= text_field_tag :subscription_auth_token, "", :size => 70, :placeholder => l(:field_subscription_auth_token_placeholder) %><br>
<em><%= l(:field_subscription_auth_token_hint) %></em>
</p>
</div>

<table class="list subscription_templates">
<thead>
<tr>
Expand All @@ -14,7 +22,7 @@
</tr>
</thead>
<tbody id="subscriptionTemplateList">
<%= render collection: subscription_templates, partial: 'subscription_templates/subscription_template' %>
<%= render collection: subscription_templates.sort_by(&:name), partial: 'subscription_templates/subscription_template' %>
</tbody>
</table>

Expand Down
6 changes: 6 additions & 0 deletions app/views/subscription_templates/copy.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ var command = "curl -i -X POST '<%= @broker_url %>' \\\n" +
"-H 'Fiware-ServicePath: <%= @subscription_template.fiware_servicepath %>' \\\n" +
<% end %>
"--data-binary '" + '<%= @json_payload.html_safe %>' + "'";

var authToken = document.getElementById('subscription_auth_token').value;
if (authToken) {
command += " \\\n-H 'Authorization: Bearer " + authToken + "'";
}

console.log(command);

// Function to copy command to clipboard
Expand Down
29 changes: 25 additions & 4 deletions app/views/subscription_templates/publish.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ headers.append('Content-Type', 'application/json');
headers.append('Fiware-ServicePath', '<%= @subscription_template.fiware_servicepath %>');
<% end %>

var authToken = document.getElementById('subscription_auth_token').value;
if (authToken) {
headers.append('Authorization', 'Bearer ' + authToken);
}

// Build the request options
var requestOptions = {
method: 'POST',
Expand Down Expand Up @@ -37,11 +42,27 @@ function showNotification(message) {
// Send the POST request
fetch('<%= @broker_url %>', requestOptions)
.then(response => {
var location = response.headers.get('Location');
if (!location) {
if (!response.ok) {
if (response.status === 401) {
throw new Error("<%= l(:subscription_unauthorized_error) %>");
} else {
throw new Error("<%= l(:subscription_published_error) %>");
}
}

// Case-insensitive retrieval of the "Location" header
var locationHeader = null;
response.headers.forEach((value, name) => {
if (name.toLowerCase() === 'location') {
locationHeader = value;
}
});

if (!locationHeader) {
throw new Error('Location header is missing in the response');
}
var parts = location.split('/');

var parts = locationHeader.split('/');
var subscriptionId = parts[parts.length - 1];

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

// Show error notification
showNotification("<%= l(:subscription_published_error) %>");
showNotification(error.message);
});
5 changes: 5 additions & 0 deletions app/views/subscription_templates/unpublish.js.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Build the request headers
var headers = new Headers();

var authToken = document.getElementById('subscription_auth_token').value;
if (authToken) {
headers.append('Authorization', 'Bearer ' + authToken);
}

// Build the request options for the DELETE request
var deleteRequestOptions = {
method: 'DELETE',
Expand Down
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ en:
project_geometry_not_defined: "Project boundary is not defined"
project_geometry_not_supported_multipolygon: "Project boundary is a multipolygon, which is not supported for geospatial queries"

field_subscription_auth_token: "Authorization token"
field_subscription_auth_token_placeholder: "Bearer token"
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."

link_to_copy: "Clipboard"
link_to_copy_hint: "Copy command to clipboard"
command_copied: "Command copied to clipboard"
Expand All @@ -89,6 +93,8 @@ en:
subscription_unpublished: "Subscription unpublished from broker"
subscription_unpublished_warning: "There was a problem unpublishing the subscription from the broker"

subscription_unauthorized_error: "Unauthorized to perform this action"

gtt_fiware:
attachement_not_found: "Attachment not found"
category_not_found: "Category not found"
Expand Down
6 changes: 6 additions & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ en:
project_geometry_not_defined: "Project boundary is not defined"
project_geometry_not_supported_multipolygon: "Project boundary is a multipolygon, which is not supported for geospatial queries"

field_subscription_auth_token: "Authorization token"
field_subscription_auth_token_placeholder: "Bearer token"
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."

link_to_copy: "Clipboard"
link_to_copy_hint: "Copy command to clipboard"
command_copied: "Command copied to clipboard"
Expand All @@ -89,6 +93,8 @@ en:
subscription_unpublished: "Subscription unpublished from broker"
subscription_unpublished_warning: "There was a problem unpublishing the subscription from the broker"

subscription_unauthorized_error: "Unauthorized to perform this action"

gtt_fiware:
attachement_not_found: "Attachment not found"
category_not_found: "Category not found"
Expand Down

0 comments on commit bd392ae

Please sign in to comment.