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,50 +86,57 @@ export default class SyncTargetJoplinServer extends BaseSyncTarget {
syncTargetId = syncTargetId === null ? this.id() : syncTargetId;
let fileApi = null;
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.
output.errorMessage = error.message;
if (error.code) output.errorMessage += ` (Code ${error.code})`;
return output;
}
// 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
// even if account upload is disabled. And we need such account to
// successfully login so that they can fix it by deleting extraneous
// notes or resources.
try {
const r = await fileApi.get('info.json');
if (r) {
const parsed = JSON.parse(r);
if (parsed) {
output.ok = true;
return output;
}
if (!fileApi) {
try {
fileApi = await newFileApi(syncTargetId, options);
} catch (error) {
// If there's an error it's probably an application error, but we
// can't proceed anyway, so exit.
output.errorMessage = error.message;
if (error.code) output.errorMessage += ` (Code ${error.code})`;
return output;
}
} catch (error) {
// Ignore because we'll use the next test to check for sure if it
// works or not.
staticLogger.warn('Could not fetch or parse info.json:', error);
}
// This is a more generic test, which writes a file and tries to read it
// back.
const previousRequestRepeatCount = fileApi.requestRepeatCount_;
fileApi.requestRepeatCount_ = 0;
try {
await fileApi.put('testing.txt', 'testing');
const result = await fileApi.get('testing.txt');
if (result !== 'testing') throw new Error(`Could not access data on server "${options.path()}"`);
await fileApi.delete('testing.txt');
output.ok = true;
} catch (error) {
output.errorMessage = error.message;
if (error.code) output.errorMessage += ` (Code ${error.code})`;
// 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
// even if account upload is disabled. And we need such account to
// successfully login so that they can fix it by deleting extraneous
// notes or resources.
try {
const r = await fileApi.get('info.json');
if (r) {
const parsed = JSON.parse(r);
if (parsed) {
output.ok = true;
return output;
}
}
} catch (error) {
// Ignore because we'll use the next test to check for sure if it
// works or not.
staticLogger.warn('Could not fetch or parse info.json:', error);
}
// This is a more generic test, which writes a file and tries to read it
// back.
try {
await fileApi.put('testing.txt', 'testing');
const result = await fileApi.get('testing.txt');
if (result !== 'testing') throw new Error(`Could not access data on server "${options.path()}"`);
await fileApi.delete('testing.txt');
output.ok = true;
} catch (error) {
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';
}