MOBILE-3860 login: Deprecate login/token.php checks

main
Dani Palou 2021-09-15 14:50:59 +02:00
parent 534bbbb289
commit 3109c93b64
3 changed files with 76 additions and 61 deletions

View File

@ -20,9 +20,12 @@ import { CoreError } from '@classes/errors/error';
export class CoreAjaxError extends CoreError { export class CoreAjaxError extends CoreError {
available = 1; // @deprecated since app 4.0. AJAX endpoint should always be available in supported Moodle versions. available = 1; // @deprecated since app 4.0. AJAX endpoint should always be available in supported Moodle versions.
status?: number;
constructor(message: string) { constructor(message: string, available?: number, status?: number) {
super(message); super(message);
this.status = status;
} }
} }

View File

@ -54,6 +54,8 @@ import { CoreSitesFactory } from './sites-factory';
import { CoreText } from '@singletons/text'; import { CoreText } from '@singletons/text';
import { CoreLoginHelper } from '@features/login/services/login-helper'; import { CoreLoginHelper } from '@features/login/services/login-helper';
import { CoreErrorWithTitle } from '@classes/errors/errorwithtitle'; import { CoreErrorWithTitle } from '@classes/errors/errorwithtitle';
import { CoreAjaxError } from '@classes/errors/ajaxerror';
import { CoreAjaxWSError } from '@classes/errors/ajaxwserror';
export const CORE_SITE_SCHEMAS = new InjectionToken<CoreSiteSchema[]>('CORE_SITE_SCHEMAS'); export const CORE_SITE_SCHEMAS = new InjectionToken<CoreSiteSchema[]>('CORE_SITE_SCHEMAS');
@ -183,33 +185,30 @@ export class CoreSitesProvider {
// Now, replace the siteUrl with the protocol. // Now, replace the siteUrl with the protocol.
siteUrl = siteUrl.replace(/^https?:\/\//i, protocol); siteUrl = siteUrl.replace(/^https?:\/\//i, protocol);
// Create a temporary site to fetch site info.
let temporarySite = CoreSitesFactory.makeSite(undefined, siteUrl);
let config: CoreSitePublicConfigResponse | undefined;
try { try {
await this.siteExists(siteUrl); config = await temporarySite.getPublicConfig();
} catch (error) { } catch (error) {
// Do not continue checking if WS are not enabled. const treatedError = await this.treatGetPublicConfigError(temporarySite.getURL(), error);
if (error.errorcode == 'enablewsdescription') { if (treatedError.critical) {
error.critical = true; throw treatedError; // App received a WS error, stop.
throw error;
} }
// Site doesn't exist. Try to add or remove 'www'. // Try to add or remove 'www'.
const treatedUrl = CoreUrlUtils.addOrRemoveWWW(siteUrl); temporarySite = CoreSitesFactory.makeSite(undefined, CoreUrlUtils.addOrRemoveWWW(siteUrl));
try { try {
await this.siteExists(treatedUrl); config = await temporarySite.getPublicConfig();
// Success, use this new URL as site url.
siteUrl = treatedUrl;
} catch (secondError) { } catch (secondError) {
// Do not continue checking if WS are not enabled. const treatedSecondError = await this.treatGetPublicConfigError(temporarySite.getURL(), secondError);
if (secondError.errorcode == 'enablewsdescription') { if (treatedSecondError.critical) {
secondError.critical = true; throw treatedSecondError; // App received a WS error, stop.
throw secondError;
} }
// Return the error. // App didn't receive a WS response, probably cannot connect. Prioritize first error if it's valid.
if (CoreTextUtils.getErrorMessageFromError(error)) { if (CoreTextUtils.getErrorMessageFromError(error)) {
throw error; throw error;
} else { } else {
@ -218,21 +217,16 @@ export class CoreSitesProvider {
} }
} }
// Site exists. Create a temporary site to fetch its info.
const temporarySite = CoreSitesFactory.makeSite(undefined, siteUrl);
let config: CoreSitePublicConfigResponse | undefined;
try {
config = await temporarySite.getPublicConfig();
// Check that the user can authenticate. // Check that the user can authenticate.
if (!config.enablewebservices) { if (!config.enablewebservices) {
throw new CoreSiteError({ throw new CoreSiteError({
message: Translate.instant('core.login.webservicesnotenabled'), message: Translate.instant('core.login.webservicesnotenabled'),
critical: true,
}); });
} else if (!config.enablemobilewebservice) { } else if (!config.enablemobilewebservice) {
throw new CoreSiteError({ throw new CoreSiteError({
message: Translate.instant('core.login.mobileservicesnotenabled'), message: Translate.instant('core.login.mobileservicesnotenabled'),
critical: true,
}); });
} else if (config.maintenanceenabled) { } else if (config.maintenanceenabled) {
let message = Translate.instant('core.sitemaintenance'); let message = Translate.instant('core.sitemaintenance');
@ -242,9 +236,31 @@ export class CoreSitesProvider {
throw new CoreSiteError({ throw new CoreSiteError({
message, message,
critical: true,
}); });
} }
} catch (error) {
siteUrl = temporarySite.getURL();
return { siteUrl, code: config?.typeoflogin || 0, service: CoreConstants.CONFIG.wsservice, config };
}
/**
* Treat an error returned by getPublicConfig in checkSiteWithProtocol. Converts the error to a CoreSiteError.
*
* @param siteUrl Site URL.
* @param error Error returned.
* @return Promise resolved with the treated error.
*/
protected async treatGetPublicConfigError(siteUrl: string, error: CoreAjaxError | CoreAjaxWSError): Promise<CoreSiteError> {
if (!('errorcode' in error)) {
// The WS didn't return data, probably cannot connect.
return new CoreSiteError({
message: error.message || '',
critical: 'status' in error && error.status === -2, // Certificate error.
});
}
// Service supported but an error happened. Return error. // Service supported but an error happened. Return error.
if (error.errorcode == 'codingerror') { if (error.errorcode == 'codingerror') {
// This could be caused by a redirect. Check if it's the case. // This could be caused by a redirect. Check if it's the case.
@ -258,23 +274,19 @@ export class CoreSitesProvider {
} }
} }
throw new CoreSiteError({ return new CoreSiteError({
message: error.message, message: error.message,
errorcode: error.errorcode, errorcode: error.errorcode,
critical: true, critical: true,
}); });
} }
siteUrl = temporarySite.getURL();
return { siteUrl, code: config?.typeoflogin || 0, service: CoreConstants.CONFIG.wsservice, config };
}
/** /**
* Check if a site exists. * Check if a site exists.
* *
* @param siteUrl URL of the site to check. * @param siteUrl URL of the site to check.
* @return A promise to be resolved if the site exists. * @return A promise to be resolved if the site exists.
* @deprecated since app 4.0. Now the app calls uses tool_mobile_get_public_config to check if site exists.
*/ */
async siteExists(siteUrl: string): Promise<void> { async siteExists(siteUrl: string): Promise<void> {
let data: CoreSitesLoginTokenResponse; let data: CoreSitesLoginTokenResponse;

View File

@ -491,7 +491,7 @@ export class CoreWSProvider {
message = Translate.instant('core.serverconnection'); message = Translate.instant('core.serverconnection');
} }
throw new CoreAjaxError(message); throw new CoreAjaxError(message, 1, data.status);
}); });
} }