Skip to content
This repository has been archived by the owner on Oct 15, 2023. It is now read-only.

[WIP - DO NOT MERGE] Contourner les CORS en utilisant l'API native #114

Draft
wants to merge 3 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions capacitor.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const config: CapacitorConfig = {
Keyboard: {
resize: KeyboardResize.Ionic,
},
"CapacitorHttp": {
// https://capacitorjs.com/docs/apis/http#configuration
// patch Fetch and XHR to use native libs without CORS restrictions
"enabled": true
}
},
/* server: {
url: "http://192.168.1.22:8100",
Expand Down
2 changes: 1 addition & 1 deletion src/functions/fetch/GetHomeworks.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function getSkolengoHomeWork(dateFrom, dateTo, forceReload) {
try {
const token = localStorage.getItem('token');
const ent = localStorage.getItem('ent');
const etudiant = new Kdecole(token, ApiVersion[ent], 0, 'https://cors.api.getpapillon.xyz/' + ApiUrl[ent])
const etudiant = new Kdecole(token, ApiVersion[ent], 0, app.config.globalProperties.$proxyPrefix + ApiUrl[ent])

console.log("[REQUEST] [HOMEWORK] Requesting homeworks...")
const taf = await etudiant.getTravailAFaire(undefined, new Date(dateFrom))
Expand Down
2 changes: 1 addition & 1 deletion src/functions/fetch/GetNews.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function getNews(forceReload) {
function getSkolengoNews(forceReload) {
const token = localStorage.getItem('token');
const ent = localStorage.getItem('ent');
const etudiant = new Kdecole(token, ApiVersion[ent], 0, 'https://cors.api.getpapillon.xyz/' + ApiUrl[ent])
const etudiant = new Kdecole(token, ApiVersion[ent], 0, app.config.globalProperties.$proxyPrefix + ApiUrl[ent])

let newsCache = localStorage.getItem('NewsCache');
if (newsCache != null && !forceReload) {
Expand Down
2 changes: 1 addition & 1 deletion src/functions/fetch/GetTimetable.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function getSkolengoTimetable(date, forceReload) {
resolve(constructSkolengoTimetable(timetable?.listeSeances || []));
});
} else {
const user = new Kdecole(token, ApiVersion[ent], 0, 'https://cors.api.getpapillon.xyz/' + ApiUrl[ent])
const user = new Kdecole(token, ApiVersion[ent], 0, app.config.globalProperties.$proxyPrefix + ApiUrl[ent])

return user.getCalendrier().then(calendrier => {
if (calendrier.cdtOuvert === false) throw new Error("Le calendrier n'est pas ouvert.")
Expand Down
4 changes: 2 additions & 2 deletions src/functions/fetch/GetUserData.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function getSkolengoUser(force) {
resolve(constructSkolengoUser(user));
});
} else {
const etudiant = new Kdecole(token, ApiVersion[ent], 0, 'https://cors.api.getpapillon.xyz/' + ApiUrl[ent])
const etudiant = new Kdecole(token, ApiVersion[ent], 0, app.config.globalProperties.$proxyPrefix + ApiUrl[ent])
return etudiant.getInfoUtilisateur().then(infoUser => {
localStorage.setItem('avatarCache', 'data:image/png;base64,' + defaultAvatar);
const user = constructSkolengoUser(infoUser)
Expand Down Expand Up @@ -114,7 +114,7 @@ async function getPronoteUser(force) {
return constructPronoteUser(user);
}
// download avatar
let url = `https://cors.api.getpapillon.xyz/` + avatar;
let url = app.config.globalProperties.$proxyPrefix + avatar;
axios.get(url, {responseType: 'blob'})
.then((response) => {
// get blob
Expand Down
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import VueLazyload from "vue-lazyload";

import { IonicVue } from '@ionic/vue';

import { Capacitor } from '@capacitor/core';

/* Core CSS required for Ionic components to work properly */
import '@ionic/vue/css/core.css';

Expand Down Expand Up @@ -45,3 +47,5 @@ router.isReady().then(() => {
// Global vars in Vue
app.config.globalProperties.$rn = new Date;
app.config.globalProperties.$api = localStorage.getItem('customApiUrl') || "https://api.getpapillon.xyz";

app.config.globalProperties.$proxyPrefix = Capacitor.isNativePlatform() ? "" : "https://cors.api.getpapillon.xyz/";
5 changes: 3 additions & 2 deletions src/views/login/pronote/SchoolSelection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import axios from 'axios';

import { App } from '@capacitor/app';

import { linkOutline, linkSharp, qrCodeOutline, qrCodeSharp, schoolOutline, schoolSharp, businessOutline, businessSharp, navigateOutline, navigateSharp, personCircleOutline, personCircleSharp, serverOutline, serverSharp } from 'ionicons/icons';

import {app} from '@/main.ts'
import displayToast from '@/functions/utils/displayToast.js';
import { fetchDaysOffAndHolidays } from '@/functions/utils/datetimePicker.js';

Expand Down Expand Up @@ -175,7 +176,7 @@
this.terms = postal;
this.isLoading = true;

axios.get('https://cors.api.getpapillon.xyz/https://positionstack.com/geo_api.php?query=france+' + postal, {
axios.get(app.config.globalProperties.$proxyPrefix + 'https://positionstack.com/geo_api.php?query=france+' + postal, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
Expand Down
5 changes: 3 additions & 2 deletions src/views/login/skolengo/LoginForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
serverSharp
} from 'ionicons/icons';

import {app} from '@/main.ts'
import displayToast from '@/functions/utils/displayToast.js';
import {ApiUrl, ApiVersion, ApiName, Kdecole} from 'kdecole-api'

Expand Down Expand Up @@ -105,7 +106,7 @@ export default defineComponent({
loading.present();

if (password.length === 125) {
const user = new Kdecole(password, ApiVersion[ent], 0, 'https://cors.api.getpapillon.xyz/' + ApiUrl[ent])
const user = new Kdecole(password, ApiVersion[ent], 0, app.config.globalProperties.$proxyPrefix + ApiUrl[ent])
user.starting().then(() => {
localStorage.token = password
localStorage.loggedIn = true
Expand All @@ -117,7 +118,7 @@ export default defineComponent({
displayToast.presentError(`${e.message}`, "danger", e)
})
} else {
Kdecole.login(username, password, ApiVersion[ent], 'https://cors.api.getpapillon.xyz/' + ApiUrl[ent]).then(token => {
Kdecole.login(username, password, ApiVersion[ent], app.config.globalProperties.$proxyPrefix + ApiUrl[ent]).then(token => {
localStorage.token = token
localStorage.loggedIn = true
localStorage.loginService = "skolengo";
Expand Down
3 changes: 2 additions & 1 deletion src/views/settings/LogView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
IonSearchbar
} from '@ionic/vue';

import {app} from '@/main.ts'
import PapillonBackButton from '@/components/PapillonBackButton.vue';


Expand Down Expand Up @@ -162,7 +163,7 @@
displayToast.presentNativeToast("Préparation des logs...");

// Post logs to hastebin (https://logs.getpapillon.xyz)
let response = await fetch("https://cors.api.getpapillon.xyz/https://logs.getpapillon.xyz/documents", {
let response = await fetch(app.config.globalProperties.$proxyPrefix + "https://logs.getpapillon.xyz/documents", {
method: "POST",
body: this.logs.map(log => { return `[${log.type}] - ${log.date.replace('T', ' ')} - ${log.message}`; }).join("\n")
});
Expand Down