From 9f46fe67d0c73485ac9208da7912c7c11ef7b773 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 12 Nov 2024 08:26:27 +0100 Subject: [PATCH] MOBILE-4679 dev: Display auto login info and part of tokens in dev page --- src/core/classes/sites/site.ts | 44 ++++++++++++++--- src/core/features/settings/pages/dev/dev.html | 47 +++++++++++++++++++ src/core/features/settings/pages/dev/dev.ts | 30 ++++++++++-- src/theme/theme.base.scss | 10 ++++ 4 files changed, 120 insertions(+), 11 deletions(-) diff --git a/src/core/classes/sites/site.ts b/src/core/classes/sites/site.ts index 58eb7ad718c..13863cc4120 100644 --- a/src/core/classes/sites/site.ts +++ b/src/core/classes/sites/site.ts @@ -378,7 +378,7 @@ export class CoreSite extends CoreAuthenticatedSite { */ fixPluginfileURL(url: string): string { const accessKey = this.tokenPluginFileWorks || this.tokenPluginFileWorks === undefined ? - this.infos && this.infos.userprivateaccesskey : undefined; + this.getFilesAccessKey() : undefined; return CoreUrl.fixPluginfileURL(url, this.token || '', this.siteUrl, accessKey); } @@ -685,12 +685,9 @@ export class CoreSite extends CoreAuthenticatedSite { } if (this.lastAutoLogin > 0) { - const timeBetweenRequests = await CoreUtils.ignoreErrors( - this.getConfig('tool_mobile_autologinmintimebetweenreq'), - CoreConstants.SECONDS_MINUTE * 6, - ); + const timeBetweenRequests = await this.getAutoLoginMinTimeBetweenRequests(); - if (CoreTimeUtils.timestamp() - this.lastAutoLogin < Number(timeBetweenRequests)) { + if (CoreTimeUtils.timestamp() - this.lastAutoLogin < timeBetweenRequests) { // Not enough time has passed since last auto login. return url; } @@ -775,7 +772,7 @@ export class CoreSite extends CoreAuthenticatedSite { * @returns Promise resolved with boolean: whether it works or not. */ checkTokenPluginFile(url: string): Promise { - if (!CoreUrl.canUseTokenPluginFile(url, this.siteUrl, this.infos && this.infos.userprivateaccesskey)) { + if (!CoreUrl.canUseTokenPluginFile(url, this.siteUrl, this.getFilesAccessKey())) { // Cannot use tokenpluginfile. return Promise.resolve(false); } else if (this.tokenPluginFileWorks !== undefined) { @@ -872,6 +869,39 @@ export class CoreSite extends CoreAuthenticatedSite { }); } + /** + * Get the access key to use to fetch files. + * + * @returns Access key. + */ + getFilesAccessKey(): string | undefined { + return this.infos?.userprivateaccesskey; + } + + /** + * Get auto-login time between requests. + * + * @returns Time between requests. + */ + async getAutoLoginMinTimeBetweenRequests(): Promise { + const timeBetweenRequests = await CoreUtils.ignoreErrors( + this.getConfig('tool_mobile_autologinmintimebetweenreq'), + CoreConstants.SECONDS_MINUTE * 6, + ); + + return Number(timeBetweenRequests); + } + + /** + * Get last auto login time. + * This time is stored in memory, so restarting the app will reset it. + * + * @returns Last auto login time. + */ + getLastAutoLoginTime(): number { + return this.lastAutoLogin; + } + } /** diff --git a/src/core/features/settings/pages/dev/dev.html b/src/core/features/settings/pages/dev/dev.html index 4a82ee1079a..abe5c2f73bd 100644 --- a/src/core/features/settings/pages/dev/dev.html +++ b/src/core/features/settings/pages/dev/dev.html @@ -88,6 +88,53 @@

{{ 'core.settings.developeroptions' | translate }}

+ + +

WebService token

+

{{ token }}

+
+
+ + +

Private token

+ @if (privateToken) { +

{{ privateToken }}

+ } @else { +

---

+ } +
+
+ + +

Files access key

+ @if (filesAccessKey) { +

{{ filesAccessKey }}

+ } @else { +

---

+ } +
+
+ + @if (privateToken) { + + +

Minimum time between auto-login requests

+

{{ autoLoginTimeBetweenRequests | coreDuration }}

+
+
+ + +

Last auto login in this device

+ @if (lastAutoLoginTime && lastAutoLoginTime > 0) { +

{{ lastAutoLoginTime | coreTimeAgo }}

+ This value will reset when the app is restarted. + } @else { +

---

+ } +
+
+ } +

Disabled features

diff --git a/src/core/features/settings/pages/dev/dev.ts b/src/core/features/settings/pages/dev/dev.ts index ac6b3a07017..29123a57d1b 100644 --- a/src/core/features/settings/pages/dev/dev.ts +++ b/src/core/features/settings/pages/dev/dev.ts @@ -62,6 +62,13 @@ export class CoreSettingsDevPage implements OnInit { siteId: string | undefined; + token?: string; + privateToken?: string; + filesAccessKey?: string; + + autoLoginTimeBetweenRequests?: number; + lastAutoLoginTime?: number; + async ngOnInit(): Promise { this.rtl = CorePlatform.isRTL; this.RTLChanged(); @@ -69,7 +76,8 @@ export class CoreSettingsDevPage implements OnInit { this.forceSafeAreaMargins = document.documentElement.classList.contains('force-safe-area-margins'); this.safeAreaChanged(); - this.siteId = CoreSites.getCurrentSite()?.getId(); + const currentSite = CoreSites.getCurrentSite(); + this.siteId = currentSite?.getId(); this.stagingSitesCount = CoreConstants.CONFIG.sites.filter((site) => site.staging).length; @@ -79,7 +87,7 @@ export class CoreSettingsDevPage implements OnInit { } this.alwaysShowLoginForm = Boolean(await CoreConfig.get(ALWAYS_SHOW_LOGIN_FORM, 0)); - if (!this.siteId) { + if (!currentSite) { return; } @@ -91,6 +99,15 @@ export class CoreSettingsDevPage implements OnInit { this.userToursEnabled = !CoreUserTours.isDisabled(); + const privateToken = currentSite.getPrivateToken(); + const filesAccessKey = currentSite.getFilesAccessKey(); + this.token = '...' + currentSite.getToken().slice(-3); + this.privateToken = privateToken && ('...' + privateToken.slice(-3)); + this.filesAccessKey = filesAccessKey && ('...' + filesAccessKey.slice(-3)); + + this.autoLoginTimeBetweenRequests = await currentSite.getAutoLoginMinTimeBetweenRequests(); + this.lastAutoLoginTime = currentSite.getLastAutoLoginTime(); + document.head.querySelectorAll('style').forEach((style) => { if (this.siteId && style.id.endsWith(this.siteId)) { if (style.innerHTML.length > 0) { @@ -113,7 +130,7 @@ export class CoreSettingsDevPage implements OnInit { version: plugin.version, })); - const disabledFeatures = (await CoreSites.getCurrentSite()?.getPublicConfig())?.tool_mobile_disabledfeatures; + const disabledFeatures = (await currentSite.getPublicConfig())?.tool_mobile_disabledfeatures; this.disabledFeatures = disabledFeatures?.split(',').filter(feature => feature.trim().length > 0) ?? []; } @@ -183,7 +200,12 @@ export class CoreSettingsDevPage implements OnInit { * Copies site info. */ copyInfo(): void { - CoreText.copyToClipboard(JSON.stringify({ disabledFeatures: this.disabledFeatures, sitePlugins: this.sitePlugins })); + CoreText.copyToClipboard(JSON.stringify({ + disabledFeatures: this.disabledFeatures, + sitePlugins: this.sitePlugins, + autoLoginTimeBetweenRequests: this.autoLoginTimeBetweenRequests, + lastAutoLoginTime: this.lastAutoLoginTime, + })); } /** diff --git a/src/theme/theme.base.scss b/src/theme/theme.base.scss index d56ff371358..f5f8d1d1eeb 100644 --- a/src/theme/theme.base.scss +++ b/src/theme/theme.base.scss @@ -42,6 +42,16 @@ a { font-weight: bold; } +.core-text-sm { + font: var(--mdl-typography-body-font-sm); +} +.core-text-md { + font: var(--mdl-typography-body-font-md); +} +.core-text-lg { + font: var(--mdl-typography-body-font-lg); +} + .img-responsive { display: block; max-width: 100%;