Implement CSRF
Deploy Spot / deploy (push) Successful in 34s

This commit is contained in:
2026-05-28 13:22:44 +02:00
parent 8092846d6f
commit fdd0ada815
14 changed files with 129 additions and 106 deletions
+38 -22
View File
@@ -1,44 +1,60 @@
export default class Api {
constructor({server, processPage, timezone, errorCode, lang}) {
constructor({server, processPage, timezone, csrfToken, errorCode, lang}) {
this.server = server;
this.processPage = processPage;
this.timezone = timezone;
this.csrfToken = csrfToken;
this.errorCode = errorCode;
this.lang = lang;
}
async get(action, params = {}) {
const response = await this.request(action, params);
async get(sAction, asParams = {}) {
const response = await this.request(sAction, asParams, 'GET');
return response.data;
}
async request(action, params = {}) {
const requestParams = {
...params,
a: action,
async post(sAction, asParams = {}) {
const response = await this.request(sAction, asParams, 'POST');
return response.data;
}
async request(sAction, asParams = {}, method = 'GET') {
const oUrl = new URL(this.processPage, this.server);
const sUrlParams = new URLSearchParams({
...asParams,
a: sAction,
t: this.timezone
}).toString();
const asOptions = {
method,
headers: {'Accept': 'application/json'}
};
const url = new URL(this.processPage, this.server);
url.search = new URLSearchParams(requestParams).toString();
const request = await fetch(url, {
method: 'GET',
headers: {'Content-Type': 'application/json'}
});
if(!request.ok) {
throw new Error('Error HTTP ' + request.status + ': ' + request.statusText);
if(method === 'GET') {
oUrl.search = sUrlParams;
}
else {
asOptions.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
asOptions.headers['X-CSRF-Token'] = this.csrfToken;
asOptions.body = sUrlParams;
}
const response = await request.json();
response.desc = this.lang.parse(response.desc);
const oRequest = await fetch(oUrl, asOptions);
if(response.result == this.errorCode) {
throw response.desc;
if(!oRequest.ok) {
throw new Error('Error HTTP ' + oRequest.status + ': ' + oRequest.statusText);
}
return response;
const oResponse = await oRequest.json();
oResponse.desc = this.lang.parse(oResponse.desc);
if(oResponse.result == this.errorCode) {
throw oResponse.desc;
}
return oResponse;
}
}
+13 -4
View File
@@ -1,4 +1,5 @@
import { LngLat } from 'maplibre-gl';
const EARTH_RADIUS = 6371008.8;
const DEGREES_TO_RADIANS = Math.PI / 180;
export default class Projects {
@@ -63,12 +64,10 @@ export default class Projects {
let iDistance = 0, iElevDrop = 0, iElevGain = 0, iTime = 0;
for(let i = 1; i < aoCoords.length; i++) {
const oCurrPoint = new LngLat(aoCoords[i][0], aoCoords[i][1]);
const oPrevPoint = new LngLat(aoCoords[i - 1][0], aoCoords[i - 1][1]);
const iCurrElev = Number(aoCoords[i][2]);
const iPrevElev = Number(aoCoords[i - 1][2]);
const iElevDelta = (Number.isFinite(iCurrElev) && Number.isFinite(iPrevElev))?(iCurrElev - iPrevElev):0;
const iSegDistance = oCurrPoint.distanceTo(oPrevPoint);
const iSegDistance = this.getDistance(aoCoords[i], aoCoords[i - 1]);
if(iSegDistance <= 0) continue;
iDistance += iSegDistance;
@@ -94,4 +93,14 @@ export default class Projects {
time: iTime
};
}
getDistance(aoPointA, aoPointB) {
const iLatA = aoPointA[1] * DEGREES_TO_RADIANS;
const iLatB = aoPointB[1] * DEGREES_TO_RADIANS;
const iAngle =
Math.sin(iLatA) * Math.sin(iLatB)
+ Math.cos(iLatA) * Math.cos(iLatB) * Math.cos((aoPointB[0] - aoPointA[0]) * DEGREES_TO_RADIANS);
return EARTH_RADIUS * Math.acos(Math.min(iAngle, 1));
}
}