From f6125a2542104d273dfe521edbae5e70affe6bff Mon Sep 17 00:00:00 2001 From: Lorent Lempereur Date: Mon, 7 May 2018 00:26:46 +0200 Subject: [PATCH] [KEYCLOAK-6655] Javascript Adapter - Allow users to provide cordova-specific options to login and register (#4998) * Javascript Adapter - Allow users to pass cordova-specific options (in-app-browser) to the login and register functions * Javascript Adapter - Allow users to pass cordova-specific options (in-app-browser) to the login and register functions * [KEYCLOAK-6655] On Android 8, explicit hidden=no fails on in-app-browser load. --- .../oidc/js/src/main/resources/keycloak.js | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/adapters/oidc/js/src/main/resources/keycloak.js b/adapters/oidc/js/src/main/resources/keycloak.js index 271cd500f1..23532116bf 100755 --- a/adapters/oidc/js/src/main/resources/keycloak.js +++ b/adapters/oidc/js/src/main/resources/keycloak.js @@ -1187,17 +1187,41 @@ return window.open(loginUrl, target, options); } }; + + var shallowCloneCordovaOptions = function (userOptions) { + if (userOptions && userOptions.cordovaOptions) { + return Object.keys(userOptions.cordovaOptions).reduce(function (options, optionName) { + options[optionName] = userOptions.cordovaOptions[optionName]; + return options; + }, {}); + } else { + return {}; + } + }; + + var formatCordovaOptions = function (cordovaOptions) { + return Object.keys(cordovaOptions).reduce(function (options, optionName) { + options.push(optionName+"="+cordovaOptions[optionName]); + return options; + }, []).join(","); + }; + + var createCordovaOptions = function (userOptions) { + var cordovaOptions = shallowCloneCordovaOptions(userOptions); + cordovaOptions.location = 'no'; + if (userOptions && userOptions.prompt == 'none') { + cordovaOptions.hidden = 'yes'; + } + return formatCordovaOptions(cordovaOptions); + }; + return { login: function(options) { var promise = createPromise(); - var o = 'location=no'; - if (options && options.prompt == 'none') { - o += ',hidden=yes'; - } - + var cordovaOptions = createCordovaOptions(options); var loginUrl = kc.createLoginUrl(options); - var ref = cordovaOpenWindowWrapper(loginUrl, '_blank', o); + var ref = cordovaOpenWindowWrapper(loginUrl, '_blank', cordovaOptions); var completed = false; ref.addEventListener('loadstart', function(event) { @@ -1228,7 +1252,7 @@ logout: function(options) { var promise = createPromise(); - + var logoutUrl = kc.createLogoutUrl(options); var ref = cordovaOpenWindowWrapper(logoutUrl, '_blank', 'location=no,hidden=yes'); @@ -1263,7 +1287,8 @@ register : function() { var registerUrl = kc.createRegisterUrl(); - var ref = cordovaOpenWindowWrapper(registerUrl, '_blank', 'location=no'); + var cordovaOptions = createCordovaOptions(options); + var ref = cordovaOpenWindowWrapper(registerUrl, '_blank', cordovaOptions); ref.addEventListener('loadstart', function(event) { if (event.url.indexOf('http://localhost') == 0) { ref.close();