2026-05-15 14:08:38 +02:00
|
|
|
import * as redirect from "./redirect";
|
|
|
|
|
|
2026-05-15 17:27:27 +02:00
|
|
|
async function sendRequest(endpoint, body) {
|
2026-05-15 14:08:38 +02:00
|
|
|
const request = {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
|
},
|
|
|
|
|
|
2026-05-15 17:27:27 +02:00
|
|
|
body: new URLSearchParams(body)
|
2026-05-15 14:08:38 +02:00
|
|
|
};
|
2026-05-15 17:27:27 +02:00
|
|
|
console.debug(request)
|
2026-05-15 14:08:38 +02:00
|
|
|
|
2026-05-15 17:27:27 +02:00
|
|
|
let response;
|
2026-05-15 16:43:17 +02:00
|
|
|
try {
|
2026-05-15 17:27:27 +02:00
|
|
|
response = await fetch(endpoint, request)
|
2026-05-15 16:43:17 +02:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(response.statusText)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (err) {
|
2026-05-15 17:27:27 +02:00
|
|
|
console.error(err)
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await response.json()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {string} code the authorization code received from the OIDC
|
|
|
|
|
* provider
|
|
|
|
|
* @returns {boolean} true if the token was fetched, false otherwise
|
|
|
|
|
*/
|
|
|
|
|
async function getToken(code) {
|
|
|
|
|
|
|
|
|
|
const auth_url = import.meta.env.VITE_URL;
|
|
|
|
|
const endpoint = `${auth_url}/auth-api/token`;
|
|
|
|
|
const formData = {
|
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
|
"code": code,
|
|
|
|
|
"redirect_uri": `${auth_url}/complete/epita/`,
|
|
|
|
|
"client_id": import.meta.env.VITE_CLIENT_ID
|
|
|
|
|
}
|
|
|
|
|
const response = await sendRequest(endpoint, formData)
|
|
|
|
|
if (response === null) {
|
2026-05-15 14:08:38 +02:00
|
|
|
console.error("Failed to retrieve OIDC token")
|
2026-05-15 16:43:17 +02:00
|
|
|
alert("Failed to retrieve OIDC token")
|
|
|
|
|
|
2026-05-15 14:08:38 +02:00
|
|
|
localStorage.clear();
|
|
|
|
|
redirect.redirectToLoginPage()
|
2026-05-15 17:27:27 +02:00
|
|
|
return false;
|
2026-05-15 14:08:38 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-15 17:27:27 +02:00
|
|
|
localStorage.setItem("token", response.id_token);
|
|
|
|
|
localStorage.setItem("refresh_token", response.refresh_token);
|
|
|
|
|
console.debug("Saved token and refresh tokens")
|
2026-05-15 16:43:17 +02:00
|
|
|
|
2026-05-15 14:08:38 +02:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {string} refreshToken the refresh token (optional)
|
|
|
|
|
* @returns {boolean} whether the token has been refreshed or not
|
|
|
|
|
*/
|
2026-05-15 16:43:17 +02:00
|
|
|
async function refreshToken(refreshToken) {
|
|
|
|
|
|
2026-05-15 17:27:27 +02:00
|
|
|
refreshToken= refreshToken || localStorage.getItem("refresh_token");
|
|
|
|
|
if (!refreshToken) {
|
|
|
|
|
console.error("Unable to retrieve refresh token")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-15 16:43:17 +02:00
|
|
|
const auth_url = import.meta.env.VITE_URL;
|
|
|
|
|
const endpoint = `${auth_url}/auth-api/token`;
|
|
|
|
|
const formData = {
|
2026-05-15 17:27:27 +02:00
|
|
|
"client_id": import.meta.env.VITE_CLIENT_ID,
|
|
|
|
|
// client_secret: "",
|
|
|
|
|
"grant_type": "authorization_code",
|
2026-05-15 16:43:17 +02:00
|
|
|
refresh_token: refreshToken,
|
2026-05-15 17:27:27 +02:00
|
|
|
scope: "epita profile picture"
|
2026-05-15 16:43:17 +02:00
|
|
|
}
|
2026-05-15 17:27:27 +02:00
|
|
|
const response = await sendRequest(endpoint, formData)
|
|
|
|
|
if (response === null) {
|
2026-05-15 16:43:17 +02:00
|
|
|
console.error("Failed to retrieve OIDC token")
|
2026-05-15 17:27:27 +02:00
|
|
|
alert("Failed to retrieve OIDC token")
|
2026-05-15 16:43:17 +02:00
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
redirect.redirectToLoginPage()
|
2026-05-15 17:27:27 +02:00
|
|
|
return false;
|
2026-05-15 16:43:17 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-15 17:27:27 +02:00
|
|
|
localStorage.setItem("token", response.id_token);
|
|
|
|
|
localStorage.setItem("refresh_token", response.refresh_token);
|
|
|
|
|
console.debug("Saved token and refresh tokens")
|
2026-05-15 16:43:17 +02:00
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
2026-05-15 14:08:38 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @returns {boolean} true if the user is authenticated, false otherwise
|
|
|
|
|
*/
|
|
|
|
|
async function authenticate() {
|
|
|
|
|
const token = localStorage.getItem("token");
|
|
|
|
|
if (token !== null)
|
|
|
|
|
return true;
|
|
|
|
|
const refresh_token = localStorage.getItem("refresh_token");
|
|
|
|
|
if (refresh_token !== null)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
redirect.redirectToLoginPage();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME
|
|
|
|
|
// missing functions:
|
2026-05-15 11:08:23 +02:00
|
|
|
// - authedAPIRequest (makes an authenticated request to the API)
|
2026-05-15 14:08:38 +02:00
|
|
|
|
2026-05-15 16:43:17 +02:00
|
|
|
export {
|
|
|
|
|
getToken,
|
2026-05-15 17:27:27 +02:00
|
|
|
refreshToken,
|
2026-05-15 16:43:17 +02:00
|
|
|
authenticate
|
2026-05-15 14:08:38 +02:00
|
|
|
}
|