2023-10-17 19:04:15 +02:00
|
|
|
"use strict;"
|
|
|
|
async function getAuthToken() {
|
|
|
|
let storageData, dec, tok;
|
|
|
|
storageData = window.sessionStorage.getItem("token");
|
|
|
|
while (!tok) {
|
|
|
|
storageData = window.sessionStorage.getItem("token");
|
|
|
|
dec = JSON.parse(storageData);
|
2024-04-08 21:25:44 +02:00
|
|
|
if (!!dec && "accessToken" in dec) {
|
2023-11-13 20:25:49 +01:00
|
|
|
let tokInspect = dec.accessToken;
|
2023-10-17 19:04:15 +02:00
|
|
|
if (tokInspect && tokInspect.startsWith("eyJ")) {
|
|
|
|
tok = tokInspect;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log("No accessToken available. Sleeping...")
|
|
|
|
await new Promise(res => setTimeout(res, 5000));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
function correlationId() {
|
|
|
|
return crypto.randomUUID() + "_" + crypto.randomUUID()
|
|
|
|
}
|
|
|
|
|
2024-02-15 19:14:55 +01:00
|
|
|
async function getKundenkontoV2(){
|
|
|
|
let tok = await getAuthToken();
|
|
|
|
let req = await fetch("https://www.bahn.de/web/api/kundenkonto/v2", {
|
|
|
|
headers: [
|
|
|
|
["Accept", "application/json"], ["authorization", "Bearer " + tok], //["X-Correlation-ID", correlationId()]
|
|
|
|
]
|
|
|
|
});
|
|
|
|
if(req.ok){
|
|
|
|
const data = await req.json();
|
|
|
|
return {
|
|
|
|
"addr__appellation": data.anrede,
|
|
|
|
"addr__firstName": data.vorname,
|
|
|
|
"addr__surName": data.nachname,
|
|
|
|
"bday": data.geburtsdatum,
|
|
|
|
"addr__email": data.email,
|
|
|
|
"addr__placename": data.hauptadresse.ort,
|
|
|
|
"addr__postcode": data.hauptadresse.plz,
|
2024-04-10 18:47:00 +02:00
|
|
|
"addr__street": data.hauptadresse.strasse,
|
|
|
|
"addr__country": data.hauptadresse.land
|
2024-02-15 19:14:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:04:15 +02:00
|
|
|
async function getBahncardInfo() {
|
|
|
|
let tok = await getAuthToken();
|
|
|
|
let req = await fetch("https://www.bahn.de/web/api/kundenkonto/bahncard", {
|
|
|
|
headers: [
|
|
|
|
["Accept", "application/json"], ["authorization", "Bearer " + tok], //["X-Correlation-ID", correlationId()]
|
|
|
|
]
|
|
|
|
});
|
|
|
|
if (req.ok) {
|
|
|
|
const data = await req.json();
|
|
|
|
console.log(data);
|
|
|
|
const orders = data.bahnCardBestellungen.filter((order) => {
|
|
|
|
return order.bahncardTyp === "BC100" && (Date.parse(order.gueltigAb) < Date.now())
|
|
|
|
});
|
|
|
|
console.log("BahnCard candidates:", orders);
|
|
|
|
if (orders[0] !== undefined) {
|
|
|
|
// Orders are returned in reverse chronological order.
|
|
|
|
// TODO Ask which one if there are multiple.
|
|
|
|
// (Maybe inject a <dialog> element here, those have <input type=radio> and dropdowns.)
|
|
|
|
return orders[0].bcNummer || false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log("Request failed:", req);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-02-15 19:14:55 +01:00
|
|
|
browser.storage.sync.get(["acquireAll","acquireBahncard"]).then(v => {
|
|
|
|
if (v.acquireAll!=false){
|
|
|
|
Promise.all([getKundenkontoV2(), getBahncardInfo()]).then(data => {
|
|
|
|
const saveData = {...data[0], "bcnum": data[1]};
|
|
|
|
console.log("Saving", saveData);
|
|
|
|
browser.storage.sync.set(saveData).then(()=>{
|
|
|
|
alert("Saved all available data to settings.\nPlease enter your bank details on the bottom of the settings page.");
|
|
|
|
window.close();
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2023-10-17 19:04:15 +02:00
|
|
|
if (v.acquireBahncard != false) { // may also be undefined, so no identity check here
|
|
|
|
getBahncardInfo().then(v => {
|
|
|
|
if (!!v && v.length == 16) {
|
|
|
|
const saveData = { "bcnum": v, "acquireBahncard": false }
|
|
|
|
console.log("Saving", saveData);
|
2024-02-15 19:14:55 +01:00
|
|
|
browser.storage.sync.set(saveData).then(() => {
|
|
|
|
alert("Saved your BahnCard number to settings.\nReload the settings page to continue.");
|
|
|
|
window.close();
|
2023-10-17 19:04:15 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}, f => console.log("Could not acquire BahnCard number.", f))
|
|
|
|
}
|
|
|
|
})
|