From b1d30fb671dac8344db0cd57256840a96202f2b1 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Fri, 20 Sep 2024 16:17:19 +0200 Subject: [PATCH] [+] New super secure token authentication system. --- src/service/api.tsx | 73 +++++++++++++++++++--------------------- src/service/firebase.jsx | 2 +- 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/service/api.tsx b/src/service/api.tsx index 2be430e..c043970 100644 --- a/src/service/api.tsx +++ b/src/service/api.tsx @@ -1,7 +1,8 @@ -import { getAuth } from 'firebase/auth'; -import { Bounce, toast } from "react-toastify"; +import {auth} from './firebase.jsx' +import {Bounce, toast} from "react-toastify"; -const apiUrl: string = 'https://www.servii.fr/api'; +//const apiUrl: string = 'https://www.servii.fr/api'; +const apiUrl: string = 'http://localhost:3000'; interface ApiResponse { return_code: number; @@ -9,7 +10,6 @@ interface ApiResponse { } interface BaseRequest { - token: string; } interface SubdomainRequest extends BaseRequest { @@ -87,13 +87,14 @@ function toast_status(status: number, message: string) { } class serviiApi { - private static async call(endpoint: serviiRequest, body: T): Promise { + private static async call(endpoint: serviiRequest, body: T, token: string): Promise { const unreachable: string = "Couldn't find an available API"; try { const response = await fetch(`${apiUrl}/${endpoint}`, { method: 'POST', headers: { 'Content-Type': 'application/json', + 'SST': token, }, body: JSON.stringify(body), }); @@ -121,81 +122,77 @@ class serviiApi { constructor() {} - private static token(): string { - const currentUser = getAuth().currentUser; - if (!currentUser) { - throw new Error('No user is currently logged in.'); + private static async token(): Promise { + try { + return await auth.currentUser.getIdToken(true); + } catch (error) { + throw new Error("No token retrieved."); } - return currentUser.uid; } public static async setSubdomain(subdomain: string): Promise { - const payload: SubdomainRequest = { token: this.token(), subdomain: subdomain }; - return this.call(serviiRequest.setSubdomain, payload); + const payload: SubdomainRequest = { subdomain: subdomain }; + return this.call(serviiRequest.setSubdomain, payload, await this.token()); } public static async fetchServers(): Promise { - const payload: BaseRequest = { token: this.token() }; - return this.call(serviiRequest.fetchServers, payload); + const payload: BaseRequest = { wait: "wtf" }; + return this.call(serviiRequest.fetchServers, payload, await this.token()); } public static async fetchLogs(name: string): Promise { - const payload: ServerRequest = { token: this.token(), name: name }; - return this.call(serviiRequest.fetchLogs, payload); + const payload: ServerRequest = { name: name }; + return this.call(serviiRequest.fetchLogs, payload, await this.token()); } public static async fetchHistory(name: string): Promise { - const payload: ServerRequest = { token: this.token(), name: name }; - return this.call(serviiRequest.fetchHistory, payload); + const payload: ServerRequest = { name: name }; + return this.call(serviiRequest.fetchHistory, payload, await this.token()); } public static async fetchPlayersStatus(name: string): Promise { - const payload: ServerRequest = { token: this.token(), name: name,}; - return this.call(serviiRequest.fetchPlayersStatus, payload); + const payload: ServerRequest = { name: name,}; + return this.call(serviiRequest.fetchPlayersStatus, payload, await this.token()); } public static async accountCreate(): Promise { const payload: BaseRequest = { token: this.token() }; - return this.call(serviiRequest.accountCreate, payload); + return this.call(serviiRequest.accountCreate, payload, await this.token()); } public static async serverCreate(name: string, version: string, framework: string): Promise { - const payload: ServerCreationRequest = { token: this.token(), name: name, version: version, framework: framework }; - return this.call(serviiRequest.serverCreate, payload); + const payload: ServerCreationRequest = { name: name, version: version, framework: framework }; + return this.call(serviiRequest.serverCreate, payload, await this.token()); } public static async serverDelete(name: string): Promise { - const payload: ServerRequest = { token: this.token(), name: name }; - return this.call(serviiRequest.serverDelete, payload); + const payload: ServerRequest = { name: name }; + return this.call(serviiRequest.serverDelete, payload, await this.token()); } public static async accountDelete(): Promise { const payload: BaseRequest = { token: this.token() }; - return this.call(serviiRequest.accountDelete, payload); + return this.call(serviiRequest.accountDelete, payload, await this.token()); } public static async serverRun(name: string): Promise { - const payload: ServerRequest = { token: this.token(), name: name }; - return this.call(serviiRequest.serverRun, payload); + const payload: ServerRequest = { name: name }; + return this.call(serviiRequest.serverRun, payload, await this.token()); } public static async serverStop(name: string): Promise { - const payload: ServerRequest = { token: this.token(), name: name }; - return this.call(serviiRequest.serverStop, payload); + const payload: ServerRequest = { name: name }; + return this.call(serviiRequest.serverStop, payload, await this.token()); } public static async updateProperties(name: string, props: [string, string][]): Promise { - const payload: UpdatePropertiesRequest = { - token: this.token(), - name: name, - props: props, - }; - return this.call(serviiRequest.updateProperty, payload); + const payload: UpdatePropertiesRequest = { name: name, props: props }; + return this.call(serviiRequest.updateProperty, payload, await this.token()); } public static async command(command: string, name: string): Promise { - const payload: CommandRequest = { token: this.token(), command: command, name: name }; - return this.call(serviiRequest.command, payload); + const payload: CommandRequest = { command: command, name: name }; + return this.call(serviiRequest.command, payload, await this.token()); } } diff --git a/src/service/firebase.jsx b/src/service/firebase.jsx index 428677e..5f84bdd 100644 --- a/src/service/firebase.jsx +++ b/src/service/firebase.jsx @@ -37,4 +37,4 @@ const getUserSubdomain = async (userId) => { } }; -export { auth, googleProvider, signInWithPopup, getUserSubdomain }; +export { auth, googleProvider, signInWithPopup, getUserSubdomain, app };