Skip to content

Commit 73377d3

Browse files
EI Subscription REST POST supporting authentication headers (#28)
* code changes 9561 * formatting * reviewed changes * changing port number as it was before * added spacing for Authorizationjusted
1 parent bc13418 commit 73377d3

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

src/main/resources/static/js/subscription.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var save_method;
33
var table;
44
var frontendServiceUrl;
55
var defaultFormKeyValuePair = {"formkey" : "","formvalue" : ""};
6-
6+
var defaultFormKeyValuePairAuth = {"formkey" : "Authorization","formvalue" : ""};
77

88
jQuery(document).ready(function() {
99

@@ -109,10 +109,14 @@ jQuery(document).ready(function() {
109109
this.notificationType = ko.observable(data.notificationType);
110110
this.restPostBodyMediaType = ko.observable(data.restPostBodyMediaType);
111111
this.notificationMessageKeyValues = ko.observableArray(data.notificationMessageKeyValues);
112+
this.notificationMessageKeyValuesAuth = ko.observableArray(data.notificationMessageKeyValuesAuth);
112113
this.repeat = ko.observable(data.repeat);
113114
this.requirements = ko.observableArray(data.requirements);
114115
this.subscriptionName = ko.observable(data.subscriptionName);
115116
this.aggregationtype = ko.observable(data.aggregationtype);
117+
this.authenticationType = ko.observable(data.authenticationType);
118+
this.userName = ko.observable(data.userName);
119+
this.token = ko.observable(data.token);
116120

117121
this.notificationType.subscribe(function (new_value) {
118122
vm.subscription()[0].restPostBodyMediaType(null);
@@ -127,7 +131,6 @@ jQuery(document).ready(function() {
127131
vm.formpostkeyvaluepairs(false);
128132
}
129133
});
130-
131134
}
132135

133136
function formdata_model(formdata){
@@ -155,12 +158,20 @@ jQuery(document).ready(function() {
155158
{"text": "Mail Trigger", value:"templateEmailTrigger"}
156159
]);
157160
self.choosen_subscription_template = ko.observable();
161+
self.authenticationType = ko.observable();
158162
self.formpostkeyvaluepairs = ko.observable(false);
163+
self.formpostkeyvaluepairsAuth = ko.observable(false);
159164
self.notificationType_in = ko.observableArray(
160165
[
161166
{"text": "REST_POST", value:"REST_POST"},
162167
{"text": "MAIL", value:"MAIL"}
163168
]);
169+
self.authenticationType_in = ko.observableArray(
170+
[
171+
{"text": "NO_AUTH", value:"NO_AUTH"},
172+
{"text": "BASIC_AUTH", value:"BASIC_AUTH"}
173+
]);
174+
164175

165176
self.restPostBodyType_in = ko.observableArray(
166177
[
@@ -203,6 +214,19 @@ jQuery(document).ready(function() {
203214
self.subscription(data);
204215
self.subscription.valueHasMutated();
205216
};
217+
218+
self.addNotificationMsgKeyValuePairAuth = function(data, event) {
219+
data.notificationMessageKeyValues.push({"formkey" : "Authorization","formvalue" : ko.computed(function() {
220+
return "Basic " + btoa(data.userName() + ":" + data.token());
221+
222+
})});
223+
// ko.observable(value);
224+
// Force update
225+
var data = self.subscription().slice(0);
226+
self.subscription([]);
227+
self.subscription(data);
228+
self.subscription.valueHasMutated();
229+
};
206230

207231

208232
self.getUTCDate= function(epochtime){
@@ -245,11 +269,8 @@ jQuery(document).ready(function() {
245269
}
246270
});
247271
};
248-
249-
250-
251-
252272
};
273+
253274

254275
// Start to check is backend secured
255276
var isSecured = false;
@@ -277,13 +298,11 @@ jQuery(document).ready(function() {
277298
var vm = new SubscriptionViewModel();
278299
ko.applyBindings(vm, observableObject);
279300

280-
281301
// /Stop ## Knockout #####################################################
282302

283303

284304

285305

286-
287306
// /Start ## Datatables ##################################################
288307
var currentUser = localStorage.getItem("currentUser");
289308
table = $('#table').DataTable({
@@ -646,6 +665,8 @@ jQuery(document).ready(function() {
646665
for (i=0; i < item[0].notificationMessageKeyValues.length; i++) {
647666
item[0].notificationMessageKeyValues[i] = new formdata_model(item[0].notificationMessageKeyValues[i])
648667
}
668+
669+
649670
return new subscription_model(item[0]);
650671
});
651672
// Load data into observable array
@@ -689,6 +710,7 @@ jQuery(document).ready(function() {
689710
if(!vm.formpostkeyvaluepairs()) {
690711
notificationMessageKeyValuesArray[0].formkey=""; // OBS must be empty when NOT using REST POST Form key/value pairs
691712
}
713+
692714

693715
//START: Make sure all datatables field has a value
694716
if (!(/[a-z]|[A-Z]|[0-9]|[\_]/.test(String(vm.subscription()[0].subscriptionName()).slice(-1)))) {
@@ -738,8 +760,6 @@ jQuery(document).ready(function() {
738760
//END OF: Make sure all datatables field has a value
739761

740762

741-
742-
743763
//START: Check of other subscription fields values
744764
for (i=0; i < notificationMessageKeyValuesArray.length; i++) {
745765
var test_key = ko.toJSON(notificationMessageKeyValuesArray[i].formkey);

src/main/resources/static/resources/subscription_templates.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ template_vars["templatejenkinsPipelineParameterizedBuildTrigger"] =
4141
{
4242
"formkey" : "json",
4343
"formvalue" : "{parameter: [{ name: 'jsonparams', value : to_string(@) }, { name: 'runpipeline', value : 'mybuildstep' }]}"
44-
}
45-
],
44+
}
45+
],
4646
"repeat" : false,
4747
"requirements" : [
4848
{

src/main/resources/templates/subscription.html

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ <h3 class="modal-title text-center">Subscription Form</h3>
147147
<tr>
148148
<td data-bind="visible: $root.formpostkeyvaluepairs">
149149
<input data-toggle="tooltip" title="Specify a Key" data-bind="textInput:$data.formkey" name="formkey"
150-
placeholder="Key" class="form-control" type="text"/>
150+
placeholder="Key" class="form-control" type="text"/>
151151
</td>
152+
152153
<td>
153154
<textarea data-toggle="tooltip" title="Choose a notification message" data-bind="textInput:$data.formvalue"
154155
name="formvalue" placeholder="Value" class="form-control" type="text"></textarea>
@@ -169,10 +170,38 @@ <h3 class="modal-title text-center">Subscription Form</h3>
169170
class="glyphicon glyphicon-plus float-right"></i> Add Key/Value Pair
170171
</button>
171172
</div>
172-
173+
</div>
174+
175+
<div class="pt-3 form-group">
176+
<label class="pl-1 control-label font-weight-bold">Authorization</label>
177+
<div>
178+
<select data-toggle="tooltip" title="Choose an authentication type" data-bind="options: $root.authenticationType_in,
179+
optionsText: 'text',
180+
optionsValue: 'value',
181+
value: authenticationType,
182+
optionsCaption: 'Choose...'"></select>
183+
</div>
173184
</div>
174-
175-
185+
186+
<div class="pb-3 border border-primary form-group" data-bind="visible: authenticationType() == 'BASIC_AUTH'">
187+
<label class="pl-1 control-label font-weight-bold">User Name*</label>
188+
<div>
189+
<input data-toggle="tooltip" title="Enter user name" data-bind="textInput:$data.userName" name="userName" placeholder="userName" class="form-control" type="text" />
190+
<span class="help-block"></span>
191+
</div>
192+
193+
<label class="pl-1 control-label font-weight-bold">Token*</label>
194+
<div>
195+
<input data-toggle="tooltip" title="Enter token" data-bind="textInput:$data.token" name="token" placeholder="token" class="form-control" type="password" />
196+
<span class="help-block"></span>
197+
</div>
198+
199+
<div>
200+
<button data-bind="click: $root.addNotificationMsgKeyValuePairAuth, clickBubble: false" data-toggle="tooltip" title="Generate Key/Value Pair" class="btn btn-success float-left"><i class="glyphicon glyphicon-plus float-right"></i> Generate Key/Value Pair</button>
201+
</div>
202+
</div>
203+
204+
176205
<div class="pt-3 form-group">
177206
<label class="pl-1 control-label font-weight-bold">Repeat</label>
178207
<div>

0 commit comments

Comments
 (0)