1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-26 22:41:17 +02:00

All: Open the connection screen when a SAML session has expired

This commit is contained in:
Laurent Cozic
2025-10-29 13:41:32 +01:00
parent 42d8df3036
commit fc0014c0b5
3 changed files with 77 additions and 45 deletions

View File

@@ -78,7 +78,7 @@ export default class SyncTargetJoplinServer extends BaseSyncTarget {
return super.fileApi();
}
public static async checkConfig(options: FileApiOptions, syncTargetId: number = null) {
public static async checkConfig(options: FileApiOptions, syncTargetId: number = null, fileApi: FileApi|null = null) {
const output = {
ok: false,
errorMessage: '',
@@ -86,10 +86,9 @@ export default class SyncTargetJoplinServer extends BaseSyncTarget {
syncTargetId = syncTargetId === null ? this.id() : syncTargetId;
let fileApi = null;
if (!fileApi) {
try {
fileApi = await newFileApi(syncTargetId, options);
fileApi.requestRepeatCount_ = 0;
} catch (error) {
// If there's an error it's probably an application error, but we
// can't proceed anyway, so exit.
@@ -97,7 +96,12 @@ export default class SyncTargetJoplinServer extends BaseSyncTarget {
if (error.code) output.errorMessage += ` (Code ${error.code})`;
return output;
}
}
const previousRequestRepeatCount = fileApi.requestRepeatCount_;
fileApi.requestRepeatCount_ = 0;
try {
// First we try to fetch info.json. It may not be present if it's a new
// sync target but otherwise, if it is, and it's valid, we know the
// credentials are valid. We do this test first because it will work
@@ -131,6 +135,9 @@ export default class SyncTargetJoplinServer extends BaseSyncTarget {
output.errorMessage = error.message;
if (error.code) output.errorMessage += ` (Code ${error.code})`;
}
} finally {
fileApi.requestRepeatCount_ = previousRequestRepeatCount;
}
return output;
}

View File

@@ -52,6 +52,9 @@ export const authenticateWithCode = async (code: string) => {
//
// Based on the regular Joplin Server sync target.
export default class SyncTargetJoplinServerSAML extends SyncTargetJoplinServer {
private lastFileApiOptions_: FileApiOptions|null = null;
public static override id() {
return 11;
}
@@ -65,7 +68,16 @@ export default class SyncTargetJoplinServerSAML extends SyncTargetJoplinServer {
}
public override async isAuthenticated() {
return Setting.value('sync.11.id') !== '';
if (!Setting.value('sync.11.id')) return false;
// We check that the file API has been initialized at least once, otherwise the below check
// will always fail and it will be impossible to login.
if (this.lastFileApiOptions_) {
const check = await SyncTargetJoplinServer.checkConfig(null, null, await this.fileApi());
return check.ok;
}
return true;
}
public static override requiresPassword() {
@@ -111,11 +123,12 @@ export default class SyncTargetJoplinServerSAML extends SyncTargetJoplinServer {
}
protected override async initFileApi() {
return initFileApi(SyncTargetJoplinServerSAML.id(), this.logger(), {
this.lastFileApiOptions_ = {
path: () => Setting.value('sync.11.path'),
userContentPath: () => Setting.value('sync.11.userContentPath'),
username: () => '',
password: () => '',
});
};
return initFileApi(SyncTargetJoplinServerSAML.id(), this.logger(), this.lastFileApiOptions_);
}
}

View File

@@ -2,6 +2,7 @@ import { utils, CommandRuntime, CommandDeclaration, CommandContext } from '../se
import { _ } from '../locale';
import { reg } from '../registry';
import Setting from '../models/Setting';
import NavService from '../services/NavService';
export const declaration: CommandDeclaration = {
name: 'synchronize',
@@ -35,7 +36,18 @@ export const runtime = (): CommandRuntime => {
return 'auth';
}
reg.logger().error('Not authenticated with sync target - please check your credentials.');
const error = new Error('Not authenticated with sync target - please check your credentials.');
utils.store.dispatch({
type: 'SYNC_REPORT_UPDATE',
report: { errors: [error] },
});
if (Setting.value('sync.target') === 11) {
await NavService.go('JoplinServerSamlLogin');
}
reg.logger().error(error);
return 'error';
}