From 28404f8b25aa9daf517f95cce1003edb547b7e22 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 16 Jan 2020 15:43:11 +0100 Subject: [PATCH] MOBILE-3286 login: Parse url domain if site wasn't found --- src/classes/utils/url.ts | 29 ++++++++++++++++ src/core/login/pages/site/site.ts | 55 ++++++++++++++++++++----------- 2 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 src/classes/utils/url.ts diff --git a/src/classes/utils/url.ts b/src/classes/utils/url.ts new file mode 100644 index 000000000..1fb990b81 --- /dev/null +++ b/src/classes/utils/url.ts @@ -0,0 +1,29 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +export class CoreUrl { + + /** + * Parse url domain. + * + * @param url Url. + * @return Url domain. + */ + static parseDomain(url: string): string | null { + const match = url.trim().match(/(https?:\/\/|^)([^/]+)/); + + return match ? match[2] : null; + } + +} diff --git a/src/core/login/pages/site/site.ts b/src/core/login/pages/site/site.ts index f9bc3a139..b1b7dbd9b 100644 --- a/src/core/login/pages/site/site.ts +++ b/src/core/login/pages/site/site.ts @@ -15,11 +15,12 @@ import { Component } from '@angular/core'; import { IonicPage, NavController, ModalController, NavParams } from 'ionic-angular'; import { CoreAppProvider } from '@providers/app'; -import { CoreSitesProvider } from '@providers/sites'; +import { CoreSitesProvider, CoreSiteCheckResponse } from '@providers/sites'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreConfigConstants } from '../../../../configconstants'; import { CoreLoginHelperProvider } from '../../providers/helper'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; +import { CoreUrl } from '@classes/utils/url'; /** * Page to enter or select the site URL to connect to. @@ -111,27 +112,22 @@ export class CoreLoginSitePage { } else { // Not a demo site. - this.sitesProvider.checkSite(url).then((result) => { - return this.sitesProvider.checkRequiredMinimumVersion(result.config).then(() => { - if (result.warning) { - this.domUtils.showErrorModal(result.warning, true, 4000); + this.sitesProvider.checkSite(url) + + // Attempt parsing the domain after initial check failed + .catch((error) => { + const domain = CoreUrl.parseDomain(url); + + if (!domain) { + throw error; } - if (this.loginHelper.isSSOLoginNeeded(result.code)) { - // SSO. User needs to authenticate in a browser. - this.loginHelper.confirmAndOpenBrowserForSSOLogin( - result.siteUrl, result.code, result.service, result.config && result.config.launchurl); - } else { - this.navCtrl.push('CoreLoginCredentialsPage', { siteUrl: result.siteUrl, siteConfig: result.config }); - } - }).catch(() => { - // Ignore errors. - }); - }, (error) => { - this.showLoginIssue(url, error); - }).finally(() => { - modal.dismiss(); - }); + return this.sitesProvider.checkSite(domain); + }) + + .then((result) => this.login(result)) + .catch((error) => this.showLoginIssue(url, error)) + .finally(() => modal.dismiss()); } } @@ -173,4 +169,23 @@ export class CoreLoginSitePage { modal.present(); } + + private async login(response: CoreSiteCheckResponse): Promise { + return this.sitesProvider.checkRequiredMinimumVersion(response.config).then(() => { + if (response.warning) { + this.domUtils.showErrorModal(response.warning, true, 4000); + } + + if (this.loginHelper.isSSOLoginNeeded(response.code)) { + // SSO. User needs to authenticate in a browser. + this.loginHelper.confirmAndOpenBrowserForSSOLogin( + response.siteUrl, response.code, response.service, response.config && response.config.launchurl); + } else { + this.navCtrl.push('CoreLoginCredentialsPage', { siteUrl: response.siteUrl, siteConfig: response.config }); + } + }).catch(() => { + // Ignore errors. + }); + } + }