Skip to content

Commit

Permalink
Merge pull request #4233 from dpalou/MOBILE-4679
Browse files Browse the repository at this point in the history
MOBILE-4679 dev: Display auto login info and part of tokens in dev page
  • Loading branch information
crazyserver authored Nov 18, 2024
2 parents e0c39dc + 9f46fe6 commit 1afe196
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 11 deletions.
44 changes: 37 additions & 7 deletions src/core/classes/sites/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -775,7 +772,7 @@ export class CoreSite extends CoreAuthenticatedSite {
* @returns Promise resolved with boolean: whether it works or not.
*/
checkTokenPluginFile(url: string): Promise<boolean> {
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) {
Expand Down Expand Up @@ -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<number> {
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;
}

}

/**
Expand Down
47 changes: 47 additions & 0 deletions src/core/features/settings/pages/dev/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,53 @@ <h1>{{ 'core.settings.developeroptions' | translate }}</h1>
</ion-item>

<ng-container *ngIf="siteId">
<ion-item class="ion-text-wrap">
<ion-label>
<p class="item-heading">WebService token</p>
<p>{{ token }}</p>
</ion-label>
</ion-item>
<ion-item class="ion-text-wrap">
<ion-label>
<p class="item-heading">Private token</p>
@if (privateToken) {
<p>{{ privateToken }}</p>
} @else {
<p>---</p>
}
</ion-label>
</ion-item>
<ion-item class="ion-text-wrap">
<ion-label>
<p class="item-heading">Files access key</p>
@if (filesAccessKey) {
<p>{{ filesAccessKey }}</p>
} @else {
<p>---</p>
}
</ion-label>
</ion-item>

@if (privateToken) {
<ion-item class="ion-text-wrap" *ngIf="autoLoginTimeBetweenRequests">
<ion-label>
<p class="item-heading">Minimum time between auto-login requests</p>
<p>{{ autoLoginTimeBetweenRequests | coreDuration }}</p>
</ion-label>
</ion-item>
<ion-item class="ion-text-wrap">
<ion-label>
<p class="item-heading">Last auto login in this device</p>
@if (lastAutoLoginTime && lastAutoLoginTime > 0) {
<p>{{ lastAutoLoginTime | coreTimeAgo }}</p>
<ion-note class="core-text-sm">This value will reset when the app is restarted.</ion-note>
} @else {
<p>---</p>
}
</ion-label>
</ion-item>
}

<ion-item-divider>
<ion-label>
<h2>Disabled features</h2>
Expand Down
30 changes: 26 additions & 4 deletions src/core/features/settings/pages/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,22 @@ export class CoreSettingsDevPage implements OnInit {

siteId: string | undefined;

token?: string;
privateToken?: string;
filesAccessKey?: string;

autoLoginTimeBetweenRequests?: number;
lastAutoLoginTime?: number;

async ngOnInit(): Promise<void> {
this.rtl = CorePlatform.isRTL;
this.RTLChanged();

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;

Expand All @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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) ?? [];
}
Expand Down Expand Up @@ -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,
}));
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/theme/theme.base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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%;
Expand Down

0 comments on commit 1afe196

Please sign in to comment.