mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 00:08:25 +00:00
* fix: rotate refresh tokens and revoke the session when an old one is reused A refresh token could be replayed to get new access tokens until it expired, so a leaked token gave access for up to seven days. * test: mint the refresh token inside the frozen time The token was minted at the real time, so refreshing at an earlier frozen time failed once the clock passed it. * fix: document the rotated refresh token in the token refresh response The schema checked simplejwt's own rotation setting, which is off, so the API docs left out refresh_token. * fix: keep the user session valid across refresh token rotation The previous release crashed on the new cookie payload, and the cookie stopped working as soon as its refresh token rotated. * test: sign out through the API in the secure file serve test Signing out revokes the session, which is what the user session cookie checks now.
87 lines
2.3 KiB
TypeScript
87 lines
2.3 KiB
TypeScript
import { getClient } from "../client";
|
|
import { faker } from "@faker-js/faker";
|
|
|
|
export type User = {
|
|
name: string;
|
|
email: string;
|
|
password?: string;
|
|
language: string;
|
|
accessToken: string;
|
|
refreshToken: string;
|
|
};
|
|
|
|
export async function getTokenAuth(
|
|
email: String,
|
|
password: String
|
|
): Promise<User> {
|
|
/**
|
|
* Authenticates an existing user.
|
|
*/
|
|
const response: any = await getClient().post("user/token-auth/", {
|
|
email: email,
|
|
password: password,
|
|
});
|
|
return {
|
|
name: response.data.user.first_name,
|
|
email: response.data.user.username,
|
|
password: String(password),
|
|
language: response.data.user.language,
|
|
accessToken: response.data.access_token,
|
|
refreshToken: response.data.refresh_token,
|
|
};
|
|
}
|
|
|
|
export async function getBrowserRefreshToken(user: User): Promise<string> {
|
|
/**
|
|
* Returns a new refresh token to log the browser in with. Refreshing rotates
|
|
* the refresh token, so one the browser already used stops working.
|
|
*/
|
|
if (!user.password) {
|
|
return user.refreshToken;
|
|
}
|
|
return (await getTokenAuth(user.email, user.password)).refreshToken;
|
|
}
|
|
|
|
export async function getStaffUser(): Promise<User> {
|
|
/**
|
|
* Authenticates as the 'e2e' staff user. Used in fixtures which rely
|
|
* on API endpoints that require an admin/staff user.
|
|
*/
|
|
return getTokenAuth("e2e@baserow.io", "testpassword");
|
|
}
|
|
|
|
export async function createUser(
|
|
skipOnboarding = true,
|
|
skipGuidedTours = true
|
|
): Promise<User> {
|
|
const password = faker.internet.password();
|
|
const response: any = await getClient().post("user/", {
|
|
name: faker.person.fullName(),
|
|
email: faker.internet.email(),
|
|
password,
|
|
language: "en",
|
|
authenticate: true,
|
|
});
|
|
const user: User = {
|
|
name: response.data.user.first_name,
|
|
email: response.data.user.username,
|
|
password,
|
|
language: response.data.user.language,
|
|
accessToken: response.data.access_token,
|
|
refreshToken: response.data.refresh_token,
|
|
};
|
|
if (skipOnboarding || skipGuidedTours) {
|
|
await getClient(user).patch("user/account/", {
|
|
completed_onboarding: skipOnboarding,
|
|
completed_guided_tours: skipGuidedTours
|
|
? ["sidebar", "database", "builder", "automation"]
|
|
: [],
|
|
});
|
|
}
|
|
return user;
|
|
}
|
|
|
|
export async function deleteUser(user: User): Promise<any> {
|
|
await getClient(user).post("user/schedule-account-deletion/");
|
|
}
|