mirror of
https://github.com/rommapp/romm.git
synced 2026-12-04 21:02:21 +01:00
hopefully actually fix csrf tokens
This commit is contained in:
@@ -63,6 +63,7 @@ if not IS_PYTEST_RUN and not DISABLE_CSRF_PROTECTION:
|
||||
# CSRF protection (except endpoints listed in exempt_urls)
|
||||
app.add_middleware(
|
||||
CustomCSRFMiddleware,
|
||||
cookie_name="romm_csrftoken",
|
||||
secret=ROMM_AUTH_SECRET_KEY,
|
||||
exempt_urls=[re.compile(r"^/token.*"), re.compile(r"^/ws")],
|
||||
)
|
||||
|
||||
@@ -5,50 +5,34 @@ import userApi from "@/services/api/user";
|
||||
import storeAuth from "@/stores/auth";
|
||||
import storeConfig from "@/stores/config";
|
||||
import storeHeartbeat from "@/stores/heartbeat";
|
||||
import storeNotifications from "@/stores/notifications";
|
||||
import Cookies from "js-cookie";
|
||||
import { storeToRefs } from "pinia";
|
||||
import { onBeforeMount } from "vue";
|
||||
import router from "./plugins/router";
|
||||
|
||||
// Props
|
||||
const notificationStore = storeNotifications();
|
||||
const { notifications } = storeToRefs(notificationStore);
|
||||
const heartbeat = storeHeartbeat();
|
||||
const auth = storeAuth();
|
||||
const configStore = storeConfig();
|
||||
|
||||
onBeforeMount(async () => {
|
||||
await api
|
||||
.get("/heartbeat")
|
||||
.then(async ({ data: data }) => {
|
||||
heartbeat.set(data);
|
||||
if (heartbeat.value.SHOW_SETUP_WIZARD) {
|
||||
router.push({ name: "setup" });
|
||||
} else {
|
||||
await userApi
|
||||
.fetchCurrentUser()
|
||||
.then(({ data: user }) => {
|
||||
auth.setUser(user);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
await api.get("/config").then(({ data: data }) => {
|
||||
configStore.set(data);
|
||||
onBeforeMount(() => {
|
||||
api.get("/heartbeat").then(async ({ data: data }) => {
|
||||
heartbeat.set(data);
|
||||
if (heartbeat.value.SHOW_SETUP_WIZARD) {
|
||||
router.push({ name: "setup" });
|
||||
} else {
|
||||
await userApi
|
||||
.fetchCurrentUser()
|
||||
.then(({ data: user }) => {
|
||||
auth.setUser(user);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
const allCookies = Cookies.get(); // Get all cookies
|
||||
for (let cookie in allCookies) {
|
||||
Cookies.remove(cookie); // Remove each cookie
|
||||
}
|
||||
router.push({
|
||||
name: "login",
|
||||
|
||||
await api.get("/config").then(({ data: data }) => {
|
||||
configStore.set(data);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import type { UserSchema } from "@/__generated__";
|
||||
import identityApi from "@/services/api/identity";
|
||||
import { refetchCSRFToken } from "@/services/api/index";
|
||||
import storeAuth from "@/stores/auth";
|
||||
import storeNavigation from "@/stores/navigation";
|
||||
import type { Events } from "@/types/emitter";
|
||||
@@ -22,13 +23,17 @@ const { smAndDown } = useDisplay();
|
||||
|
||||
// Functions
|
||||
async function logout() {
|
||||
identityApi.logout().then(({ data }) => {
|
||||
identityApi.logout().then(async ({ data }) => {
|
||||
// Refetch CSRF token
|
||||
await refetchCSRFToken();
|
||||
|
||||
emitter?.emit("snackbarShow", {
|
||||
msg: data.msg,
|
||||
icon: "mdi-check-bold",
|
||||
color: "green",
|
||||
});
|
||||
});
|
||||
|
||||
await router.push({ name: "login" });
|
||||
auth.setUser(null);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import router from "@/plugins/router";
|
||||
import axios from "axios";
|
||||
import { default as cookie, default as Cookies } from "js-cookie";
|
||||
import { default as Cookies } from "js-cookie";
|
||||
import { debounce } from "lodash";
|
||||
|
||||
const api = axios.create({ baseURL: "/api", timeout: 120000 });
|
||||
@@ -19,7 +19,7 @@ api.interceptors.request.use((config) => {
|
||||
networkQuiesced.cancel();
|
||||
|
||||
// Set CSRF header for all requests
|
||||
config.headers["x-csrftoken"] = cookie.get("csrftoken");
|
||||
config.headers["x-csrftoken"] = Cookies.get("romm_csrftoken");
|
||||
return config;
|
||||
});
|
||||
|
||||
@@ -35,15 +35,22 @@ api.interceptors.response.use(
|
||||
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
async (error) => {
|
||||
if (error.response?.status === 403) {
|
||||
const allCookies = Cookies.get(); // Get all cookies
|
||||
for (const cookie in allCookies) {
|
||||
Cookies.remove(cookie); // Remove each cookie
|
||||
}
|
||||
// Clear cookies and redirect to login page
|
||||
Cookies.remove("romm_session");
|
||||
|
||||
// Refetch CSRF cookie
|
||||
await refetchCSRFToken();
|
||||
|
||||
const pathname = window.location.pathname;
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
|
||||
router.push({
|
||||
name: "login",
|
||||
query: { next: router.currentRoute.value.path },
|
||||
query: {
|
||||
next: params.get("next") ?? (pathname !== "/login" ? pathname : "/"),
|
||||
},
|
||||
});
|
||||
}
|
||||
return Promise.reject(error);
|
||||
@@ -51,3 +58,9 @@ api.interceptors.response.use(
|
||||
);
|
||||
|
||||
export default api;
|
||||
|
||||
export async function refetchCSRFToken() {
|
||||
Cookies.remove("romm_csrftoken");
|
||||
|
||||
return await api.get("/heartbeat");
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import identityApi from "@/services/api/identity";
|
||||
import api from "@/services/api/index";
|
||||
import { refetchCSRFToken } from "@/services/api/index";
|
||||
import storeHeartbeat from "@/stores/heartbeat";
|
||||
import type { Events } from "@/types/emitter";
|
||||
import type { Emitter } from "mitt";
|
||||
@@ -18,56 +18,32 @@ const logging = ref(false);
|
||||
|
||||
async function login() {
|
||||
logging.value = true;
|
||||
|
||||
await identityApi
|
||||
.login(username.value, password.value)
|
||||
.then(() => {
|
||||
const next = (router.currentRoute.value.query?.next || "/").toString();
|
||||
router.push(next);
|
||||
.then(async () => {
|
||||
// Refetch CSRF token
|
||||
await refetchCSRFToken();
|
||||
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
router.push(params.get("next") ?? "/");
|
||||
})
|
||||
.catch(async ({ response, message }) => {
|
||||
.catch(({ response, message }) => {
|
||||
const errorMessage =
|
||||
response.data?.detail ||
|
||||
response.data ||
|
||||
message ||
|
||||
response.statusText;
|
||||
|
||||
if (errorMessage.includes("CSRF token")) {
|
||||
// Regenerate and retry login if CSRF token expired
|
||||
await api.get("/heartbeat");
|
||||
await identityApi
|
||||
.login(username.value, password.value)
|
||||
.then(() => {
|
||||
const next = (
|
||||
router.currentRoute.value.query?.next || "/"
|
||||
).toString();
|
||||
router.push(next);
|
||||
})
|
||||
.catch(async ({ response, message }) => {
|
||||
const errorMessage =
|
||||
response.data?.detail ||
|
||||
response.data ||
|
||||
message ||
|
||||
response.statusText;
|
||||
emitter?.emit("snackbarShow", {
|
||||
msg: `Unable to login: ${errorMessage}`,
|
||||
icon: "mdi-close-circle",
|
||||
color: "red",
|
||||
});
|
||||
|
||||
emitter?.emit("snackbarShow", {
|
||||
msg: `Unable to login: ${errorMessage}`,
|
||||
icon: "mdi-close-circle",
|
||||
color: "red",
|
||||
});
|
||||
console.error(
|
||||
`[${response.status} ${response.statusText}] ${errorMessage}`
|
||||
);
|
||||
});
|
||||
} else {
|
||||
emitter?.emit("snackbarShow", {
|
||||
msg: `Unable to login: ${errorMessage}`,
|
||||
icon: "mdi-close-circle",
|
||||
color: "red",
|
||||
});
|
||||
console.error(
|
||||
`[${response.status} ${response.statusText}] ${errorMessage}`
|
||||
);
|
||||
}
|
||||
console.error(
|
||||
`[${response.status} ${response.statusText}] ${errorMessage}`
|
||||
);
|
||||
})
|
||||
.finally(() => {
|
||||
logging.value = false;
|
||||
@@ -95,7 +71,6 @@ async function login() {
|
||||
type="text"
|
||||
label="Username"
|
||||
variant="underlined"
|
||||
@keyup.enter="login()"
|
||||
/>
|
||||
<v-text-field
|
||||
v-model="password"
|
||||
@@ -108,7 +83,6 @@ async function login() {
|
||||
:append-inner-icon="
|
||||
visiblePassword ? 'mdi-eye-off' : 'mdi-eye'
|
||||
"
|
||||
@keyup.enter="login()"
|
||||
@click:append-inner="visiblePassword = !visiblePassword"
|
||||
/>
|
||||
<v-btn
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
<script setup lang="ts">
|
||||
import router from "@/plugins/router";
|
||||
import { refetchCSRFToken } from "@/services/api/index";
|
||||
import userApi from "@/services/api/user";
|
||||
import storeHeartbeat from "@/stores/heartbeat";
|
||||
import type { Events } from "@/types/emitter";
|
||||
import type { Emitter } from "mitt";
|
||||
import { useRoute } from "vue-router";
|
||||
import { computed, inject, ref } from "vue";
|
||||
import { useDisplay } from "vuetify";
|
||||
|
||||
@@ -12,7 +12,6 @@ import { useDisplay } from "vuetify";
|
||||
const { xs, smAndDown } = useDisplay();
|
||||
const emitter = inject<Emitter<Events>>("emitter");
|
||||
const heartbeat = storeHeartbeat();
|
||||
const route = useRoute()
|
||||
const visiblePassword = ref(false);
|
||||
// Use a computed property to reactively update metadataOptions based on heartbeat
|
||||
const metadataOptions = computed(() => [
|
||||
@@ -53,8 +52,8 @@ const isLastStep = computed(() => step.value == 2);
|
||||
async function finishWizard() {
|
||||
await userApi
|
||||
.createUser(defaultAdminUser.value)
|
||||
.then(() => {
|
||||
router.go(0); // Needed to get the csrftoken properly after creating default admin user
|
||||
.then(async () => {
|
||||
await refetchCSRFToken();
|
||||
router.push({ name: "login" });
|
||||
})
|
||||
.catch(({ response, message }) => {
|
||||
|
||||
Reference in New Issue
Block a user