Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically use user token when sharing token is not sufficient for a request #8139

Merged
merged 16 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ For upgrade instructions, please check the [migration guide](MIGRATIONS.released

### Fixed
- Fixed a bug during dataset upload in case the configured `datastore.baseFolder` is an absolute path. [#8098](https://github.com/scalableminds/webknossos/pull/8098) [#8103](https://github.com/scalableminds/webknossos/pull/8103)
- When trying to save an annotation opened via a link including a sharing token, the token is automatically discarded in case it is insufficient for update actions but the users token is. [#8139](https://github.com/scalableminds/webknossos/pull/8139)
- Fixed that the skeleton search did not automatically expand groups that contained the selected tree [#8129](https://github.com/scalableminds/webknossos/pull/8129)
- Fixed a bug that zarr streaming version 3 returned the shape of mag (1, 1, 1) / the finest mag for all mags. [#8116](https://github.com/scalableminds/webknossos/pull/8116)
- Fixed sorting of mags in outbound zarr streaming. [#8125](https://github.com/scalableminds/webknossos/pull/8125)
Expand Down
42 changes: 35 additions & 7 deletions frontend/javascripts/admin/api/token.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { location } from "libs/window";
import Request from "libs/request";
import * as Utils from "libs/utils";
import Toast from "libs/toast";

let tokenPromise: Promise<string>;

let tokenRequestPromise: Promise<string> | null;
let shouldUseURLToken: boolean = true;
MichaelBuessemeyer marked this conversation as resolved.
Show resolved Hide resolved

function requestUserToken(): Promise<string> {
if (tokenRequestPromise) {
Expand Down Expand Up @@ -33,22 +35,48 @@ export function getSharingTokenFromUrlParameters(): string | null | undefined {
return null;
}

export function doWithToken<T>(fn: (token: string) => Promise<T>, tries: number = 1): Promise<any> {
const sharingToken = getSharingTokenFromUrlParameters();
function removeErrorToast(error: any) {
if ("errors" in error && Array.isArray(error.errors)) {
error.errors.forEach((errorText: string) => {
if (errorText.includes("Token may be expired")) {
Toast.close(errorText);
}
});
}
}

export async function doWithToken<T>(
fn: (token: string) => Promise<T>,
tries: number = 1,
useURLTokenIfAvailable: boolean = true,
): Promise<any> {
MichaelBuessemeyer marked this conversation as resolved.
Show resolved Hide resolved
let token =
useURLTokenIfAvailable && shouldUseURLToken ? getSharingTokenFromUrlParameters() : null;

if (sharingToken != null) {
return fn(sharingToken);
if (token == null) {
tokenPromise = tokenPromise == null ? requestUserToken() : tokenPromise;
} else {
tokenPromise = Promise.resolve(token);
}

if (!tokenPromise) tokenPromise = requestUserToken();
return tokenPromise.then(fn).catch((error) => {
return tokenPromise.then(fn).catch(async (error) => {
if (error.status === 403) {
console.warn("Token expired. Requesting new token...");
tokenPromise = requestUserToken();

// If three new tokens did not fix the 403, abort, otherwise we'll get into an endless loop here
if (tries < 3) {
return doWithToken(fn, tries + 1);
// If using the url sharing token failed, we try the user specific token instead.
const result = await doWithToken(fn, tries + 1, false);
// Upon successful retry with own token, discard the url token.
if (useURLTokenIfAvailable) {
shouldUseURLToken = false;
Toast.info(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a technical detail that we shouldn't bother users with. I would replace the Toast with a console.log() for debugging purposes but not actively show it in the UI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just hiding the error toast is enough you think? My thought was to somewhat inform the user that saving / or some other request worked on second try.

Therefore, maybe a toast with a message like "Request succeeded on upon retry" or something similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But yeah, I agree that the technical details are definitely too much in my toast message

"Initial request using the URL token failed. Your personal token will be used from now on.",
);
removeErrorToast(error);
}
return result;
}
}

Expand Down