Skip to content

Commit 18f2f0b

Browse files
author
Albert Gasset
committed
MOBILE-1545: Support to log in via SSO with an embedded browser
1 parent 00b03ea commit 18f2f0b

File tree

6 files changed

+59
-17
lines changed

6 files changed

+59
-17
lines changed

www/core/components/contentlinks/services/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ angular.module('mm.core.contentlinks')
208208

209209
return promise.then(function() {
210210
if (ssoNeeded) {
211-
$mmLoginHelper.openBrowserForSSOLogin(result.siteurl);
211+
$mmLoginHelper.openBrowserForSSOLogin(result.siteurl, result.code);
212212
} else {
213213
$state.go('mm_login.credentials', {
214214
siteurl: result.siteurl,

www/core/components/login/controllers/credentials.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ angular.module('mm.core.login')
5252
// Check that there's no SSO authentication ongoing and the view hasn't changed.
5353
if (!$mmLoginHelper.isSSOLoginOngoing() && !$scope.$$destroyed) {
5454
$mmUtil.showConfirm($translate('mm.login.logininsiterequired')).then(function() {
55-
$mmLoginHelper.openBrowserForSSOLogin(result.siteurl);
55+
$mmLoginHelper.openBrowserForSSOLogin(result.siteurl, result.code);
5656
});
5757
}
5858
} else {

www/core/components/login/controllers/site.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ angular.module('mm.core.login')
6565
if ($mmLoginHelper.isSSOLoginNeeded(result.code)) {
6666
// SSO. User needs to authenticate in a browser.
6767
$mmUtil.showConfirm($translate('mm.login.logininsiterequired')).then(function() {
68-
$mmLoginHelper.openBrowserForSSOLogin(result.siteurl);
68+
$mmLoginHelper.openBrowserForSSOLogin(result.siteurl, result.code);
6969
});
7070
} else {
7171
$state.go('mm_login.credentials', {siteurl: result.siteurl});

www/core/components/login/main.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ angular.module('mm.core.login', [])
104104
// Register observer to check if the app was launched via URL scheme.
105105
$mmURLDelegate.register('mmLoginSSO', appLaunchedByURL);
106106

107+
// Observe loaded pages in the InAppBrowser to handle SSO URLs.
108+
$rootScope.$on('$cordovaInAppBrowser:loadstart', function(e, event) {
109+
// URLs with a custom scheme are prefixed with "http://", we need to remove this.
110+
var url = event.url.replace(/^http:\/\//, '');
111+
if (appLaunchedByURL(url)) {
112+
// Close the browser if it's a valid SSO URL.
113+
$mmUtil.closeInAppBrowser();
114+
}
115+
});
116+
107117
// Redirect depending on user session.
108118
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
109119

@@ -162,7 +172,7 @@ angular.module('mm.core.login', [])
162172
if ($mmLoginHelper.isSSOLoginNeeded(result.code)) {
163173
// SSO. User needs to authenticate in a browser.
164174
$mmUtil.showConfirm($translate('mm.login.reconnectssodescription')).then(function() {
165-
$mmLoginHelper.openBrowserForSSOLogin(result.siteurl);
175+
$mmLoginHelper.openBrowserForSSOLogin(result.siteurl, result.code);
166176
});
167177
} else {
168178
var info = $mmSite.getInfo();

www/core/components/login/services/helper.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
angular.module('mm.core.login')
1616

1717
.constant('mmLoginSSOCode', 2) // This code is returned by local_mobile Moodle plugin if SSO in browser is required.
18+
.constant('mmLoginSSOInAppCode', 3)
1819
.constant('mmLoginLaunchSiteURL', 'mmLoginLaunchSiteURL')
1920
.constant('mmLoginLaunchPassport', 'mmLoginLaunchPassport')
2021

@@ -25,8 +26,9 @@ angular.module('mm.core.login')
2526
* @ngdoc service
2627
* @name $mmLoginHelper
2728
*/
28-
.factory('$mmLoginHelper', function($q, $log, $mmConfig, mmLoginSSOCode, mmLoginLaunchSiteURL, mmLoginLaunchPassport,
29-
md5, $mmSite, $mmSitesManager, $mmLang, $mmUtil, $state, $mmAddonManager, mmCoreConfigConstants) {
29+
.factory('$mmLoginHelper', function($q, $log, $mmConfig, mmLoginSSOCode, mmLoginSSOInAppCode, mmLoginLaunchSiteURL,
30+
mmLoginLaunchPassport, md5, $mmSite, $mmSitesManager, $mmLang, $mmUtil, $state, $mmAddonManager,
31+
$translate, mmCoreConfigConstants) {
3032

3133
$log = $log.getInstance('$mmLoginHelper');
3234

@@ -93,7 +95,7 @@ angular.module('mm.core.login')
9395
* @return {Boolean} True if SSO login is needed, false othwerise.
9496
*/
9597
self.isSSOLoginNeeded = function(code) {
96-
return code == mmLoginSSOCode;
98+
return code == mmLoginSSOCode || code == mmLoginSSOInAppCode;
9799
};
98100

99101
/**
@@ -116,8 +118,9 @@ angular.module('mm.core.login')
116118
* @ngdoc method
117119
* @name $mmLoginHelper#openBrowserForSSOLogin
118120
* @param {String} siteurl URL of the site where the SSO login will be performed.
121+
* @param {Number} typeOfLogin mmLoginSSOCode or mmLoginSSOInAppCode
119122
*/
120-
self.openBrowserForSSOLogin = function(siteurl) {
123+
self.openBrowserForSSOLogin = function(siteurl, typeOfLogin) {
121124
var passport = Math.random() * 1000;
122125
var loginurl = siteurl + "/local/mobile/launch.php?service=" + mmCoreConfigConstants.wsextservice;
123126
loginurl += "&passport=" + passport;
@@ -128,9 +131,19 @@ angular.module('mm.core.login')
128131
$mmConfig.set(mmLoginLaunchSiteURL, siteurl);
129132
$mmConfig.set(mmLoginLaunchPassport, passport);
130133

131-
$mmUtil.openInBrowser(loginurl);
132-
if (navigator.app) {
133-
navigator.app.exitApp();
134+
if (typeOfLogin == mmLoginSSOInAppCode) {
135+
$translate('mm.login.cancel').then(function(cancelStr) {
136+
var options = {
137+
clearsessioncache: 'yes', // Clear the session cache to allow for multiple logins.
138+
closebuttoncaption: cancelStr,
139+
};
140+
$mmUtil.openInApp(loginurl, options);
141+
});
142+
} else {
143+
$mmUtil.openInBrowser(loginurl);
144+
if (navigator.app) {
145+
navigator.app.exitApp();
146+
}
134147
}
135148
};
136149

www/core/lib/util.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ angular.module('mm.core')
6666
};
6767

6868
this.$get = function($ionicLoading, $ionicPopup, $injector, $translate, $http, $log, $q, $mmLang, $mmFS, $timeout, $mmApp,
69-
$mmText, mmCoreWifiDownloadThreshold, mmCoreDownloadThreshold, $ionicScrollDelegate) {
69+
$mmText, mmCoreWifiDownloadThreshold, mmCoreDownloadThreshold, $ionicScrollDelegate, $cordovaInAppBrowser) {
7070

7171
$log = $log.getInstance('$mmUtil');
7272

@@ -388,20 +388,39 @@ angular.module('mm.core')
388388
* @ngdoc method
389389
* @name $mmUtil#openInApp
390390
* @param {String} url The URL to open.
391+
* @param {Object} options Override default options passed to $cordovaInAppBrowser#open
391392
* @return {Void}
392393
*/
393-
self.openInApp = function(url) {
394+
self.openInApp = function(url, options) {
394395
if (!url) {
395396
return;
396397
}
397398

398-
var options = 'enableViewPortScale=yes'; // Enable zoom on iOS.
399-
if (ionic.Platform.isIOS() && url.indexOf('file://') === 0) {
399+
options = options || {};
400+
401+
if (!options.enableViewPortScale) {
402+
options.enableViewPortScale = 'yes'; // Enable zoom on iOS.
403+
}
404+
405+
if (!options.location && ionic.Platform.isIOS() && url.indexOf('file://') === 0) {
400406
// The URL uses file protocol, don't show it on iOS.
401407
// In Android we keep it because otherwise we lose the whole toolbar.
402-
options += ',location=no';
408+
options.location = 'no';
403409
}
404-
window.open(url, '_blank', options);
410+
411+
$cordovaInAppBrowser.open(url, '_blank', options);
412+
};
413+
414+
/**
415+
* Close the InAppBrowser window.
416+
*
417+
* @module mm.core
418+
* @ngdoc method
419+
* @name $mmUtil#closeInAppBrowser
420+
* @return {Void}
421+
*/
422+
self.closeInAppBrowser = function() {
423+
$cordovaInAppBrowser.close();
405424
};
406425

407426
/**

0 commit comments

Comments
 (0)