Skip to content

Commit 5154048

Browse files
committed
Fix #200
1 parent 4e4dd72 commit 5154048

File tree

3 files changed

+65
-29
lines changed

3 files changed

+65
-29
lines changed

userdetails-cognito/src/main/groovy/au/org/ala/userdetails/CognitoApplicationService.groovy

+24-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import com.amazonaws.services.dynamodbv2.model.AttributeValue
1414
import com.amazonaws.services.dynamodbv2.model.DeleteItemRequest
1515
import com.amazonaws.services.dynamodbv2.model.PutItemRequest
1616
import com.amazonaws.services.dynamodbv2.model.QueryRequest
17+
import groovy.util.logging.Slf4j
1718

19+
@Slf4j
1820
class CognitoApplicationService implements IApplicationService {
1921

2022
IUserService userService
@@ -153,14 +155,20 @@ class CognitoApplicationService implements IApplicationService {
153155
request.callbackURLs.addAll(tokensCallbackURLs)
154156
}
155157

156-
CreateUserPoolClientResult response = cognitoIdp.createUserPoolClient(request)
158+
try {
159+
CreateUserPoolClientResult response = cognitoIdp.createUserPoolClient(request)
157160

158-
if (isSuccessful(response)) {
159-
def clientId = response.userPoolClient.clientId
160-
addClientIdForUser(userId, clientId)
161-
return userPoolClientToApplication(response.userPoolClient)
162-
} else {
163-
throw new RuntimeException("Could not generate client")
161+
if (isSuccessful(response)) {
162+
def clientId = response.userPoolClient.clientId
163+
addClientIdForUser(userId, clientId)
164+
return userPoolClientToApplication(response.userPoolClient)
165+
} else {
166+
throw new RuntimeException("Could not generate client")
167+
}
168+
}
169+
catch (Exception e) {
170+
log.error(e.getMessage(), e)
171+
throw new RuntimeException("Could not create client")
164172
}
165173
}
166174

@@ -200,9 +208,15 @@ class CognitoApplicationService implements IApplicationService {
200208
request.callbackURLs.addAll(tokensCallbackURLs)
201209
}
202210

203-
def response = cognitoIdp.updateUserPoolClient(request)
204-
if (!isSuccessful(response)) {
205-
throw new RuntimeException("Could not update client $applicationRecord.clientId")
211+
try {
212+
def response = cognitoIdp.updateUserPoolClient(request)
213+
if (!isSuccessful(response)) {
214+
throw new RuntimeException("Could not update client $applicationRecord.clientId")
215+
}
216+
}
217+
catch (Exception e) {
218+
log.error(e.getMessage(), e)
219+
throw new RuntimeException("Could not update client")
206220
}
207221
}
208222

userdetails-plugin/grails-app/views/profile/_applicationForm.gsp

+30-16
Original file line numberDiff line numberDiff line change
@@ -118,24 +118,38 @@
118118
function addCallback() {
119119
let $callback = $('#callbacks');
120120

121-
if (!$callback[0].checkValidity()) {
122-
alert('not a valid url');
121+
if (isValidUrl($callback[0].value)) {
122+
let value = $callback.val();
123+
$callback.val('');
124+
125+
let $callbacks = $('#callback-list');
126+
let length = $callbacks.children('input').length;
127+
128+
let span = $('<span></span>', {class: 'tag label label-default', 'data-index': length});
129+
let innerSpan = $('<span></span>', {text: value});
130+
let button = $('<a></a>', {'data-index': length, role: 'button', class: 'btn btn-danger delete'}).append('<i class="fa fa-trash"></i>');
131+
let input = $('<input></input>', {value: value, 'data-index': length, type: 'hidden', name: 'callbacks'});
132+
133+
span.append(innerSpan);
134+
span.append(button);
135+
$callbacks.append(span);
136+
$callbacks.append(input);
123137
}
124-
let value = $callback.val();
125-
$callback.val('');
126-
127-
let $callbacks = $('#callback-list');
128-
let length = $callbacks.children('input').length;
129-
130-
let span = $('<span></span>', {class: 'tag label label-default', 'data-index': length});
131-
let innerSpan = $('<span></span>', {text: value});
132-
let button = $('<a></a>', {'data-index': length, role: 'button', class: 'btn btn-danger delete'}).append('<i class="fa fa-trash"></i>');
133-
let input = $('<input></input>', {value: value, 'data-index': length, type: 'hidden', name: 'callbacks'});
138+
}
134139

135-
span.append(innerSpan);
136-
span.append(button);
137-
$callbacks.append(span);
138-
$callbacks.append(input);
140+
function isValidUrl(string) {
141+
try {
142+
const newUrl = new URL(string);
143+
let ifCognito = "${grailsApplication.config.getProperty('userdetails.cognito.auth', boolean, false)}"
144+
if (ifCognito && newUrl.protocol === 'http:' && newUrl.hostname !== 'localhost') {
145+
alert('Not a valid http url. HTTPS is required over HTTP, except for http://localhost. Additionally, app callback URLs like myapp://example are supported.');
146+
return false;
147+
}
148+
return true;
149+
} catch (err) {
150+
alert('Not a valid url.');
151+
return false;
152+
}
139153
}
140154

141155
function removeCallback(i) {

userdetails-plugin/grails-app/views/profile/applications.gsp

+11-3
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@
247247
addCallbackToForm($callbacks, callbacks, i, true);
248248
}
249249

250-
$('input.callbacks').val('');
250+
$('#callbacks').val('');
251251

252252
let url = '<g:createLink controller="profile" action="updateClient" id="clientId"/>'.replace('clientId', data.clientId);
253253
$('#modal-save-form').validationEngine('attach', { scroll: false });
@@ -260,13 +260,16 @@
260260
let $saveButtonContent = $saveButton.content;
261261
$saveButton.html('<i class="fa fa-spinner"></i>');
262262
setModalButtonsDisabled(true);
263+
$('#callbacks').val('');
263264
$.post(
264265
url,
265266
$(this).serialize()
266267
).done(function(data) {
267268
refreshAppTable();
268269
$('#client-modal').modal('hide');
269-
}).always(function() {
270+
}).fail(function(data) {
271+
alert( "Error when updating the application ");
272+
}).always(function() {
270273
$saveButton.html($saveButtonContent);
271274
setModalButtonsDisabled(false);
272275
});
@@ -292,6 +295,8 @@
292295
$callbacks.children().remove();
293296
addCallbackToForm($callbacks, ["http://localhost:8080/callback"], 0, false);
294297

298+
$('#callbacks').val('');
299+
295300
let url = '<g:createLink controller="profile" action="generateClient" />';
296301
$('#modal-save-form').validationEngine('attach', { scroll: false });
297302
$("#modal-save-form").off('submit').on('submit', function (e) {
@@ -303,6 +308,7 @@
303308
let $saveButtonContent = $saveButton.content;
304309
$saveButton.html('<i class="fa fa-spinner"></i>');
305310
setModalButtonsDisabled(true);
311+
$('#callbacks').val('');
306312
$.post(
307313
url,
308314
$(this).serialize()
@@ -314,7 +320,9 @@
314320
showEditModal(data);
315321
refreshAppTable();
316322
}
317-
}).always(function() {
323+
}).fail(function(data) {
324+
alert( "Error when creating the application");
325+
}).always(function() {
318326
$saveButton.html($saveButtonContent);
319327
setModalButtonsDisabled(false);
320328
});

0 commit comments

Comments
 (0)