MOBILE-4108 core: Don't call autologin WS for external URLs

main
Dani Palou 2022-07-01 15:42:35 +02:00
parent d3a8a30142
commit 290b78c8ab
17 changed files with 51 additions and 71 deletions

View File

@ -89,7 +89,7 @@ export class AddonBlockRecentlyAccessedItemsComponent extends CoreBlockBaseCompo
try { try {
const treated = await CoreContentLinksHelper.handleLink(url); const treated = await CoreContentLinksHelper.handleLink(url);
if (!treated) { if (!treated) {
return CoreSites.getCurrentSite()?.openInBrowserWithAutoLoginIfSameSite(url); return CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(url);
} }
} finally { } finally {
modal.dismiss(); modal.dismiss();

View File

@ -149,7 +149,7 @@ export class AddonBlockTimelineEventsComponent implements OnChanges {
try { try {
const treated = await CoreContentLinksHelper.handleLink(url); const treated = await CoreContentLinksHelper.handleLink(url);
if (!treated) { if (!treated) {
return CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLoginIfSameSite(url); return CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLogin(url);
} }
} finally { } finally {
modal.dismiss(); modal.dismiss();

View File

@ -418,7 +418,7 @@ export class AddonModFeedbackFormPage implements OnInit, OnDestroy, CanLeave {
const treated = await CoreContentLinksHelper.handleLink(this.siteAfterSubmit); const treated = await CoreContentLinksHelper.handleLink(this.siteAfterSubmit);
if (!treated) { if (!treated) {
await this.currentSite.openInBrowserWithAutoLoginIfSameSite(this.siteAfterSubmit); await this.currentSite.openInBrowserWithAutoLogin(this.siteAfterSubmit);
} }
} finally { } finally {
modal.dismiss(); modal.dismiss();

View File

@ -221,7 +221,7 @@ export class AddonModResourceIndexComponent extends CoreCourseModuleMainResource
} }
// The resource cannot be downloaded, open the activity in browser. // The resource cannot be downloaded, open the activity in browser.
await CoreSites.getCurrentSite()?.openInBrowserWithAutoLoginIfSameSite(this.module.url || ''); await CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(this.module.url || '');
} }
/** /**

View File

@ -151,9 +151,9 @@ export class AddonModUrlIndexComponent extends CoreCourseModuleMainResourceCompo
// Will be displayed in an iframe. Check if we need to auto-login. // Will be displayed in an iframe. Check if we need to auto-login.
const currentSite = CoreSites.getCurrentSite(); const currentSite = CoreSites.getCurrentSite();
if (currentSite?.containsUrl(this.url)) { if (currentSite && this.url) {
// Format the URL to add auto-login. // Format the URL to add auto-login if needed.
this.url = await currentSite.getAutoLoginUrl(this.url!, false); this.url = await currentSite.getAutoLoginUrl(this.url, false);
} }
} }
} }

View File

@ -36,7 +36,7 @@ export class AddonModUrlHelperProvider {
const treated = await CoreContentLinksHelper.handleLink(url, undefined, true, true); const treated = await CoreContentLinksHelper.handleLink(url, undefined, true, true);
if (!treated) { if (!treated) {
await CoreSites.getCurrentSite()?.openInBrowserWithAutoLoginIfSameSite(url); await CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(url);
} }
} finally { } finally {
modal.dismiss(); modal.dismiss();

View File

@ -80,7 +80,7 @@ export class AddonReportInsightsActionLinkHandlerService extends CoreContentLink
// Cannot be opened in the app, open in browser. // Cannot be opened in the app, open in browser.
const site = await CoreSites.getSite(siteId); const site = await CoreSites.getSite(siteId);
await site.openInBrowserWithAutoLoginIfSameSite(forwardUrl); await site.openInBrowserWithAutoLogin(forwardUrl);
} }
} }
}, },

View File

@ -1533,13 +1533,14 @@ export class CoreSite {
* @param alertMessage If defined, an alert will be shown before opening the browser. * @param alertMessage If defined, an alert will be shown before opening the browser.
* @param options Other options. * @param options Other options.
* @return Promise resolved when done, rejected otherwise. * @return Promise resolved when done, rejected otherwise.
* @deprecated since 4.1. Use openInBrowserWithAutoLogin instead, now it always checks that URL belongs to same site.
*/ */
async openInBrowserWithAutoLoginIfSameSite( async openInBrowserWithAutoLoginIfSameSite(
url: string, url: string,
alertMessage?: string, alertMessage?: string,
options: CoreUtilsOpenInBrowserOptions = {}, options: CoreUtilsOpenInBrowserOptions = {},
): Promise<void> { ): Promise<void> {
await this.openWithAutoLoginIfSameSite(false, url, options, alertMessage); return this.openInBrowserWithAutoLogin(url, alertMessage, options);
} }
/** /**
@ -1563,15 +1564,14 @@ export class CoreSite {
* @param options Override default options passed to inappbrowser. * @param options Override default options passed to inappbrowser.
* @param alertMessage If defined, an alert will be shown before opening the inappbrowser. * @param alertMessage If defined, an alert will be shown before opening the inappbrowser.
* @return Promise resolved when done. * @return Promise resolved when done.
* @deprecated since 4.1. Use openInAppWithAutoLogin instead, now it always checks that URL belongs to same site.
*/ */
async openInAppWithAutoLoginIfSameSite( async openInAppWithAutoLoginIfSameSite(
url: string, url: string,
options?: InAppBrowserOptions, options?: InAppBrowserOptions,
alertMessage?: string, alertMessage?: string,
): Promise<InAppBrowserObject> { ): Promise<InAppBrowserObject> {
const iabInstance = <InAppBrowserObject> await this.openWithAutoLoginIfSameSite(true, url, options, alertMessage); return this.openInAppWithAutoLogin(url, options, alertMessage);
return iabInstance;
} }
/** /**
@ -1623,6 +1623,7 @@ export class CoreSite {
* @param options Override default options passed to inappbrowser. * @param options Override default options passed to inappbrowser.
* @param alertMessage If defined, an alert will be shown before opening the browser/inappbrowser. * @param alertMessage If defined, an alert will be shown before opening the browser/inappbrowser.
* @return Promise resolved when done. Resolve param is returned only if inApp=true. * @return Promise resolved when done. Resolve param is returned only if inApp=true.
* @deprecated since 4.1. Use openWithAutoLogin instead, now it always checks that URL belongs to same site.
*/ */
async openWithAutoLoginIfSameSite( async openWithAutoLoginIfSameSite(
inApp: boolean, inApp: boolean,
@ -1630,15 +1631,7 @@ export class CoreSite {
options: InAppBrowserOptions & CoreUtilsOpenInBrowserOptions = {}, options: InAppBrowserOptions & CoreUtilsOpenInBrowserOptions = {},
alertMessage?: string, alertMessage?: string,
): Promise<InAppBrowserObject | void> { ): Promise<InAppBrowserObject | void> {
if (this.containsUrl(url)) { return this.openWithAutoLogin(inApp, url, options, alertMessage);
return this.openWithAutoLogin(inApp, url, options, alertMessage);
} else {
if (inApp) {
return Promise.resolve(CoreUtils.openInApp(url, options));
} else {
CoreUtils.openInBrowser(url, options);
}
}
} }
/** /**
@ -1822,6 +1815,11 @@ export class CoreSite {
return url; return url;
} }
if (!this.containsUrl(url)) {
// URL doesn't belong to the site, don't auto login.
return url;
}
if (this.lastAutoLogin > 0) { if (this.lastAutoLogin > 0) {
const timeBetweenRequests = await CoreUtils.ignoreErrors( const timeBetweenRequests = await CoreUtils.ignoreErrors(
this.getConfig('tool_mobile_autologinmintimebetweenreq'), this.getConfig('tool_mobile_autologinmintimebetweenreq'),

View File

@ -45,7 +45,7 @@ export class CoreContextMenuItemComponent implements OnInit, OnDestroy, OnChange
@Input() ariaAction?: string; // Aria label to add to iconAction. If not set, it will be equal to content. @Input() ariaAction?: string; // Aria label to add to iconAction. If not set, it will be equal to content.
@Input() href?: string; // Link to go if no action provided. @Input() href?: string; // Link to go if no action provided.
@Input() captureLink?: boolean | string; // Whether the link needs to be captured by the app. @Input() captureLink?: boolean | string; // Whether the link needs to be captured by the app.
@Input() autoLogin?: string; // Whether the link needs to be opened using auto-login. @Input() autoLogin: boolean | string = true; // Whether the link needs to be opened using auto-login.
@Input() closeOnClick = true; // Whether to close the popover when the item is clicked. @Input() closeOnClick = true; // Whether to close the popover when the item is clicked.
@Input() priority?: number; // Used to sort items. The highest priority, the highest position. @Input() priority?: number; // Used to sort items. The highest priority, the highest position.
@Input() badge?: string; // A badge to show in the item. @Input() badge?: string; // A badge to show in the item.
@ -88,7 +88,6 @@ export class CoreContextMenuItemComponent implements OnInit, OnDestroy, OnChange
// Navigation help if href provided. // Navigation help if href provided.
this.captureLink = this.href && this.captureLink ? this.captureLink : false; this.captureLink = this.href && this.captureLink ? this.captureLink : false;
this.autoLogin = this.autoLogin || 'check';
if (!this.destroyed) { if (!this.destroyed) {
this.ctxtMenu.addItem(this); this.ctxtMenu.addItem(this);

View File

@ -601,7 +601,7 @@ export class CoreFormatTextDirective implements OnChanges, OnDestroy, AsyncCompo
} }
if (openInApp) { if (openInApp) {
site.openInAppWithAutoLoginIfSameSite(url); site.openInAppWithAutoLogin(url);
if (refreshOnResume && this.refreshContext) { if (refreshOnResume && this.refreshContext) {
// Refresh the context when the IAB is closed. // Refresh the context when the IAB is closed.
@ -610,7 +610,7 @@ export class CoreFormatTextDirective implements OnChanges, OnDestroy, AsyncCompo
}); });
} }
} else { } else {
site.openInBrowserWithAutoLoginIfSameSite(url, undefined, { site.openInBrowserWithAutoLogin(url, undefined, {
showBrowserWarning: !confirmMessage, showBrowserWarning: !confirmMessage,
}); });

View File

@ -41,11 +41,7 @@ export class CoreLinkDirective implements OnInit {
@Input() href?: string | SafeUrl; // Link URL. @Input() href?: string | SafeUrl; // Link URL.
@Input() capture?: boolean | string; // If the link needs to be captured by the app. @Input() capture?: boolean | string; // If the link needs to be captured by the app.
@Input() inApp?: boolean | string; // True to open in embedded browser, false to open in system browser. @Input() inApp?: boolean | string; // True to open in embedded browser, false to open in system browser.
/* Whether the link should be opened with auto-login. Accepts the following values: @Input() autoLogin: boolean | string = true; // Whether to try to use auto-login. Values yes/no/check are deprecated.
"yes" -> Always auto-login.
"no" -> Never auto-login.
"check" -> Auto-login only if it points to the current site. Default value. */
@Input() autoLogin = 'check';
@Input() showBrowserWarning = true; // Whether to show a warning before opening browser. Defaults to true. @Input() showBrowserWarning = true; // Whether to show a warning before opening browser. Defaults to true.
protected element: HTMLElement; protected element: HTMLElement;
@ -61,8 +57,6 @@ export class CoreLinkDirective implements OnInit {
* Function executed when the component is initialized. * Function executed when the component is initialized.
*/ */
ngOnInit(): void { ngOnInit(): void {
this.inApp = this.inApp === undefined ? this.inApp : CoreUtils.isTrueOrOne(this.inApp);
if (this.element.tagName != 'BUTTON' && this.element.tagName != 'A') { if (this.element.tagName != 'BUTTON' && this.element.tagName != 'A') {
this.element.setAttribute('tabindex', '0'); this.element.setAttribute('tabindex', '0');
this.element.setAttribute('role', 'button'); this.element.setAttribute('role', 'button');
@ -184,10 +178,16 @@ export class CoreLinkDirective implements OnInit {
* @return Promise resolved when done. * @return Promise resolved when done.
*/ */
protected async openExternalLink(href: string, openIn?: string | null): Promise<void> { protected async openExternalLink(href: string, openIn?: string | null): Promise<void> {
// It's an external link, we will open with browser. Check if we need to auto-login. // Priority order is: core-link inApp attribute > forceOpenLinksIn setting > data-open-in HTML attribute.
const openInApp = this.inApp !== undefined ?
CoreUtils.isTrueOrOne(this.inApp) :
(CoreConstants.CONFIG.forceOpenLinksIn !== 'browser' &&
(CoreConstants.CONFIG.forceOpenLinksIn === 'app' || openIn === 'app'));
// Check if we need to auto-login.
if (!CoreSites.isLoggedIn()) { if (!CoreSites.isLoggedIn()) {
// Not logged in, cannot auto-login. // Not logged in, cannot auto-login.
if (this.inApp) { if (openInApp) {
CoreUtils.openInApp(href); CoreUtils.openInApp(href);
} else { } else {
CoreUtils.openInBrowser(href, { showBrowserWarning: this.showBrowserWarning }); CoreUtils.openInBrowser(href, { showBrowserWarning: this.showBrowserWarning });
@ -221,38 +221,22 @@ export class CoreLinkDirective implements OnInit {
} }
} }
if (this.autoLogin == 'yes') { const autoLogin = typeof this.autoLogin === 'boolean' ?
if (this.inApp) { this.autoLogin :
!CoreUtils.isFalseOrZero(this.autoLogin) && this.autoLogin !== 'no'; // Support deprecated values yes/no/check.
if (autoLogin) {
if (openInApp) {
await currentSite.openInAppWithAutoLogin(href); await currentSite.openInAppWithAutoLogin(href);
} else { } else {
await currentSite.openInBrowserWithAutoLogin(href, undefined, { showBrowserWarning: this.showBrowserWarning }); await currentSite.openInBrowserWithAutoLogin(href, undefined, { showBrowserWarning: this.showBrowserWarning });
} }
} else if (this.autoLogin == 'no') { } else {
if (this.inApp) { if (openInApp) {
CoreUtils.openInApp(href); CoreUtils.openInApp(href);
} else { } else {
CoreUtils.openInBrowser(href, { showBrowserWarning: this.showBrowserWarning }); CoreUtils.openInBrowser(href, { showBrowserWarning: this.showBrowserWarning });
} }
} else {
// Priority order is: core-link inApp attribute > forceOpenLinksIn setting > data-open-in HTML attribute.
let openInApp = this.inApp;
if (this.inApp === undefined) {
if (CoreConstants.CONFIG.forceOpenLinksIn == 'browser') {
openInApp = false;
} else if (CoreConstants.CONFIG.forceOpenLinksIn == 'app' || openIn == 'app') {
openInApp = true;
}
}
if (openInApp) {
await currentSite.openInAppWithAutoLoginIfSameSite(href);
} else {
await currentSite.openInBrowserWithAutoLoginIfSameSite(
href,
undefined,
{ showBrowserWarning: this.showBrowserWarning },
);
}
} }
} }

View File

@ -67,7 +67,7 @@ export class CoreCourseModuleDefaultHandler implements CoreCourseModuleHandler {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLoginIfSameSite(url); CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLogin(url);
}, },
}]; }];
} }

View File

@ -129,7 +129,7 @@ export class CoreH5PIframeComponent implements OnChanges, OnDestroy {
CoreH5PCore.DISPLAY_OPTION_DOWNLOAD + '=0', CoreH5PCore.DISPLAY_OPTION_DOWNLOAD + '=0',
); );
// Get auto-login URL so the user is automatically authenticated. // Get auto-login URL so the user is automatically authenticated if needed.
const url = await this.site.getAutoLoginUrl(src, false); const url = await this.site.getAutoLoginUrl(src, false);
// Add the preventredirect param so the user can authenticate. // Add the preventredirect param so the user can authenticate.

View File

@ -166,7 +166,7 @@ export class CoreMainMenuMorePage implements OnInit, OnDestroy {
if (!treated) { if (!treated) {
// Can't handle it, open it in browser. // Can't handle it, open it in browser.
CoreSites.getCurrentSite()?.openInBrowserWithAutoLoginIfSameSite(text); CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(text);
} }
} else { } else {
// It's not a URL, open it in a modal so the user can see it and copy it. // It's not a URL, open it in a modal so the user can see it and copy it.

View File

@ -28,17 +28,16 @@ export class CoreViewerIframePage implements OnInit {
title?: string; // Page title. title?: string; // Page title.
url?: string; // Iframe URL. url?: string; // Iframe URL.
/* Whether the URL should be open with auto-login. Accepts the following values: autoLogin?: boolean; // Whether to try to use auto-login.
"yes" -> Always auto-login.
"no" -> Never auto-login.
"check" -> Auto-login only if it points to the current site. Default value. */
autoLogin?: string;
finalUrl?: string; finalUrl?: string;
async ngOnInit(): Promise<void> { async ngOnInit(): Promise<void> {
this.title = CoreNavigator.getRouteParam('title'); this.title = CoreNavigator.getRouteParam('title');
this.url = CoreNavigator.getRouteParam('url'); this.url = CoreNavigator.getRouteParam('url');
this.autoLogin = CoreNavigator.getRouteParam('autoLogin') || 'check'; const autoLoginParam = CoreNavigator.getRouteParam('autoLogin') ?? true;
this.autoLogin = typeof autoLoginParam === 'boolean' ?
autoLoginParam :
autoLoginParam !== 'no'; // Support deprecated values yes/no/check.
if (!this.url) { if (!this.url) {
return; return;
@ -46,7 +45,7 @@ export class CoreViewerIframePage implements OnInit {
const currentSite = CoreSites.getCurrentSite(); const currentSite = CoreSites.getCurrentSite();
if (currentSite && (this.autoLogin == 'yes' || (this.autoLogin == 'check' && currentSite.containsUrl(this.url)))) { if (currentSite && this.autoLogin) {
// Format the URL to add auto-login. // Format the URL to add auto-login.
this.finalUrl = await currentSite.getAutoLoginUrl(this.url, false); this.finalUrl = await currentSite.getAutoLoginUrl(this.url, false);
} else { } else {

View File

@ -510,7 +510,7 @@ export class CoreIframeUtilsProvider {
if (!CoreSites.isLoggedIn()) { if (!CoreSites.isLoggedIn()) {
CoreUtils.openInBrowser(link.href); CoreUtils.openInBrowser(link.href);
} else { } else {
await CoreSites.getCurrentSite()!.openInBrowserWithAutoLoginIfSameSite(link.href); await CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(link.href);
} }
} else if (link.target == '_parent' || link.target == '_top' || link.target == '_blank') { } else if (link.target == '_parent' || link.target == '_top' || link.target == '_blank') {
// Opening links with _parent, _top or _blank can break the app. We'll open it in InAppBrowser. // Opening links with _parent, _top or _blank can break the app. We'll open it in InAppBrowser.

View File

@ -112,7 +112,7 @@ export class CoreWindow {
// Not logged in, cannot auto-login. // Not logged in, cannot auto-login.
CoreUtils.openInBrowser(url); CoreUtils.openInBrowser(url);
} else { } else {
await CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLoginIfSameSite(url); await CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLogin(url);
} }
} }
} }